Main Page   Namespace List   Alphabetical List   Compound List   File List   Compound Members  

cmdl::CmdLine Class Reference

parses the command line for options and arguments. More...

#include <cmdline.h>

List of all members.

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


Detailed Description

parses the command line for options and arguments.

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.

Version:
Version 0.3
See also:
CmdLineException


Member Typedef Documentation

typedef std::vector<const char*> cmdl::CmdLine::CharPtrList_t [private]
 

A list of const char*


Member Enumeration Documentation

enum cmdl::CmdLine::option_t
 

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 3
         
Both forms are allowed if option cmdMultiple is used with Call().
Enumeration values:
cmdLineDefault 
cmdMultiple 


Constructor & Destructor Documentation

cmdl::CmdLine::CmdLine char    c_option_char = '-'
 

Command line options (like "--help" or "-R") start usually with c_option_char. Some developers may want to use '/' here.


Member Function Documentation

template<class C, class T>
unsigned short cmdl::CmdLine::Call const char *    pszOpt,
C &    object,
void(C::*    function)(const T &strDest),
char    options = cmdLineDefault
[inline]
 

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);
 

template<class T>
unsigned short cmdl::CmdLine::Call const char *    pszOpt,
void(*    function)(const T &strDest),
char    options = cmdLineDefault
[inline]
 

Call a function of type void f(const T&) for the given command line option pszOpt. T may be any type a istream &operator<<(istream &,&T) exists for.

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

  • by default a CmdLineException is thrown.
  • if options is set to cmdMultiple f(string_object) is called for each argument of pszOpt.
Example: Command line:
        -s Str1 Str2 -n 3 -s Str3                
Calling:
        Call("-s", MyStringFunction)        
MyStringFunction would be called three time with arguments string(Str1), string(Str2) and string(Str3).
Returns:
number of command line arguments given to option pszOpt.

unsigned short cmdl::CmdLine::Call const char *    pszOpt,
void(*    function)(),
char    options = cmdLineDefault
[inline]
 

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:
Commande line is: -h -h

         Call( "-h", PrintHelp)
                       => a CmdLineException is thrown
         Call( "-h", PrintHelp, cmdLineDefault)
                       => Print help twice
         
Returns:
number of occurences of pszOpt in the command line.

void cmdl::CmdLine::Done  
 

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().

const char* cmdl::CmdLine::FindFirstArgForOption const char *    pszOpt,
CharPtrList_t::iterator &   
[private]
 

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:
char-Pointer to argument, it argument is found resp. NULL, if option was not used.
See also:
CmdLineException

const char* cmdl::CmdLine::GetNextArgForOption const char *    pszOpt,
CharPtrList_t::iterator &   
[private]
 

returns non null if a next (first) arg for the given option was found

const char* cmdl::CmdLine::getSingleArgForOption const char *    pszOpt [private]
 

Throws an exception if multiple or no arguments were found.

Returns:
pointer to a single argument for the given option.

bool cmdl::CmdLine::GetSingleValue const char *    pszOpt,
bool &    bDest
[inline]
 

        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.        

Returns:
true if options was used, else false.      

template<class T>
bool cmdl::CmdLine::GetSingleValue const char *    pszOpt,
T &    tDest
[inline]
 

Retrieve a single variable of type T given to command line option pszOpt. T may be any type an istream &operator<<(istream &,&T) exists for. If pszOpt was not used at the command line, tDest is not changed. If no or two or more arguments are used for the given option, then a CmdLineException is thrown.

Example: If you call GetSingleValue("-s", myStringObj) then

  • the command line is: -s "a String" would set myStringObj to "a String".
  • any command line not using -s would leave myStringObj untouched.
  • when using multiple arguments for -s (like -s "first" "second"), then a CmdLineException is thrown.
  • the same applies for multiple use of option -s (like -s "first" -s "second").
Throws a CmdLineException in case of a command line error (i.e. using -s without an argument or with two or more arguments).

Returns:
true, if pszOpt was given at the command line, else false.

void cmdl::CmdLine::Init int    argc,
char *    argv[]
 

Initialize command the line parser. argc and argv are usually taken from main(int argc, char *argv[]). To skip the program name (the first element of argv), use: Init(--argc, ++argv). If the first char-pointer found in argv is not an option (i.e. it does not start with m_c_option_char), a CmdLineException is thrown.

Note: the strings found in argv are not copied. So argv must exist as long as the CmdLine object.

bool cmdl::CmdLine::IsOption CharPtrList_t::iterator    it [inline, private]
 

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.

Returns:
true if *it is an unhandled option, else false.

bool cmdl::CmdLine::OptionSetNoArgs const char *    pszOpt [private]
 

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.

Returns:
true, if the given option was used at the command line - else false.
See also:
CmdLineException

template<class T>
void cmdl::CmdLine::transform const char *    from,
T &    to,
const char *    pszOpt
[inline, private]
 

cast a const char* into T using >>

void cmdl::CmdLine::transform const char *    from,
std::string &    to,
const char *   
[inline, private]
 

cast a const char* into a string


Member Data Documentation

char cmdl::CmdLine::m_c_option_char [private]
 

Command line options start with this character - usually '-' (unix) or '/' (windows). May be changed when calling the constructor.

CharPtrList_t cmdl::CmdLine::m_CharPtrList [private]
 

Stores the complete commandline. Holds pointers to the command line options and arguments, but does not allocate memory for these pointers.

See also:
Init()


The documentation for this class was generated from the following file:
Generated on Sun Jan 12 11:17:52 2003 for CmdLine by doxygen1.2.16