-Templatized C++ Command Line Parser Examples
+Templatized C++ Command Line Parser Manual
|
example ...
#include < string >
#include < iostream >
#include < tclap/CmdLine.h >
+using namespace std;
int main(int argc, char** argv)
{
@@ -109,34 +110,41 @@ My name is mike
My name is mike
% tester
-PARSE ERROR: for argument: undefined
- One or more required arguments missing!
+PARSE ERROR:
+ One or more required arguments missing!
+Brief USAGE:
+ tester [-u] -n [--] [-v] [-h]
+
+For complete USAGE and HELP type:
+ tester --help
+
+% tester --help
USAGE:
- test1 [-u] -n < nameString > [--] [-v] [-h]
+ tester [-u] -n [--] [-v] [-h]
+
Where:
- -u, --upperCase
- Print in upper case
+ -u, --upperCase
+ Print in upper case
- -n < nameString >, --name < nameString >
- (required) (value required) Name to print
+ -n , --name
+ (required) (value required) Name to print
- --, --ignore_rest
- Ignores the rest of the labeled arguments following this flag.
+ --, --ignore_rest
+ Ignores the rest of the labeled arguments following this flag.
- -v, --version
- Displays version information and exits.
+ -v, --version
+ Displays version information and exits.
- -h, --help
- Displays usage information and exits.
+ -h, --help
+ Displays usage information and exits.
-Command description message
-
+ Command description message
This example shows a number of different properties of the library...
@@ -145,12 +153,13 @@ This example shows a number of different properties of the library...
The version, help and -- arguments are specified automatically.
If a required argument isn't provided, the program exits and displays
the USAGE, along with an error message.
-New Feature! Note that the creation of the CmdLine object is
+Note that the creation of the CmdLine object is
slightly different now. The program name is assumed to always be
argv[0], so it isn't specified directly (let me know if you don't like
this). More importantly, a delimiter character can now be specified.
this means that if you prefer arguments of the style "-s=asdf" instead
of "-s asdf", you can do so.
+Always wrap everything in a try block that catches ArgExceptions.
Basic Properties
Arguments, whatever their type, have a few common basic properties. These
@@ -240,7 +249,7 @@ allows you to specify multiple directories to search for libraries...
In situations like this, you will want to use a MultiArg. A
MultiArg is essentially a ValueArg that appends any value
that it matches and parses onto a vector of values. When the getValue()
-method id called, a vector of values, instead of a single value is returned.
+method is called, a vector of values, instead of a single value is returned.
A MultiArg is declared much like a ValueArg:
@@ -255,7 +264,7 @@ A MultiArg is declared much like a ValueArg:
Note that MultiArgs can be added to the CmdLine in any
order (unlike UnlabeledMultiArgs).
-But I don't like labelling all of my arguments...
+I don't like labelling all of my arguments...
To this point all of our arguments have had labels (flags) indentifying them
on the command line, but
there are some situations where flags are burdensome and not worth the effort.
@@ -292,7 +301,7 @@ is important for unlabeled arguments.
-But I want an arbitrary number of arguments to be accepted...
+I want an arbitrary number of arguments to be accepted...
Don't worry, we've got you covered. Say you want a strange command that
searches each file specified for a given string (lets call it grep),
but you don't want to have to type in all of the file names or write a
@@ -350,11 +359,12 @@ argument, a vector will be returned. If you can imagine a
situation where there will be multiple args of multiple types (stings, ints,
floats, etc.) then just declare the UnlabeledMultiArg as type
string
-and parse the different values yourself.
+and parse the different values yourself or use several
+UnlabeledValueArgs.
I want one argument or the other, but not both...
-New Feature! Suppose you have a command that must read input from one
+Suppose you have a command that must read input from one
of two possible locations, either a local file or a URL. The command
must read something, so one argument is required, but not both,
yet neither argument is strictly necessary by itself. This is called
@@ -410,7 +420,7 @@ was not xor'd and wasn't matched, it will also return FALSE.)
I have more arguments than single flags make sense for...
-New Feature! Some commands have so many options that single flags
+Some commands have so many options that single flags
no longer map sensibly to the available options. In this case, it is
desirable to specify Args using only long options.
This one is easy to accomplish, just make the flag value blank in
@@ -432,8 +442,8 @@ The help output is updated accordingly.
I want to constrain the values allowed for a particular argument...
-New Feature! There are new constructors for all of the Args that
-take values that allow a list of values to be specified for that particular
+There are now constructors for all of the Args that
+parse values that allow a list of values to be specified for that particular
Arg. When the value for the Arg is parsed, it is checked against
the list of values specified in the constructor. If the value is in the list
then it is accepted. If not, then an exception is thrown.
@@ -473,7 +483,6 @@ Be sure that the description provided with the Arg reflects the
constraint you choose.
-
Visitors
Disclaimer: Almost no one will have any use for Visitors, they were added
@@ -556,7 +565,7 @@ ignore arguments after the --. To accomodate this, we can make both
their constructors. See
the API Documentation for details.
Multiple Identical Switches
-If you absolutely must allow for multiple, identical switches to be allowed,
+If you absolutely must allow for multiple, identical switches, then
don't use a SwitchArg, instead use a MultiArg of type
bool. This means you'll need to specify
a 1 or 0 on the command line with the switch (as values are required), but
|