#include <cmdline.h>
Public Types | |
enum | option_t { cmdLineDefault = 0, cmdMultiple = 1 } |
Public Methods | |
CmdLine (char c_option_char='-') | |
void | Init (int argc, char *argv[]) |
void | Done () |
template<class T> bool | GetSingleValue (const char *pszOpt, T &tDest) |
bool | GetSingleValue (const char *pszOpt, bool &bDest) |
unsigned short | Call (const char *pszOpt, void(*function)(), char options=cmdLineDefault) |
template<class T> unsigned short | Call (const char *pszOpt, void(*function)(const T &strDest), char options=cmdLineDefault) |
template<class C, class T> unsigned short | Call (const char *pszOpt, C &object, void(C::*function)(const T &strDest), char options=cmdLineDefault) |
Private Types | |
typedef std::vector< const char * > | CharPtrList_t |
Private Methods | |
void | transform (const char *from, std::string &to, const char *) |
template<class T> void | transform (const char *from, T &to, const char *pszOpt) |
bool | IsOption (CharPtrList_t::iterator it) |
const char * | FindFirstArgForOption (const char *pszOpt, CharPtrList_t::iterator &) |
const char * | GetNextArgForOption (const char *pszOpt, CharPtrList_t::iterator &) |
bool | OptionSetNoArgs (const char *pszOpt) |
const char * | getSingleArgForOption (const char *pszOpt) |
Private Attributes | |
CharPtrList_t | m_CharPtrList |
char | m_c_option_char |
CmdLine provids a framework to extract values from the command line. User either GetSingleValue() to retrieve an argument given to an option. Or use Call() to map the usage of command line options and arguments (like "-h", "--help" or "--set-values 117 118") to function calls.
|
A list of const char* |
|
These options are given to Call(): cmdMultiple: allow multiple us of an option resp. multiple arguments for an option. Note, that the following command lines are equivalent: --set-ints 1 2 3 --set-ints 1 --set-ints 2 --set-ints 3Both forms are allowed if option cmdMultiple is used with Call(). |
|
Command line options (like "--help" or "-R") start usually with c_option_char. Some developers may want to use '/' here. |
|
Calls a member function of the given object for each command line argument of option pszOpt; Example: typedef list<string> stringlist_t; stringlist_t stringlist; aCmdLineObj.Call("-add-to-list", stringlist, &stringlist_t::push_back, CmdLine::cmdMultiple); |
|
Call a function of type If used with option cmdMultiple multiple calls to f() are done - one for each argument of given to pszOpt. If pszOpt was given at the command line, this function creates a string object from pszOpts argument and calls f(string_object). If pszOpt is given more than once, then
-s Str1 Str2 -n 3 -s Str3Calling: Call("-s", MyStringFunction)MyStringFunction would be called three time with arguments string(Str1) , string(Str2) and string(Str3) .
|
|
Call a function of type void f() for the given command line option pszOpt. If used with option cmdMultiple, multiple calls to f() are done - one for each occurrence of pszOpt. Else multiple uses of pszOpt leads to throwing a CmdLineException.
Example: Call( "-h", PrintHelp) => a CmdLineException is thrown Call( "-h", PrintHelp, cmdLineDefault) => Print help twice
|
|
Check if all options given at the command line were parsed. Unparsed options are unknown (i.e. unsused) options. Throws a CmdLineException, if such an option is found. Should be called after handling all options using GetSingleValue() or Call(). |
|
find the first argument for an option, mark the option and the argument as handled (by setting them to NULL). The iterator is set to the argument. If the option was used without an argument, a CmdLineException is thrown.
|
|
returns non null if a next (first) arg for the given option was found |
|
Throws an exception if multiple or no arguments were found.
|
|
Set bDest to true if pszOpt was given at the command line, else do not modify bDest. Note that the option must not have an argument.
|
|
Retrieve a single variable of type T given to command line option pszOpt. T may be any type an
Example: If you call
|
|
Initialize command the line parser. argc and argv are usually taken from Note: the strings found in argv are not copied. So argv must exist as long as the CmdLine object. |
|
Check if the given element of m_CharPtrList is an unhandled command line option (and not a handled option or an argument for an option). Note: Handled options are marked by setting to NULL.
|
|
checks if the given option was set at the command line with no args. If so, marks the option as used. If the option was used with an argument, a CmdLineException is thrown.
|
|
cast a const char* into T using >> |
|
cast a const char* into a string |
|
Command line options start with this character - usually '-' (unix) or '/' (windows). May be changed when calling the constructor. |
|
Stores the complete commandline. Holds pointers to the command line options and arguments, but does not allocate memory for these pointers.
|