ASL
|
CmdArgs is a utility to read command line arguments with options, similar to getopt()
but simpler.
It allows to read named options (starting with a -
), optionally followed by a value, and the rest of arguments after the last option. Options without value are flags and should end with a '!'.
If an option is given more than once its value is the last one, but all its values are accessible with the () operator. The arguments given by this object with the length()
property and the [int]
operator are the free arguments, not counting options or the program name. The whole array of arguments received at main()
can be obtained with the all()
function.
For example:
Has the option format
with value jpeg
, the flag fast
, the option q
with value 85 and 2 free arguments.
That can be read with:
In case of multiple option values, use the () operator to get them:
Their values can be still retrieved:
The object can be given a specification of valid options. This is currently very simple, a comma-separated list of options. Options requiring a value must be followed by a ':' character.
This object can be created without arguments and anywhere in a program (but this is less portable, currently working on Windows and Linux).
#include <CmdArgs.h>
Public Member Functions | |
CmdArgs (const String &spec="") | |
Constructs a CmdArgs object with the arguments of the current process (only on Windows and Linux). | |
CmdArgs (int argc, char **argv, const String &spec="") | |
Constructs a CmdArgs object from the arguments to main() and an optional specification. | |
bool | has (const String &opt) const |
Tests if the given option exists. | |
bool | is (const String &opt) const |
Returns the value of a flag option (if it exists with no value or has a value 1, true or yes) | |
String | operator[] (const String &opt) const |
Returns the value of option named opt or an empty string of it was not defined. | |
String | operator() (const String &opt, const String &def) const |
Returns the value of option named opt or the default value def if it was not defined. | |
String | operator[] (int i) const |
Returns the rest argument at index i (excluding options), or empty string if i is past the number of arguments. | |
Array< String > | operator() (const String &opt) const |
Returns an array with all values of option opt if more than one was given. | |
Array< String > | rest () const |
Returns the free arguments, arguments excluding options. | |
Dic< String > | options () const |
Returns all options and their values as a Map (only the last value if an option appears more than once) | |
Array< String > | all () const |
Returns all the arguments, as received at main() | |
int | length () const |
Returns the number of free arguments, those excluding options. | |
const Array< String > | untested () const |
Returns the options that have not been tested (probably wrong options) | |