clockUtils  1.1
ArgParser

Namespaces

 clockUtils
 

Macros

#define CLOCK_ARGPARSER_API
 
#define PARSE_ARGUMENTS(buffer, length)
 parses given list with given length use this method without filename More...
 
#define PARSE_COMMANDLINE()
 parses command line Use this to parse the normal command line in the main function. It is identical to PARSE_ARGUMENTS(++argv, –argc) More...
 
#define GETLASTPARSERERROR()
 returns the last error message if some appeared More...
 
#define REGISTER_VARIABLE_INTERNAL(type, longname, shortname, value, description, required, multiple)
 registers a variable with a type, the variable and argument name, a default value and a description text for –help longname is also the variable name, shortname can be set to "" for no shortname variable will be initialized with default value right away More...
 
#define REGISTER_VARIABLE(type, longname, shortname, value, description)
 registers a variable with a type, the variable and argument name, a default value and a description text for –help being optional longname is also the variable name, shortname can be set to "" for no shortname variable will be initialized with default value right away More...
 
#define REGISTER_VARIABLE_REQUIRED(type, longname, shortname, value, description)
 registers a variable with a type, the variable and argument name, a default value and a description text for –help being required longname is also the variable name, shortname can be set to "" for no shortname variable will be initialized with default value right away if this variable isn't set, ClockError::INVALID_USAGE is returned More...
 
#define REGISTER_VARIABLE_MULTIPLE(type, longname, shortname, value, description)
 registers a variable with a type, the variable and argument name, a default value and a description text for –help longname is also the variable name, shortname can be set to "" for no shortname variable will be initialized with default value right away this variable can be set multiple times, so the registered variable is a list More...
 
#define REGISTER_VARIABLE_MULTIPLE_REQUIRED(type, longname, shortname, value, description)
 registers a variable with a type, the variable and argument name, a default value and a description text for –help being required longname is also the variable name, shortname can be set to "" for no shortname variable will be initialized with default value right away this variable can be set multiple times, so the registered variable is a list if this variable isn't set, ClockError::INVALID_USAGE is returned More...
 
#define REGISTER_VARIABLE_ARGUMENTS(name)
 registers a variable where the arguments at the end are parsed into More...
 
#define HELPSET()
 returns true if –help was set More...
 
#define GETHELPTEXT()
 returns the help text More...
 

Detailed Description

The Argument Parser can be used to easily parse lists of strings for commands and values

How to use the argument parser library

You need to include this header

The usage is really simple and contains just two steps:

First you have to register all variables that should be parseable, that means every argument. For this reason there is the macro REGISTER_VARIABLE(type, longname, shortname, value, description). The five arguments it expects are the type of variable, the long and short name of the option (the long name is used as the variable name in the code), a default value and a description for the –help/-h functionality.
If the variable type contains a comma (e.g. Template class with several parameters) create a typedef (C++ Preprocessor can't handle commas correctly)

Example :

REGISTER_VARIABLE(std::string, myStr, s, "english", "a string containing the used language");
REGISTER_VARIABLE(bool, boolOption, b, false, "a bool value");

The first line will give you a std::string variable with the name myStr being initialized with "english". The two possibilities in a command line to change this variable are:

myProgram --string german
myProgram -s german

The boolean option can be change by passing "true" or "false" as the value. Additionally for the case of bool, only the name is sufficient to set it to true.

myProgram --boolOption
myProgram -b

The second step is to parse your list. Therefor, two macros exist:
PARSE_COMMANDLINE() parses the arguments of the main method implicitly assuming (argc and argv)
PARSE_ARGUMENTS(buffer, length) parses an own list
Two options are automatically available for a help output.

--help
-h

The macro HELPSET() says whether the help optioon was given and the text output can be retrieved using GETHELPTEXT()
Example Usage:

if (HELPSET()) {
std::cout << GETHELPTEXT() << std::endl;
}


In some cases you want to ignore additional entries at the end of the commandline or you want to support more than one parameter e.g. multiple files. Therefore you can register a std::vector<std::string> using REGISTER_VARIABLE_ARGUMENTS(name) where name is the name of the variable. All additional entries are pushed into this vector.
Example:

int main(int argc, const char ** argv) {
std::cout << "Processing " << files.size() << " files" << std::endl;
}

When executing the program as follows ...

myProgram a b c

... the files vector contains three values.

Macro Definition Documentation

§ CLOCK_ARGPARSER_API

#define CLOCK_ARGPARSER_API

Definition at line 41 of file argParserParameters.h.

§ GETHELPTEXT

#define GETHELPTEXT ( )

returns the help text

Definition at line 137 of file ArgumentParser.h.

§ GETLASTPARSERERROR

#define GETLASTPARSERERROR ( )

returns the last error message if some appeared

Definition at line 54 of file ArgumentParser.h.

§ HELPSET

#define HELPSET ( )

returns true if –help was set

Definition at line 131 of file ArgumentParser.h.

§ PARSE_ARGUMENTS

#define PARSE_ARGUMENTS (   buffer,
  length 
)

parses given list with given length use this method without filename

Definition at line 40 of file ArgumentParser.h.

§ PARSE_COMMANDLINE

#define PARSE_COMMANDLINE ( )

parses command line Use this to parse the normal command line in the main function. It is identical to PARSE_ARGUMENTS(++argv, –argc)

Definition at line 48 of file ArgumentParser.h.

§ REGISTER_VARIABLE

#define REGISTER_VARIABLE (   type,
  longname,
  shortname,
  value,
  description 
)

registers a variable with a type, the variable and argument name, a default value and a description text for –help being optional longname is also the variable name, shortname can be set to "" for no shortname variable will be initialized with default value right away

Note
help and h are reserved for internal usage only

Definition at line 92 of file ArgumentParser.h.

§ REGISTER_VARIABLE_ARGUMENTS

#define REGISTER_VARIABLE_ARGUMENTS (   name)

registers a variable where the arguments at the end are parsed into

Definition at line 125 of file ArgumentParser.h.

§ REGISTER_VARIABLE_INTERNAL

#define REGISTER_VARIABLE_INTERNAL (   type,
  longname,
  shortname,
  value,
  description,
  required,
  multiple 
)

registers a variable with a type, the variable and argument name, a default value and a description text for –help longname is also the variable name, shortname can be set to "" for no shortname variable will be initialized with default value right away

Note
help and h are reserved for internal usage only

Definition at line 63 of file ArgumentParser.h.

§ REGISTER_VARIABLE_MULTIPLE

#define REGISTER_VARIABLE_MULTIPLE (   type,
  longname,
  shortname,
  value,
  description 
)

registers a variable with a type, the variable and argument name, a default value and a description text for –help longname is also the variable name, shortname can be set to "" for no shortname variable will be initialized with default value right away this variable can be set multiple times, so the registered variable is a list

Note
help and h are reserved for internal usage only

Definition at line 110 of file ArgumentParser.h.

§ REGISTER_VARIABLE_MULTIPLE_REQUIRED

#define REGISTER_VARIABLE_MULTIPLE_REQUIRED (   type,
  longname,
  shortname,
  value,
  description 
)

registers a variable with a type, the variable and argument name, a default value and a description text for –help being required longname is also the variable name, shortname can be set to "" for no shortname variable will be initialized with default value right away this variable can be set multiple times, so the registered variable is a list if this variable isn't set, ClockError::INVALID_USAGE is returned

Note
help and h are reserved for internal usage only

Definition at line 120 of file ArgumentParser.h.

§ REGISTER_VARIABLE_REQUIRED

#define REGISTER_VARIABLE_REQUIRED (   type,
  longname,
  shortname,
  value,
  description 
)

registers a variable with a type, the variable and argument name, a default value and a description text for –help being required longname is also the variable name, shortname can be set to "" for no shortname variable will be initialized with default value right away if this variable isn't set, ClockError::INVALID_USAGE is returned

Note
help and h are reserved for internal usage only

Definition at line 101 of file ArgumentParser.h.