2004-09-14 00:15:02 +04:00
|
|
|
/*=========================================================================
|
|
|
|
|
|
|
|
Program: KWSys - Kitware System Library
|
|
|
|
Module: $RCSfile$
|
|
|
|
|
|
|
|
Copyright (c) Kitware, Inc., Insight Consortium. All rights reserved.
|
|
|
|
See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
|
|
|
|
|
|
|
|
This software is distributed WITHOUT ANY WARRANTY; without even
|
|
|
|
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
|
|
|
|
PURPOSE. See the above copyright notices for more information.
|
|
|
|
|
|
|
|
=========================================================================*/
|
|
|
|
#ifndef @KWSYS_NAMESPACE@_CommandLineArguments_hxx
|
|
|
|
#define @KWSYS_NAMESPACE@_CommandLineArguments_hxx
|
|
|
|
|
|
|
|
#include <@KWSYS_NAMESPACE@/Configure.h>
|
|
|
|
#include <@KWSYS_NAMESPACE@/Configure.hxx>
|
|
|
|
|
|
|
|
#include <@KWSYS_NAMESPACE@/stl/string>
|
|
|
|
|
|
|
|
/* Define this macro temporarily to keep the code readable. */
|
|
|
|
#if !defined (KWSYS_NAMESPACE) && !@KWSYS_NAMESPACE@_NAME_IS_KWSYS
|
|
|
|
# define kwsys_stl @KWSYS_NAMESPACE@_stl
|
|
|
|
#endif
|
|
|
|
|
|
|
|
namespace @KWSYS_NAMESPACE@
|
|
|
|
{
|
|
|
|
|
|
|
|
class CommandLineArgumentsInternal;
|
|
|
|
|
|
|
|
/** \class CommandLineArguments
|
|
|
|
* \brief Command line arguments processing code.
|
|
|
|
*
|
|
|
|
* Find specified arguments with optional options and execute specified methods
|
|
|
|
* or set given variables.
|
2004-09-29 16:34:02 +04:00
|
|
|
*
|
|
|
|
* The two interfaces it knows are callback based and variable based. For
|
|
|
|
* callback based, you have to register callback for particular argument using
|
|
|
|
* AddCallback method. When that argument is passed, the callback will be
|
|
|
|
* called with argument, value, and call data. For boolean (NO_ARGUMENT)
|
|
|
|
* arguments, the value is "1". If the callback returns 0 the argument parsing
|
|
|
|
* will stop with an error.
|
|
|
|
*
|
|
|
|
* For the variable interface you associate variable with each argument. When
|
|
|
|
* the argument is specified, the variable is set to the specified value casted
|
|
|
|
* to the apropriate type. For boolean (NO_ARGUMENT), the value is "1".
|
|
|
|
*
|
|
|
|
* Both interfaces can be used at the same time.
|
|
|
|
*
|
|
|
|
* Possible argument types are:
|
|
|
|
* NO_ARGUMENT - The argument takes no value : --A
|
|
|
|
* CONCAT_ARGUMENT - The argument takes value after no space : --Aval
|
|
|
|
* SPACE_ARGUMENT - The argument takes value after space : --A val
|
|
|
|
* EQUAL_ARGUMENT - The argument takes value after equal : --A=val
|
|
|
|
*
|
|
|
|
* Example use:
|
|
|
|
*
|
|
|
|
* kwsys::CommandLineArguments arg;
|
|
|
|
* arg.Initialize(argc, argv);
|
|
|
|
* typedef kwsys::CommandLineArguments argT;
|
|
|
|
* arg.AddArgument("--something", argT::EQUAL_ARGUMENT, &some_variable,
|
|
|
|
* "This is help string for --something");
|
|
|
|
* if ( !arg.Parse() )
|
|
|
|
* {
|
|
|
|
* kwsys_ios::cerr << "Problem parsing arguments" << kwsys_ios::endl;
|
|
|
|
* res = 1;
|
|
|
|
* }
|
|
|
|
*
|
2004-09-14 00:15:02 +04:00
|
|
|
*/
|
|
|
|
|
|
|
|
class @KWSYS_NAMESPACE@_EXPORT CommandLineArguments
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
CommandLineArguments();
|
|
|
|
~CommandLineArguments();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Various argument types.
|
|
|
|
*/
|
|
|
|
enum ArgumentTypeEnum {
|
2004-09-29 16:34:02 +04:00
|
|
|
NO_ARGUMENT,
|
|
|
|
CONCAT_ARGUMENT,
|
|
|
|
SPACE_ARGUMENT,
|
|
|
|
EQUAL_ARGUMENT
|
2004-09-14 00:15:02 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2004-09-29 16:34:02 +04:00
|
|
|
* Various variable types. When using the variable interface, this specifies
|
|
|
|
* what type the variable is.
|
2004-09-14 00:15:02 +04:00
|
|
|
*/
|
|
|
|
enum VariableTypeEnum {
|
|
|
|
NO_VARIABLE_TYPE = 0, // The variable is not specified
|
|
|
|
INT_TYPE, // The variable is integer (int)
|
|
|
|
BOOL_TYPE, // The vairable is boolean (bool)
|
|
|
|
DOUBLE_TYPE, // The variable is float (double)
|
|
|
|
STRING_TYPE, // The variable is string (char*)
|
|
|
|
STL_STRING_TYPE // The variable is string (char*)
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Prototypes for callbacks for callback interface.
|
|
|
|
*/
|
|
|
|
typedef int(*CallbackType)(const char* argument, const char* value,
|
|
|
|
void* call_data);
|
|
|
|
typedef int(*ErrorCallbackType)(const char* argument, void* client_data);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Initialize internal data structures. This should be called before parsing.
|
|
|
|
*/
|
2004-09-14 18:34:10 +04:00
|
|
|
void Initialize(int argc, const char* const argv[]);
|
2004-09-14 19:48:34 +04:00
|
|
|
void Initialize(int argc, char* argv[]);
|
2004-09-14 00:15:02 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Initialize internal data structure and pass arguments one by one. This is
|
2005-06-17 19:46:29 +04:00
|
|
|
* convenience method for use from scripting languages where argc and argv
|
2004-09-14 00:15:02 +04:00
|
|
|
* are not available.
|
|
|
|
*/
|
|
|
|
void Initialize();
|
|
|
|
void ProcessArgument(const char* arg);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This method will parse arguments and call apropriate methods.
|
|
|
|
*/
|
|
|
|
int Parse();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This method will add a callback for a specific argument. The arguments to
|
|
|
|
* it are argument, argument type, callback method, and call data. The
|
|
|
|
* argument help specifies the help string used with this option. The
|
|
|
|
* callback and call_data can be skipped.
|
|
|
|
*/
|
2004-09-29 16:34:02 +04:00
|
|
|
void AddCallback(const char* argument, ArgumentTypeEnum type,
|
|
|
|
CallbackType callback, void* call_data, const char* help);
|
2004-09-14 00:15:02 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Add handler for argument which is going to set the variable to the
|
2004-09-29 16:34:02 +04:00
|
|
|
* specified value. If the argument is specified, the option is casted to the
|
|
|
|
* apropriate type.
|
2004-09-14 00:15:02 +04:00
|
|
|
*/
|
2004-09-29 16:34:02 +04:00
|
|
|
void AddArgument(const char* argument, ArgumentTypeEnum type, bool* variable,
|
|
|
|
const char* help);
|
|
|
|
void AddArgument(const char* argument, ArgumentTypeEnum type, int* variable,
|
|
|
|
const char* help);
|
|
|
|
void AddArgument(const char* argument, ArgumentTypeEnum type,
|
|
|
|
double* variable, const char* help);
|
|
|
|
void AddArgument(const char* argument, ArgumentTypeEnum type,
|
|
|
|
char** variable, const char* help);
|
|
|
|
void AddArgument(const char* argument, ArgumentTypeEnum type,
|
|
|
|
kwsys_stl::string* variable, const char* help);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add handler for boolean argument. The argument does not take any option
|
|
|
|
* and if it is specified, the value of the variable is true/1, otherwise it
|
|
|
|
* is false/0.
|
|
|
|
*/
|
|
|
|
void AddBooleanArgument(const char* argument, bool* variable, const char*
|
|
|
|
help);
|
|
|
|
void AddBooleanArgument(const char* argument, int* variable, const char*
|
|
|
|
help);
|
2004-09-14 00:15:02 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the callbacks for error handling.
|
|
|
|
*/
|
|
|
|
void SetClientData(void* client_data);
|
|
|
|
void SetUnknownArgumentCallback(ErrorCallbackType callback);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get remaining arguments. It allocates space for argv, so you have to call
|
|
|
|
* delete[] on it.
|
|
|
|
*/
|
|
|
|
void GetRemainingArguments(int* argc, char*** argv);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return string containing help. If the argument is specified, only return
|
|
|
|
* help for that argument.
|
|
|
|
*/
|
|
|
|
const char* GetHelp() { return this->Help.c_str(); }
|
|
|
|
const char* GetHelp(const char* arg);
|
|
|
|
|
|
|
|
/**
|
2004-09-29 16:34:02 +04:00
|
|
|
* Get / Set the help line length. This length is used when generating the
|
|
|
|
* help page. Default length is 80.
|
2004-09-14 00:15:02 +04:00
|
|
|
*/
|
2004-09-16 18:27:17 +04:00
|
|
|
void SetLineLength(unsigned int);
|
2004-09-14 00:15:02 +04:00
|
|
|
unsigned int GetLineLength();
|
|
|
|
|
2004-09-28 19:34:29 +04:00
|
|
|
/**
|
2004-09-29 16:34:02 +04:00
|
|
|
* Get the executable name (argv0). This is only available when using
|
|
|
|
* Initialize with argc/argv.
|
2004-09-28 19:34:29 +04:00
|
|
|
*/
|
|
|
|
const char* GetArgv0();
|
|
|
|
|
2004-09-29 15:56:25 +04:00
|
|
|
/**
|
|
|
|
* Get index of the last argument parsed. This is the last argument that was
|
|
|
|
* parsed ok in the original argc/argv list.
|
|
|
|
*/
|
|
|
|
unsigned int GetLastArgument();
|
|
|
|
|
2004-09-14 00:15:02 +04:00
|
|
|
protected:
|
|
|
|
void GenerateHelp();
|
|
|
|
|
2004-09-29 16:34:02 +04:00
|
|
|
//! This is internal method that registers variable with argument
|
|
|
|
void AddArgument(const char* argument, ArgumentTypeEnum type,
|
|
|
|
VariableTypeEnum vtype, void* variable, const char* help);
|
|
|
|
|
2004-09-14 00:15:02 +04:00
|
|
|
typedef CommandLineArgumentsInternal Internal;
|
|
|
|
Internal* Internals;
|
|
|
|
kwsys_stl::string Help;
|
|
|
|
|
|
|
|
unsigned int LineLength;
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace @KWSYS_NAMESPACE@
|
|
|
|
|
|
|
|
/* Undefine temporary macro. */
|
|
|
|
#if !defined (KWSYS_NAMESPACE) && !@KWSYS_NAMESPACE@_NAME_IS_KWSYS
|
|
|
|
# undef kwsys_stl
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|