C12Adapter Opensource C++ Interface
|
Command line parser to help dealing with argv
and argc
arguments within function main
.
More...
Public Types | |
enum | { HelpOutputLineLength = 80, HelpOutputBorder = 20 } |
Public Member Functions | |
MCommandLineParser () | |
Create a new command line parser. More... | |
~MCommandLineParser () | |
Destroy the object and reclaim its resources back to the operating system. | |
void | DeclareFlag (char shortName, const char *longName, const char *description, bool &destination) |
Declare boolean flag that the command line will take. More... | |
void | DeclareNamedBool (char shortName, const char *longName, const char *description, bool &destination) |
Declare a named argument of boolean type, one given as a flag with value. More... | |
void | DeclareNamedInt (char shortName, const char *longName, const char *label, const char *description, int &destination) |
Declare a named argument of integer type, one given as a flag with value. More... | |
void | DeclareNamedUnsignedInt (char shortName, const char *longName, const char *label, const char *description, unsigned &destination) |
Declare a named argument of unsigned integer type, one given as a flag with value. More... | |
void | DeclareNamedDouble (char shortName, const char *longName, const char *label, const char *description, double &destination) |
Declare a named argument of double precision floating point type, one given as a flag with value. More... | |
void | DeclareNamedString (char shortName, const char *longName, const char *label, const char *description, MStdString &destination) |
Declare a named argument of string type, one given as a flag with value. More... | |
void | DeclareBool (const char *label, const char *description, bool &destination) |
Declare mandatory positional argument of Boolean type. More... | |
void | DeclareInt (const char *label, const char *description, int &destination) |
Declare mandatory positional argument of integer type. More... | |
void | DeclareUnsignedInt (const char *label, const char *description, unsigned &destination) |
Declare mandatory positional argument of unsigned integer type. More... | |
void | DeclareDouble (const char *label, const char *description, double &destination) |
Declare mandatory positional argument of double precision floating point type. More... | |
void | DeclareString (const char *label, const char *description, MStdString &destination) |
Declare mandatory positional argument of string type. More... | |
void | DeclareOptionalInt (const char *label, const char *description, int &destination) |
Declare optional positional argument of integer type. More... | |
void | DeclareOptionalUnsignedInt (const char *label, const char *description, unsigned &destination) |
Declare optional positional argument of unsigned integer type. More... | |
void | DeclareOptionalDouble (const char *label, const char *description, double &destination) |
Declare optional positional argument of double precision floating point type. More... | |
void | DeclareOptionalString (const char *label, const char *description, MStdString &destination) |
Declare optional positional argument of string type. More... | |
void | DeclareStringVector (const char *label, const char *description, MStdStringVector &destination) |
Declare a list of positional arguments, strings. More... | |
MStream * | GetErrorStream () |
void | SetErrorStream (MStream *stream) |
MStream * | GetOutputStream () |
void | SetOutputStream (MStream *stream) |
const MStdString & | GetCopyright () const |
void | SetCopyright (const char *copyrightMessage) |
void | SetCopyright (const MStdString ©rightMessage) |
const MStdString & | GetDescription () const |
void | SetDescription (const char *description) |
void | SetDescription (const MStdString &description) |
const MStdString & | GetVersion () const |
void | SetVersion (const char *versionString) |
void | SetVersion (const MStdString &versionString) |
const MStdString & | GetExecutableName () const |
void | SetExecutableName (const char *name) |
void | SetExecutableName (const MStdString &name) |
const MStdString & | GetBuildDate () const |
void | SetBuildDate (const char *date) |
void | SetBuildDate (const MStdString &date) |
const MStdString & | GetFooter () const |
void | SetFooter (const char *footer) |
void | SetFooter (const MStdString &footer) |
int | Process (int argc, const char **argv) |
int | Process (int argc, char **argv) |
int | Process (const char *commandLine) |
int | Process (const MStdString &commandLine) |
void | WriteHelp () |
@) More... | |
void | WriteHeader () |
Write software header to the stream. More... | |
void | WriteUsage () |
Write usage to the output stream supplied based on the already given Declare methods. More... | |
void | WriteFooter () |
Write software header to the stream. More... | |
void | WriteException (MException &ex) |
Write the given exception to the error stream. More... | |
void | WriteError (const char *fmt,...) |
Write the given error text with variable number of arguments to the error stream. More... | |
void | WriteError (const MStdString &text) |
Write the given error text to the error stream. More... | |
Command line parser to help dealing with argv
and argc
arguments within function main
.
This is a C++ only class. Use it to parse UNIX like flags and parameters passed to a command line tool. Here is a typical usage:
MCommandLineParser::MCommandLineParser | ( | ) |
Create a new command line parser.
This will typically be a local class within function main
. After creation, the next set of actions will be defining parameters and flags with Declare, and calling Process.
void MCommandLineParser::DeclareBool | ( | const char * | label, |
const char * | description, | ||
bool & | destination | ||
) |
Declare mandatory positional argument of Boolean type.
The order of positional Declare calls determines the order of arguments. For example:
Determines two parameters, first is a Boolean and a second is an integer.
The accepted argument for boolean types are:
label | Name of the value for the argument, typically one word, appears in help as value placeholder. |
description | Description of the flag, an English text. This text is shown to the user at –help option. Text wrapping is done automatically, therefore, the description shall not have line breaks. |
destination | Reference to an integer value into which the argument will be written at the time the Process method is called. |
void MCommandLineParser::DeclareDouble | ( | const char * | label, |
const char * | description, | ||
double & | destination | ||
) |
Declare mandatory positional argument of double precision floating point type.
The order of positional Declare calls determines the order of arguments. For example:
Determines two parameters, first is a string and a second is a floating point number.
label | Name of the value for the argument, typically one word, appears in help as value placeholder. |
description | Description of the flag, an English text. This text is shown to the user at –help option. Text wrapping is done automatically, therefore, the description shall not have line breaks. |
destination | Reference to a floating point number value into which the argument will be written at the time the Process method is called. |
void MCommandLineParser::DeclareFlag | ( | char | shortName, |
const char * | longName, | ||
const char * | description, | ||
bool & | destination | ||
) |
Declare boolean flag that the command line will take.
Flag is a named entity with a short name such as -r
, or long name such as –recursive
. Presence of the flag in the command line is determined by a boolean parameter destination
, which shall be initialized into false
prior to this call.
shortName | One character, the short flag name, such as 'r' for '-r' flag. If this is '\0' the flag has no short name (it has to have a long name in this case) |
longName | Parameter long name, such as 'recursive' for '–recursive' flag. If this is an empty string the flag has no long name (it has to have a short name in this case) |
description | Description of the flag, an English text. This text is shown to the user at –help option. Text wrapping is done automatically, therefore, the description shall not have line breaks. |
destination | Reference to boolean value that will be controlled by presence of such flag in the command line. The particular variable to which this parameter refers will typically be initialized with false. When the flag is present in the command line, after calling any of Process functions, the value will become true. |
void MCommandLineParser::DeclareInt | ( | const char * | label, |
const char * | description, | ||
int & | destination | ||
) |
Declare mandatory positional argument of integer type.
The order of positional Declare calls determines the order of arguments. For example:
Determines two parameters, first is a string and a second is an integer.
label | Name of the value for the argument, typically one word, appears in help as value placeholder. |
description | Description of the flag, an English text. This text is shown to the user at –help option. Text wrapping is done automatically, therefore, the description shall not have line breaks. |
destination | Reference to an integer value into which the argument will be written at the time the Process method is called. |
void MCommandLineParser::DeclareNamedBool | ( | char | shortName, |
const char * | longName, | ||
const char * | description, | ||
bool & | destination | ||
) |
Declare a named argument of boolean type, one given as a flag with value.
Such named argument is always optional, and its presence can be determined by whether or not the default value of destination is unchanged. It can appear at any position in the list. Example:
If the argument is not supplied, the above mentioned goBool
will be unchanged at Process call.
The accepted argument for boolean types are:
Different from the other named parameters, the boolean variant does not have label, as it will always be "0/1"
shortName | One character, the short flag name, such as 'g' for '-g1' flag with value. If this is '\0' the flag has no short name (it has to have a long name in this case) |
longName | Parameter long name, such as 'go' for '–go=0' flag with value. If this is an empty string the flag has no long name (it has to have a short name in this case) |
description | Description of the flag, an English text. This text is shown to the user at –help option. Text wrapping is done automatically, therefore, the description shall not have line breaks. |
destination | Reference to a boolean value into which the argument will be written at the time the Process method is called. |
void MCommandLineParser::DeclareNamedDouble | ( | char | shortName, |
const char * | longName, | ||
const char * | label, | ||
const char * | description, | ||
double & | destination | ||
) |
Declare a named argument of double precision floating point type, one given as a flag with value.
Such argument is always optional, and its presence can be determined by whether or not the default value of destination is unchanged. It can appear at any position in the list. Example:
If the argument is not supplied, the above mentioned mul
will be unchanged at Process call.
shortName | One character, the short flag name, such as 'x' for '-x3.13' flag with value. If this is '\0' the flag has no short name (it has to have a long name in this case) |
longName | Parameter long name, such as 'number' for '–number=4' flag with value. If this is an empty string the flag has no long name (it has to have a short name in this case) |
label | Name of the value for the argument, typically one word, appears in help as value placeholder. |
description | Description of the flag, an English text. This text is shown to the user at –help option. Text wrapping is done automatically, therefore, the description shall not have line breaks. |
destination | Reference to a double precision floating point value into which the argument will be written at the time the Process method is called. |
void MCommandLineParser::DeclareNamedInt | ( | char | shortName, |
const char * | longName, | ||
const char * | label, | ||
const char * | description, | ||
int & | destination | ||
) |
Declare a named argument of integer type, one given as a flag with value.
Such argument is always optional, and its presence can be determined by whether or not the default value of destination is unchanged. It can appear at any position in the list. Example:
If the argument is not supplied, the above mentioned num
will be unchanged at Process call.
shortName | One character, the short flag name, such as 'n' for '-n4' flag with value. If this is '\0' the flag has no short name (it has to have a long name in this case) |
longName | Parameter long name, such as 'number' for '–number=4' flag with value. If this is an empty string the flag has no long name (it has to have a short name in this case) |
label | Name of the value for the argument, typically one word, appears in help as value placeholder. |
description | Description of the flag, an English text. This text is shown to the user at –help option. Text wrapping is done automatically, therefore, the description shall not have line breaks. |
destination | Reference to a integer value into which the argument will be written at the time the Process method is called. |
void MCommandLineParser::DeclareNamedString | ( | char | shortName, |
const char * | longName, | ||
const char * | label, | ||
const char * | description, | ||
MStdString & | destination | ||
) |
Declare a named argument of string type, one given as a flag with value.
Such argument is always optional, and its presence can be determined by whether or not the default value of destination is unchanged. It can appear at any position in the list. Example:
If the argument is not supplied, the above mentioned outputFile
will be unchanged at Process call.
shortName | One character, the short flag name, such as 'f' for '-f file.txt' flag with value. If this is '\0' the flag has no short name (it has to have a long name in this case) |
longName | Parameter long name, such as 'number' for '–number=4' flag with value. If this is an empty string the flag has no long name (it has to have a short name in this case) |
label | Name of the value for the argument, typically one word, appears in help as value placeholder. |
description | Description of the flag, an English text. This text is shown to the user at –help option. Text wrapping is done automatically, therefore, the description shall not have line breaks. |
destination | Reference to a string value into which the argument will be written at the time the Process method is called. |
void MCommandLineParser::DeclareNamedUnsignedInt | ( | char | shortName, |
const char * | longName, | ||
const char * | label, | ||
const char * | description, | ||
unsigned & | destination | ||
) |
Declare a named argument of unsigned integer type, one given as a flag with value.
Such argument is always optional, and its presence can be determined by whether or not the default value of destination is unchanged. It can appear at any position in the list. Example:
If the argument is not supplied, the above mentioned num
will be unchanged at Process call.
shortName | One character, the short flag name, such as 'n' for '-n4' flag with value. If this is '\0' the flag has no short name (it has to have a long name in this case) |
longName | Parameter long name, such as 'number' for '–number=4' flag with value. If this is an empty string the flag has no long name (it has to have a short name in this case) |
label | Name of the value for the argument, typically one word, appears in help as value placeholder. |
description | Description of the flag, an English text. This text is shown to the user at –help option. Text wrapping is done automatically, therefore, the description shall not have line breaks. |
destination | Reference to an unsigned integer value into which the argument will be written at the time the Process method is called. |
void MCommandLineParser::DeclareOptionalDouble | ( | const char * | label, |
const char * | description, | ||
double & | destination | ||
) |
Declare optional positional argument of double precision floating point type.
The order of positional Declare calls determines the order of arguments. Optional arguments can only be at the tail of the argument list, if any of them are present, DeclareStringVector shall not be used. Whether or not an optional argument was specified in the command line can determined by whether or not the destination variable was changed from its initial value.
label | Name of the value for the argument, typically one word, appears in help as value placeholder. |
description | Description of the flag, an English text. This text is shown to the user at –help option. Text wrapping is done automatically, therefore, the description shall not have line breaks. |
destination | Reference to a double precision floating point value into which the argument will be written at the time the Process method is called. |
void MCommandLineParser::DeclareOptionalInt | ( | const char * | label, |
const char * | description, | ||
int & | destination | ||
) |
Declare optional positional argument of integer type.
The order of positional Declare calls determines the order of arguments. Optional arguments can only be at the tail of the argument list, if any of them are present, DeclareStringVector shall not be used. Whether or not an optional argument was specified in the command line can determined by whether or not the destination variable was changed from its initial value.
label | Name of the value for the argument, typically one word, appears in help as value placeholder. |
description | Description of the flag, an English text. This text is shown to the user at –help option. Text wrapping is done automatically, therefore, the description shall not have line breaks. |
destination | Reference to an integer value into which the argument will be written at the time the Process method is called. |
void MCommandLineParser::DeclareOptionalString | ( | const char * | label, |
const char * | description, | ||
MStdString & | destination | ||
) |
Declare optional positional argument of string type.
The order of positional Declare calls determines the order of arguments. Optional arguments can only be at the tail of the argument list, if any of them are present, DeclareStringVector shall not be used. Whether or not an optional argument was specified in the command line can determined by whether or not the destination variable was changed from its initial value.
label | Name of the value for the argument, typically one word, appears in help as value placeholder. |
description | Description of the flag, an English text. This text is shown to the user at –help option. Text wrapping is done automatically, therefore, the description shall not have line breaks. |
destination | Reference to a string value into which the argument will be written at the time the Process method is called. |
void MCommandLineParser::DeclareOptionalUnsignedInt | ( | const char * | label, |
const char * | description, | ||
unsigned & | destination | ||
) |
Declare optional positional argument of unsigned integer type.
The order of positional Declare calls determines the order of arguments. Optional arguments can only be at the tail of the argument list, if any of them are present, DeclareStringVector shall not be used. Whether or not an optional argument was specified in the command line can determined by whether or not the destination variable was changed from its initial value.
label | Name of the value for the argument, typically one word, appears in help as value placeholder. |
description | Description of the flag, an English text. This text is shown to the user at –help option. Text wrapping is done automatically, therefore, the description shall not have line breaks. |
destination | Reference to an unsigned integer value into which the argument will be written at the time the Process method is called. |
void MCommandLineParser::DeclareString | ( | const char * | label, |
const char * | description, | ||
MStdString & | destination | ||
) |
Declare mandatory positional argument of string type.
The order of positional Declare calls determines the order of arguments. For example:
Determines the parameters for a command line utility that copies one given entity into another.
label | Name of the value for the argument, typically one word, appears in help as value placeholder. |
description | Description of the flag, an English text. This text is shown to the user at –help option. Text wrapping is done automatically, therefore, the description shall not have line breaks. |
destination | Reference to a string value into which the argument will be written at the time the Process method is called. |
void MCommandLineParser::DeclareStringVector | ( | const char * | label, |
const char * | description, | ||
MStdStringVector & | destination | ||
) |
Declare a list of positional arguments, strings.
This call will declare a list of arguments, and it is the only way to have optional parameters without names. The list will be located after all positional arguments.
label | Name of the value for the argument, typically one word, appears in help as value placeholder. |
description | Description of the flag, an English text. This text is shown to the user at –help option. Text wrapping is done automatically, therefore, the description shall not have line breaks. |
destination | Reference to a string vector into which the argument will be written at the time the Process method is called. |
void MCommandLineParser::DeclareUnsignedInt | ( | const char * | label, |
const char * | description, | ||
unsigned & | destination | ||
) |
Declare mandatory positional argument of unsigned integer type.
The order of positional Declare calls determines the order of arguments. For example:
Determines two parameters, first is a string, and a second is an unsigned number.
label | Name of the value for the argument, typically one word, appears in help as value placeholder. |
description | Description of the flag, an English text. This text is shown to the user at –help option. Text wrapping is done automatically, therefore, the description shall not have line breaks. |
destination | Reference to an unsigned number value into which the argument will be written at the time the Process method is called. |
|
inline |
Build date, if it has to be passed explicitly.
Typically, there is no need to specify the build date, as it is assigned from DATE macro at the time of library compilation. This call is useful if the library is compiled separately from the application that uses it, in which case the usage will be like:
|
inline |
Copyright message to use in case the command line parser needs to show it to the user.
The copyright message is shown as part of usage message. By default, copyright message is initialized from customization macro M_PRODUCT_LEGAL_COPYRIGHT and in most cases there is no need to call SetCopyright explicitly. Otherwise it should be a single string that includes copyright years and message. Usage example:
|
inline |
Set description message to use in case the command line parser needs to show it to the user.
The description is shown as part of usage message. By default, the description is empty, and it is always a good idea to call this method and specify a text. Usage example:
|
inline |
Stream for error reporting
|
inline |
Executable name, passed explicitly or fetched from argv
Typically, there is no need to specify the name of the executable, as it will be taken from the argv
parameter at the time Process is called. This method is useful if there is a necessity to overwrite the default name.
|
inline |
Set footer message to use in case the command line parser needs to show it to the user.
The footer is typically a list of examples and further references. By default, the footer is empty.
|
inline |
Stream for regular output
|
inline |
String representation of version for the command line tool
The version is shown as part of usage message, and also, when the user requests it with –version flag. Typically, there is no need to specify the version with this call, as by default, M_PRODUCT_VERSION customization macro is used.
Usage example:
int MCommandLineParser::Process | ( | int | argc, |
const char ** | argv | ||
) |
Process the arguments given as argc and argv using previously defined command line flags and parameters
Prior to this call, the parameters and flags shall be declared with a set of Declare methods.
argc | Argument count as given to function main |
argv | List of arguments as given to function main . ANSI C standard requires the item after last to be NULL. |
–help
or –version
are handled.
|
inline |
Process the arguments given as argc and argv using previously defined command line flags and parameters
Prior to this call, the parameters and flags shall be declared with a set of Declare methods.
argc | Argument count as given to function main |
argv | List of arguments as given to function main . ANSI C standard requires the item after last to be NULL. |
–help
or –version
are handled.int MCommandLineParser::Process | ( | const char * | commandLine | ) |
Process command line from string using previously defined command line flags and parameters
Prior to this call, the parameters and flags shall be declared with a set of Declare methods.
commandLine | Whole command line including the application name |
–help
or –version
are handled.
|
inline |
Process command line from string using previously defined command line flags and parameters
Prior to this call, the parameters and flags shall be declared with a set of Declare methods.
commandLine | Whole command line including the application name |
–help
or –version
are handled.
|
inline |
Build date, if it has to be passed explicitly.
Typically, there is no need to specify the build date, as it is assigned from DATE macro at the time of library compilation. This call is useful if the library is compiled separately from the application that uses it, in which case the usage will be like:
|
inline |
Build date, if it has to be passed explicitly.
Typically, there is no need to specify the build date, as it is assigned from DATE macro at the time of library compilation. This call is useful if the library is compiled separately from the application that uses it, in which case the usage will be like:
|
inline |
Copyright message to use in case the command line parser needs to show it to the user.
The copyright message is shown as part of usage message. By default, copyright message is initialized from customization macro M_PRODUCT_LEGAL_COPYRIGHT and in most cases there is no need to call SetCopyright explicitly. Otherwise it should be a single string that includes copyright years and message. Usage example:
|
inline |
Copyright message to use in case the command line parser needs to show it to the user.
The copyright message is shown as part of usage message. By default, copyright message is initialized from customization macro M_PRODUCT_LEGAL_COPYRIGHT and in most cases there is no need to call SetCopyright explicitly. Otherwise it should be a single string that includes copyright years and message. Usage example:
|
inline |
Set description message to use in case the command line parser needs to show it to the user.
The description is shown as part of usage message. By default, the description is empty, and it is always a good idea to call this method and specify a text. Usage example:
|
inline |
Set description message to use in case the command line parser needs to show it to the user.
The description is shown as part of usage message. By default, the description is empty, and it is always a good idea to call this method and specify a text. Usage example:
|
inline |
Stream for error reporting
|
inline |
Executable name, passed explicitly or fetched from argv
Typically, there is no need to specify the name of the executable, as it will be taken from the argv
parameter at the time Process is called. This method is useful if there is a necessity to overwrite the default name.
|
inline |
Executable name, passed explicitly or fetched from argv
Typically, there is no need to specify the name of the executable, as it will be taken from the argv
parameter at the time Process is called. This method is useful if there is a necessity to overwrite the default name.
|
inline |
Set footer message to use in case the command line parser needs to show it to the user.
The footer is typically a list of examples and further references. By default, the footer is empty.
|
inline |
Set footer message to use in case the command line parser needs to show it to the user.
The footer is typically a list of examples and further references. By default, the footer is empty.
|
inline |
Stream for regular output
|
inline |
String representation of version for the command line tool
The version is shown as part of usage message, and also, when the user requests it with –version flag. Typically, there is no need to specify the version with this call, as by default, M_PRODUCT_VERSION customization macro is used.
Usage example:
|
inline |
String representation of version for the command line tool
The version is shown as part of usage message, and also, when the user requests it with –version flag. Typically, there is no need to specify the version with this call, as by default, M_PRODUCT_VERSION customization macro is used.
Usage example:
void MCommandLineParser::WriteError | ( | const char * | fmt, |
... | |||
) |
Write the given error text with variable number of arguments to the error stream.
This is a convenience method for helping programs report errors
fmt | Standard C format string followed by arguments |
void MCommandLineParser::WriteError | ( | const MStdString & | text | ) |
Write the given error text to the error stream.
This is a convenience method for helping programs report errors
text | String to write as error |
void MCommandLineParser::WriteException | ( | MException & | ex | ) |
Write the given exception to the error stream.
This is a convenience method for helping programs report errors
ex | Exception to report |
void MCommandLineParser::WriteFooter | ( | ) |
Write software header to the stream.
void MCommandLineParser::WriteHeader | ( | ) |
Write software header to the stream.
Write program name and copyright message to the output stream.
void MCommandLineParser::WriteHelp | ( | ) |
@)
Write program help to the output stream
Write Header, Usage and Footer to the output stream. This method is of rare use, as the command line parser handles –help
and -h
all by itself internally.
void MCommandLineParser::WriteUsage | ( | ) |
Write usage to the output stream supplied based on the already given Declare methods.