ASL
Loading...
Searching...
No Matches
CmdArgs Class Reference

Detailed Description

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:

convert -format jpeg -fast! -q 85 image1.png image2.bmp
An Array is a contiguous and resizable array of any type of elements.
Definition Array.h:69

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:

String format = args["format"];
int quality = args["q"] | 90; // use value 90 if the option is not given
bool fast = args.has("fast");
for( int i=0; i < args.length(); i++)
{
}
int length() const
Returns the number of elements in the array.
Definition Array.h:171
CmdArgs is a utility to read command line arguments with options, similar to getopt() but simpler.
Definition CmdArgs.h:83
String represents a character string that behaves similarly to JavaScript strings in that it can be c...
Definition String.h:126

In case of multiple option values, use the () operator to get them:

compile -I /include -I /mylib/include a.cpp b.cpp

Their values can be still retrieved:

for(int i=0; i < includes.length(); i++)
{
}

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.

CmdArgs(argc, argv, "q:,fast,format:");

This object can be created without arguments and anywhere in a program (but this is less portable, currently working on Windows and Linux).

String name = args["name"];

#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< Stringoperator() (const String &opt) const
 Returns an array with all values of option opt if more than one was given.
 
Array< Stringrest () const
 Returns the free arguments, arguments excluding options.
 
Dic< Stringoptions () const
 Returns all options and their values as a Map (only the last value if an option appears more than once)
 
Array< Stringall () const
 Returns all the arguments, as received at main()
 
int length () const
 Returns the number of free arguments, those excluding options.
 
const Array< Stringuntested () const
 Returns the options that have not been tested (probably wrong options)
 

The documentation for this class was generated from the following file: