diff --git a/docs/manual.html b/docs/manual.html index 28dc348..da4be80 100644 --- a/docs/manual.html +++ b/docs/manual.html @@ -1,27 +1,27 @@ -Templatized C++ Command Line Parser Manual

Templatized C++ Command Line Parser Manual

Michael E Smoot


Table of Contents

1. Basic Usage
Overview
Example
Library Properties
Common Argument Properties
Compiling
2. Fundamental Classes
CmdLine
SwitchArg
ValueArg
MultiArg
MultiSwitchArg
UnlabeledValueArg
UnlabeledMultiArg
3. Complications
I want to combine multiple switches into one argument...
I want one argument or the other, but not both...
I have more arguments than single flags make sense for...
I want to constrain the values allowed for a particular -argument...
I want the Args to add themselves to the CmdLine...
I want different output than what is provided...
I don't want the --help and --version switches to be created automatically...
4. Notes
Type Descriptions
Visitors
More Information

Chapter 1. Basic Usage

Overview

+Templatized C++ Command Line Parser Manual

Templatized C++ Command Line Parser Manual

Michael E Smoot


Chapter 1. Basic Usage

Overview

TCLAP has a few key classes to be aware of. The first is the -CmdLine (command line) class. This class parses +CmdLine (command line) class. This class parses the command line passed to it according to the arguments that it contains. Arguments are separate objects that are added to the -CmdLine object one at a time. The six -argument classes are: ValueArg, -UnlabeledValueArg, -SwitchArg, MultiSwitchArg, -MultiArg and -UnlabeledMultiArg. +CmdLine object one at a time. The six +argument classes are: ValueArg, +UnlabeledValueArg, +SwitchArg, MultiSwitchArg, +MultiArg and +UnlabeledMultiArg. These classes are templatized, which means they can be defined to parse a value of any type**. Once you add the -arguments to the CmdLine object, it parses the +arguments to the CmdLine object, it parses the command line and assigns the data it finds to the specific argument objects it contains. Your program accesses the values parsed by -calls to the getValue() methods of the +calls to the getValue() methods of the argument objects. -

Example

+

Example

Here is a simple example ...

@@ -137,18 +137,18 @@ Where:
    Command description message
 
 

-

Library Properties

+

Library Properties

This example shows a number of different properties of the library...

  • Arguments can appear in any order (...mostly, - more on this later).
  • The help, version -and --SwitchArgs -are specified automatically. Using either the -h or ---help flag will cause the USAGE message to be displayed, --v or --version will cause + more on this later).
  • The help, version +and --SwitchArgs +are specified automatically. Using either the -h or +--help flag will cause the USAGE message to be displayed, +-v or --version will cause any version information to -be displayed, and -- or ---ignore_rest will cause the +be displayed, and -- or +--ignore_rest will cause the remaining labeled arguments to be ignored. These switches are included by default on every command line. You can disable this functionality if desired (although we don't recommend it). How we generate the behavior behind these flags is described @@ -157,24 +157,24 @@ How we generate the behavior behind these flags is described argument isn't provided), the program exits and displays a brief USAGE and an error message.
  • The program name is assumed to always be argv[0], so it isn't specified directly.
  • A value delimiter character can 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 +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! Any problems found in constructing the -CmdLine, constructing the Args, +CmdLine, constructing the Args, or parsing the command line will throw an -ArgException.

-

Common Argument Properties

+ArgException.

+

Common Argument Properties

Arguments, whatever their type, have a few common properties. These properties are set in the constructors of the arguments.

  • First is the flag or the character preceded by a dash(-) that signals the beginning of the argument on the command line.
  • Arguments also have names, which can also be used as an alternative flag on the command line, this time preceded by two dashes -(--) [like the familiar getopt_long()].
  • Next is the description of the argument. This is a short +(--) [like the familiar getopt_long()].
  • Next is the description of the argument. This is a short description of the argument displayed in the help/usage message when needed.
  • The following parameters in the constructors vary depending on the type of argument. Some possible values include: -
    • A boolean value indicating whether the Arg is required or not.
    • A default value.
    • A description of the type of value expected.
    • A constraint on the value expected.
    • The CmdLine instance that the Arg should be added to.
    • A Visitor.
  • See the API Documentation for more detail.

-

Compiling

+

  • A boolean value indicating whether the Arg is required or not.
  • A default value.
  • A description of the type of value expected.
  • A constraint on the value expected.
  • The CmdLine instance that the Arg should be added to.
  • A Visitor.
  • See the API Documentation for more detail.
  • +

    Compiling

    TCLAP is implemented entirely in header files which means you only need to include CmdLine.h to use the library.

    @@ -204,62 +204,62 @@ to work on Windows, Sun and Alpha platforms. We've made every
     effort to keep the library compliant with the ANSI C++ standard so
     if your compiler meets the standard, then this library should work
     for you. Please let us know if this is not the case! 
    -

    Windows Note

    +

    Windows Note

    As we understand things, Visual C++ does not have the file -config.h which is used to make platform +config.h which is used to make platform specific definitions. In this situation, we assume that you -have access to sstream. Our understanding is that +have access to sstream. Our understanding is that this should not be a problem for VC++ 7.x. However, if this -is not the case and you need to use strstream, +is not the case and you need to use strstream, then simply tell your compiler to define the variable -HAVE_STRSTREAM and undefine -HAVE_SSTREAM That +HAVE_STRSTREAM and undefine +HAVE_SSTREAM That should work. We think. Alternatively, just edit -the files ValueArg.h and MultiArg.h. +the files ValueArg.h and MultiArg.h.

    -

    Random Note

    -If your compiler doesn't support the using syntax used -in UnlabeledValueArg and -UnlabeledMultiArg to support two stage name lookup, +

    Random Note

    +If your compiler doesn't support the using syntax used +in UnlabeledValueArg and +UnlabeledMultiArg to support two stage name lookup, then you have two options. Either comment out the statements if you don't need two stage name lookup, or do a bunch of search and replace and use -the this pointer syntax: e.g. -this->_ignoreable instead -of just _ignorable (do this for each variable -or method referenced by using). +the this pointer syntax: e.g. +this->_ignoreable instead +of just _ignorable (do this for each variable +or method referenced by using).

    -

    Chapter 2. Fundamental Classes

    CmdLine

    -The CmdLine class contains the arguments that define +

    Chapter 2. Fundamental Classes

    CmdLine

    +The CmdLine class contains the arguments that define the command line and manages the parsing of the command line. The -CmdLine doesn't parse the command line itself it only +CmdLine doesn't parse the command line itself it only manages the parsing. The actual parsing of individual arguments occurs within -the arguments themselves. The CmdLine keeps track of +the arguments themselves. The CmdLine keeps track of of the required arguments, relationships between arguments, and output generation. -

    SwitchArg

    SwitchArgs are what the name implies: -simple, on/off, boolean switches. Use SwitchArgs +

    SwitchArg

    SwitchArgs are what the name implies: +simple, on/off, boolean switches. Use SwitchArgs anytime you want to turn -some sort of system property on or off. SwitchArgs -don't parse a value. They return TRUE or -FALSE, depending on whether the switch has been found -on the command line and what the default value was defined as.

    ValueArg

    ValueArgs are arguments that read a +some sort of system property on or off. SwitchArgs +don't parse a value. They return TRUE or +FALSE, depending on whether the switch has been found +on the command line and what the default value was defined as.

    ValueArg

    ValueArgs are arguments that read a value of some type from the command line. Any time you need a file name, a number, -etc. use a ValueArg or one of its variants. -All ValueArgs are +etc. use a ValueArg or one of its variants. +All ValueArgs are templatized** and will attempt to parse the string its flag matches on the command line as the type it is -specified as. ValueArg<int> +specified as. ValueArg<int> will attempt to parse an -int, ValueArg<float> will attempt to -parse a float, etc. If operator>> +int, ValueArg<float> will attempt to +parse a float, etc. If operator>> for the specified type doesn't recognize the string on the command line as its defined type, then an exception will be thrown. -

    MultiArg

    -A MultiArg is a ValueArg that +

    MultiArg

    +A MultiArg is a ValueArg that can be specified more than once on a command line and instead of returning -a single value, returns a vector of values. +a single value, returns a vector of values.

    Imagine a compiler that allows you to specify multiple directories to search for libraries... @@ -267,35 +267,35 @@ to search for libraries... % fooCompiler -L /dir/num1 -L /dir/num2 file.foo

    Exceptions will occur if you try to do this -with a ValueArg or a SwitchArg. +with a ValueArg or a SwitchArg. In situations like this, you will want to use a -MultiArg. A -MultiArg is essentially a -ValueArg that appends any +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 is called, a vector of +getValue() method is called, a vector of values, instead of a single value is returned. A -MultiArg is declared much like -a ValueArg: +MultiArg is declared much like +a ValueArg:

                     MultiArg<int> itest("i", "intTest", "multi int test", false,"int" );
                     cmd.add( itest );
     

    -Note that MultiArgs can be added to the -CmdLine in any order (unlike +Note that MultiArgs can be added to the +CmdLine in any order (unlike UnlabeledMultiArg). -

    MultiSwitchArg

    -A MultiSwitchArg is a SwitchArg +

    MultiSwitchArg

    +A MultiSwitchArg is a SwitchArg that can be specified more than once on a command line. This can be useful when command lines are constructed automatically from within other applications or when a switch occurring more than once indicates a value (-V means a little verbose -V -V -V means a lot -verbose), You can use a MultiSwitchArg. +verbose), You can use a MultiSwitchArg. The call -to getValue() for a MultiSwitchArg returns the number (int) of times +to getValue() for a MultiSwitchArg returns the number (int) of times the switch has been found on the command line in addition to the default value. Here is an example using the default initial value of 0:

    @@ -307,22 +307,22 @@ Alternatively, you can specify your own initial value:
     	MultiSwitchArg quiet("q","quiet","Reduce the volume of output",5);
     	cmd.add( quiet );
     

    -

    UnlabeledValueArg

    -An UnlabeledValueArg is a ValueArg that is not identified by a flag on the command line. Instead -UnlabeledValueArgs are identified by their position in +

    UnlabeledValueArg

    +An UnlabeledValueArg is a ValueArg that is not identified by a flag on the command line. Instead +UnlabeledValueArgs are identified by their position in the argv array.

    To this point all of our arguments have had labels (flags) identifying them on the command line, but there are some situations where flags are burdensome and not worth the effort. One example might be if you want to implement a magical command we'll -call copy. All copy does is +call copy. All copy does is copy the file specified in the first argument to the file specified in the second argument. -We can do this using UnlabeledValueArgs which are pretty -much just ValueArgs without the flag specified, +We can do this using UnlabeledValueArgs which are pretty +much just ValueArgs without the flag specified, which tells -the CmdLine object to treat them accordingly. +the CmdLine object to treat them accordingly. The code would look like this:

    @@ -335,28 +335,28 @@ The code would look like this:
     
     Everything else is handled identically to what is seen above. The
     only difference to be aware of, and this is important: the order
    -that UnlabeledValueArgs are added to the CmdLine 
    +that UnlabeledValueArgs are added to the CmdLine 
     is the order that they will be parsed!!!! 
     This is not the case for normal 
    -SwitchArgs and ValueArgs. 
    +SwitchArgs and ValueArgs. 
     What happens internally is the first argument that the 
    -CmdLine doesn't recognize is assumed to be 
    -the first UnlabeledValueArg and
    +CmdLine doesn't recognize is assumed to be 
    +the first UnlabeledValueArg and
     parses it as such. Note that you are allowed to intersperse labeled
     args (SwitchArgs and ValueArgs) in between
    -UnlabeledValueArgs (either on the command line 
    -or in the declaration), but the UnlabeledValueArgs 
    +UnlabeledValueArgs (either on the command line 
    +or in the declaration), but the UnlabeledValueArgs 
     will still be parsed in the order they are added. Just remember that order is
     important for unlabeled arguments. 
    -

    UnlabeledMultiArg

    -An UnlabeledMultiArg is an UnlabeledValueArg that allows more than one value to be specified. Only one -UnlabeledMultiArg can be specified per command line. -The UnlabeledMultiArg simply reads the remaining +

    UnlabeledMultiArg

    +An UnlabeledMultiArg is an UnlabeledValueArg that allows more than one value to be specified. Only one +UnlabeledMultiArg can be specified per command line. +The UnlabeledMultiArg simply reads the remaining values from argv up until -- or the end of the array is reached.

    Say you want a strange command that searches each file specified for a given string (let's call it -grep), but you don't want to have to type in all of the file +grep), but you don't want to have to type in all of the file names or write a script to do it for you. Say,

    @@ -364,7 +364,7 @@ names or write a script to do it for you. Say,
     

    First remember that the * is handled by the shell and -expanded accordingly, so what the program grep sees is +expanded accordingly, so what the program grep sees is really something like:

    @@ -372,13 +372,13 @@ really something like:
     

    To handle situations where multiple, unlabeled arguments are needed, -we provide the UnlabeledMultiArg. -UnlabeledMultiArgs +we provide the UnlabeledMultiArg. +UnlabeledMultiArgs are declared much like everything else, but with only a description -of the arguments. By default, if an UnlabeledMultiArg +of the arguments. By default, if an UnlabeledMultiArg is specified, then at least one is required to be present or an exception will be thrown. The most important thing to remember is, -that like UnlabeledValueArgs: order matters! +that like UnlabeledValueArgs: order matters! In fact, an UnlabeledMultiArg must be the last argument added to the CmdLine!. Here is what a declaration looks like: @@ -395,30 +395,30 @@ CmdLine!. Here is what a declaration looks like:

    -You must only ever specify one (1) UnlabeledMultiArg. -One UnlabeledMultiArg will read every unlabeled +You must only ever specify one (1) UnlabeledMultiArg. +One UnlabeledMultiArg will read every unlabeled Arg that wasn't already processed by a -UnlabeledValueArg into a -vector of type T. Any -UnlabeledValueArg or other -UnlabeledMultiArg specified after the first -UnlabeledMultiArg will be ignored, and if +UnlabeledValueArg into a +vector of type T. Any +UnlabeledValueArg or other +UnlabeledMultiArg specified after the first +UnlabeledMultiArg will be ignored, and if they are required, exceptions will be thrown. When you call the -getValue() -method of the UnlabeledValueArg argument, -a vector +getValue() +method of the UnlabeledValueArg 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 or use -several UnlabeledValueArgs. -

    Chapter 3. Complications

    Naturally, what we have seen to this point doesn't satisfy all of our needs. -

    I want to combine multiple switches into one argument...

    -Multiple SwitchArgs can be combined into a +

    I want to combine multiple switches into one argument...

    +Multiple SwitchArgs can be combined into a single argument on the command line. If you have switches -a, -b and -c it is valid to do either: @@ -440,7 +440,7 @@ it is valid to do either: This is to make this library more in line with the POSIX and GNU standards (as I understand them). -

    I want one argument or the other, but not both...

    +

    I want one argument or the other, but not both...

    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 @@ -448,43 +448,41 @@ argument is required, but not both, yet neither argument is strictly necessary by itself. This is called "exclusive or" or "XOR". To accommodate this situation, there is now an option to add two or more -Args to -a CmdLine that are exclusively or'd with one another: -xorAdd(). This means that exactly one of the -Args must be set and no more. +Args to +a CmdLine that are exclusively or'd with one another: +xorAdd(). This means that exactly one of the +Args must be set and no more.

    -xorAdd() comes in two flavors, either -xorAdd(Arg& a, Arg& b) -to add just two Args to be xor'd and -xorAdd( vector<Arg*> xorList ) -to add more than two Args. +xorAdd() comes in two flavors, either +xorAdd(Arg& a, Arg& b) +to add just two Args to be xor'd and +xorAdd( vector<Arg*> xorList ) +to add more than two Args.

     
     
    -        ValueArg<string>  fileArg("f","file","File name to read",true,"homer",
    -                                 "filename");
    -        ValueArg<string>  urlArg("u","url","URL to load",true, 
    -                                    "http://example.com", "URL");
    +        ValueArg<string>  fileArg("f","file","File name to read",true,"/dev/null", "filename");
    +        ValueArg<string>  urlArg("u","url","URL to load",true, "http://example.com", "URL");
     
             cmd.xorAdd( fileArg, urlArg );
             cmd.parse(argc, argv);
     
     

    -Once one Arg in the xor list is matched on the -CmdLine then the others in the xor list will be +Once one Arg in the xor list is matched on the +CmdLine then the others in the xor list will be marked as set. The question then, is how to determine which of the -Args has been set? This is accomplished by calling the -isSet() method for each Arg. If the -Arg has been -matched on the command line, the isSet() will return -TRUE, whereas if the Arg -has been set as a result of matching the other Arg -that was xor'd isSet() will -return FALSE. -(Of course, if the Arg was not xor'd and -wasn't matched, it will also return FALSE.) +Args has been set? This is accomplished by calling the +isSet() method for each Arg. If the +Arg has been +matched on the command line, the isSet() will return +TRUE, whereas if the Arg +has been set as a result of matching the other Arg +that was xor'd isSet() will +return FALSE. +(Of course, if the Arg was not xor'd and +wasn't matched, it will also return FALSE.)

     
    @@ -498,12 +496,34 @@ wasn't matched, it will also return FALSE.)
                     throw("Very bad things...");
     
     

    -

    I have more arguments than single flags make sense for...

    + +It is helpful to note that Args of any type can be xor'd together. +This means that you can xor a SwitchArg with a ValueArg. +This is helpful in situations where one of several options is necessary and one of the options +requires additional information. + +

    +
    +        SwitchArg  stdinArg("s", "stdin", "Read from STDIN", false);
    +        ValueArg<string>  fileArg("f","file","File name to read",true,"/dev/null", "filename");
    +        ValueArg<string>  urlArg("u","url","URL to load",true, "http://example.com", "URL");
    +
    +        vector<Arg*>  xorlist;
    +        xorlist.push_back(&stdinArg);
    +        xorlist.push_back(&fileArg);
    +        xorlist.push_back(&urlArg);
    +
    +        cmd.xorAdd( xorlist );
    +
    +

    + + +

    I have more arguments than single flags make sense for...

    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 the Arg -constructor. This will tell the Arg that only the long +specify Args using only long options. This one is easy to +accomplish, just make the flag value blank in the Arg +constructor. This will tell the Arg that only the long option should be matched and will force users to specify the long option on the command line. The help output is updated accordingly. @@ -515,26 +535,26 @@ option on the command line. The help output is updated accordingly.

    I want to constrain the values allowed for a particular -argument...

    +argument...

    Interface Change!!! Sorry folks, but we've changed -the interface since version 1.0.X for constraining Args. -Constraints are now hidden behind the Constraint +the interface since version 1.0.X for constraining Args. +Constraints are now hidden behind the Constraint interface. To -constrain an Arg simply implement the interface +constrain an Arg simply implement the interface and specify the new class in the constructor as before.

    -You can still constrain Args based on -a list of values. Instead of adding a vector of -allowed values to the Arg directly, -create a ValuesConstraint object -with a vector of values and add that to the -Arg. The Arg constructors +You can still constrain Args based on +a list of values. Instead of adding a vector of +allowed values to the Arg directly, +create a ValuesConstraint object +with a vector of values and add that to the +Arg. The Arg constructors have been modified accordingly.

    When the value for the -Arg is parsed, +Arg is parsed, it is checked against the list of values specified in the -ValuesConstraint. +ValuesConstraint. If the value is in the list then it is accepted. If not, then an exception is thrown. Here is a simple example: @@ -551,31 +571,31 @@ not, then an exception is thrown. Here is a simple example: cmd.add( nameArg );

    -When a ValuesConstraint is specified, +When a ValuesConstraint is specified, instead of a type description being specified in the -Arg, a +Arg, a type description is created by concatenating the values in the allowed list using operator<< for the specified type. The -help/usage for the Arg therefore lists the +help/usage for the Arg therefore lists the allowable values. Because of this, you might want to keep the list relatively small, however there is no limit on this.

    Obviously, a list of allowed values isn't always the best way to constrain things. For instance, one might wish to allow only integers greater than 0. In this case, simply create a class that -implements the Constraint<int> interface and +implements the Constraint<int> interface and checks whether the value parsed is greater than 0 (done in the -check() method) and create your -Arg with your new Constraint. -

    I want the Args to add themselves to the CmdLine...

    -New constructors have been added for each Arg -that take a CmdLine object as an argument. -Each Arg then -adds itself to the CmdLine -object. There is no difference in how the Arg +check() method) and create your +Arg with your new Constraint. +

    I want the Args to add themselves to the CmdLine...

    +New constructors have been added for each Arg +that take a CmdLine object as an argument. +Each Arg then +adds itself to the CmdLine +object. There is no difference in how the Arg is handled between this method and calling the -add() method directly. At the moment, there is -no way to do an xorAdd() from the constructor. Here +add() method directly. At the moment, there is +no way to do an xorAdd() from the constructor. Here is an example:

    @@ -598,14 +618,14 @@ is an example:
             cmd.parse(argc,argv);
     
     

    -

    I want different output than what is provided...

    +

    I want different output than what is provided...

    It is straightforward to change the output generated by TCLAP. Either subclass the -StdOutput class and re-implement the methods you choose, +StdOutput class and re-implement the methods you choose, or write your own class that implements the -CmdLineOutput interface. Once you have done this, -then use the CmdLine setOutput -method to tell the CmdLine to use your new output +CmdLineOutput interface. Once you have done this, +then use the CmdLine setOutput +method to tell the CmdLine to use your new output class. Here is a simple example:

     class MyOutput : public StdOutput
    @@ -644,86 +664,132 @@ int main(int argc, char** argv)
     		// proceed normally ...
     

    -See test4.cpp in the examples directory for the full +See test4.cpp in the examples directory for the full example. NOTE: if you supply your own Output object, we -will not delete it in the CmdLine destructor. This +will not delete it in the CmdLine destructor. This could lead to a (very small) memory leak if you don't take care of the object -yourself. Also note that the failure method is +yourself. Also note that the failure method is now responsible for exiting the application (assuming that is the desired behavior). -

    I don't want the --help and --version switches to be created automatically...

    +

    I don't want the --help and --version switches to be created automatically...

    Help and version information is useful for nearly all command line applications and as such we generate flags that provide those options automatically. However, there are situations when these flags are undesirable. For these cases we've added we've added a forth parameter to the -CmdLine constructor. Making this boolean parameter +CmdLine constructor. Making this boolean parameter false will disable automatic help and version generation.

     		CmdLine cmd("this is a message", ' ', "0.99", false );
     

    -

    Ignoring arguments

    -The -- flag is automatically included in the -CmdLine. +

    I want to ignore certain arguments...

    +The -- flag is automatically included in the +CmdLine. As (almost) per POSIX and GNU standards, any argument specified -after the -- flag is ignored. +after the -- flag is ignored. Almost because if an -UnlabeledValueArg that has not been set or an -UnlabeledMultiArg has been specified, by default -we will assign any arguments beyond the -- +UnlabeledValueArg that has not been set or an +UnlabeledMultiArg has been specified, by default +we will assign any arguments beyond the -- to the those arguments as per the rules above. This is primarily useful if you want to pass in arguments with a dash as the first character of the argument. It -should be noted that even if the -- flag is -passed on the command line, the CmdLine will +should be noted that even if the -- flag is +passed on the command line, the CmdLine will still test to make sure all of the required arguments are present.

    Of course, this isn't how POSIX/GNU handle things, they explicitly -ignore arguments after the --. To accommodate this, -we can make both UnlabeledValueArgs and -UnlabeledMultiArgs ignoreable in their constructors. +ignore arguments after the --. To accommodate this, +we can make both UnlabeledValueArgs and +UnlabeledMultiArgs ignoreable in their constructors. See the API Documentation for details. -

    Chapter 4. Notes

    Table of Contents

    Type Descriptions
    Visitors
    More Information

    +

    I want to read hex integers as arguments...

    +Sometimes it's desirable to read integers formatted in decimal, hexadecimal, +and octal format. This is now possible by #defining the TCLAP_SETBASE_ZERO +directive. Simply define this directive in your code and integer arguments will be parsed +in each base. +

    +
    +#define TCLAP_SETBASE_ZERO 1
    +
    +#include "tclap/CmdLine.h"
    +#include <iostream>
    +
    +using namespace TCLAP;
    +using namespace std;
    +
    +int main(int argc, char** argv)
    +{
    +
    +	try {
    +
    +	CmdLine cmd("this is a message", ' ', "0.99" );
    +
    +	ValueArg<int> itest("i", "intTest", "integer test", true, 5, "int");
    +	cmd.add( itest );
    +
    +	//
    +	// Parse the command line.
    +	//
    +	cmd.parse(argc,argv);
    +
    +	//
    +	// Set variables
    +	//
    +	int _intTest = itest.getValue();
    +	cout << "found int: " << _intTest << endl;
    +
    +	} catch ( ArgException& e )
    +	{ cout << "ERROR: " << e.error() << " " << e.argId() << endl; }
    +}
    +

    +

    +The reason that this behavior is not the default behavior for TCLAP is that the use of +setbase(0) appears to be something of a side effect and is not necessarily how +setbase() is meant to be used. So while we're making this functionality +available, we're not turning it on by default for fear of bad things happening in different compilers. +If you know otherwise, please let us know. +

    Chapter 4. Notes

    Table of Contents

    Type Descriptions
    Visitors
    More Information

    Like all good rules, there are many exceptions.... -

    Type Descriptions

    +

    Type Descriptions

    Ideally this library would use RTTI to return a human readable name of the type declared for a particular argument. Unfortunately, at -least for g++, the names returned aren't +least for g++, the names returned aren't particularly useful. -

    Visitors

    +

    Visitors

    Disclaimer: Almost no one will have any use for -Visitors, they were +Visitors, they were added to provide special handling for default arguments. Nothing -that Visitors do couldn't be accomplished +that Visitors do couldn't be accomplished by the user after the command line has been parsed. If you're still interested, keep reading...

    -Some of you may be wondering how we get the --help, ---version and -- +Some of you may be wondering how we get the --help, +--version and -- arguments to do their thing without mucking up the -CmdLine code with lots of if +CmdLine code with lots of if statements and type checking. This is accomplished by using a variation on the Visitor Pattern. Actually, it may not be a Visitor Pattern at all, but that's what inspired me.

    If we want some argument to do some sort of special handling, -besides simply parsing a value, then we add a Visitor -pointer to the Arg. More specifically, we add a -subclass of the Visitor +besides simply parsing a value, then we add a Visitor +pointer to the Arg. More specifically, we add a +subclass of the Visitor class. Once the argument has been successfully parsed, the -Visitor for that argument is +Visitor for that argument is called. Any data that needs to be operated on is declared in the -Visitor constructor and then operated on in the -visit() method. A Visitor -is added to an Arg as the last argument in its +Visitor constructor and then operated on in the +visit() method. A Visitor +is added to an Arg as the last argument in its declaration. This may sound complicated, but it is pretty straightforward. Let's see an example.

    -Say you want to add an --authors flag to a program that +Say you want to add an --authors flag to a program that prints the names of the authors when present. First subclass -Visitor: +Visitor:

     
    @@ -744,7 +810,7 @@ class AuthorVisitor : public Visitor
     
     Now include this class definition somewhere and go about creating
     your command line. When you create the author switch, add the
    -AuthorVisitor pointer as follows:
    +AuthorVisitor pointer as follows:
     
     

     
    @@ -754,12 +820,12 @@ your command line. When you create the author switch, add the
     
     

    -Now, any time the -a or ---author flag is specified, +Now, any time the -a or +--author flag is specified, the program will print the author name, Homer J. Simpson and exit without processing any further (as specified in the -visit() method). -

    More Information

    +visit() method). +

    More Information

    For more information, look at the API Documentation and the examples included with the distribution. diff --git a/docs/manual.xml b/docs/manual.xml index 6d4ebc4..4d12833 100644 --- a/docs/manual.xml +++ b/docs/manual.xml @@ -872,10 +872,9 @@ false will disable automatic help and version generation. - -Ignoring arguments +I want to ignore certain arguments... The -- flag is automatically included in the CmdLine. @@ -903,6 +902,61 @@ See the API Documentation for details. + +I want to read hex integers as arguments... + +Sometimes it's desirable to read integers formatted in decimal, hexadecimal, +and octal format. This is now possible by #defining the TCLAP_SETBASE_ZERO +directive. Simply define this directive in your code and integer arguments will be parsed +in each base. + + +#define TCLAP_SETBASE_ZERO 1 + +#include "tclap/CmdLine.h" +#include <iostream> + +using namespace TCLAP; +using namespace std; + +int main(int argc, char** argv) +{ + + try { + + CmdLine cmd("this is a message", ' ', "0.99" ); + + ValueArg<int> itest("i", "intTest", "integer test", true, 5, "int"); + cmd.add( itest ); + + // + // Parse the command line. + // + cmd.parse(argc,argv); + + // + // Set variables + // + int _intTest = itest.getValue(); + cout << "found int: " << _intTest << endl; + + } catch ( ArgException& e ) + { cout << "ERROR: " << e.error() << " " << e.argId() << endl; } +} + + + +The reason that this behavior is not the default behavior for TCLAP is that the use of +setbase(0) appears to be something of a side effect and is not necessarily how +setbase() is meant to be used. So while we're making this functionality +available, we're not turning it on by default for fear of bad things happening in different compilers. +If you know otherwise, please let us know. + + + + + + Notes diff --git a/examples/Makefile.am b/examples/Makefile.am index 229337d..e4d88aa 100644 --- a/examples/Makefile.am +++ b/examples/Makefile.am @@ -1,7 +1,7 @@ noinst_PROGRAMS = test1 test2 test3 test4 test5 test6 test7 test8 test9 \ test10 test11 test12 test13 test14 test15 test16 \ - test17 test18 + test17 test18 test19 test1_SOURCES = test1.cpp test2_SOURCES = test2.cpp @@ -21,6 +21,7 @@ test15_SOURCES = test15.cpp test16_SOURCES = test16.cpp test17_SOURCES = test17.cpp test17-a.cpp test18_SOURCES = test18.cpp +test19_SOURCES = test19.cpp AM_CPPFLAGS = -I$(top_srcdir)/include diff --git a/examples/test19.cpp b/examples/test19.cpp new file mode 100644 index 0000000..5513e78 --- /dev/null +++ b/examples/test19.cpp @@ -0,0 +1,39 @@ + + +#define TCLAP_SETBASE_ZERO 1 + +#include "tclap/CmdLine.h" +#include +#include + + +using namespace TCLAP; +using namespace std; + +int main(int argc, char** argv) +{ + + try { + + CmdLine cmd("this is a message", ' ', "0.99" ); + + ValueArg itest("i", "intTest", "integer test", true, 5, "int"); + cmd.add( itest ); + + // + // Parse the command line. + // + cmd.parse(argc,argv); + + // + // Set variables + // + int _intTest = itest.getValue(); + cout << "found int: " << _intTest << endl; + + } catch ( ArgException& e ) + { cout << "ERROR: " << e.error() << " " << e.argId() << endl; } +} + + + diff --git a/include/tclap/Arg.h b/include/tclap/Arg.h index e461d81..7312423 100644 --- a/include/tclap/Arg.h +++ b/include/tclap/Arg.h @@ -35,6 +35,8 @@ #include #include #include +#include +#include #if defined(HAVE_SSTREAM) #include @@ -384,7 +386,11 @@ ExtractValue(T &destVal, const std::string& strVal, ValueLike vl) int valuesRead = 0; while ( is.good() ) { if ( is.peek() != EOF ) +#ifdef TCLAP_SETBASE_ZERO + is >> std::setbase(0) >> destVal; +#else is >> destVal; +#endif else break; diff --git a/tests/Makefile.am b/tests/Makefile.am index 82b779c..ed675b2 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -69,7 +69,9 @@ TESTS = test1.sh \ test67.sh \ test68.sh \ test69.sh \ - test70.sh + test70.sh \ + test72.sh \ + test71.sh EXTRA_DIST = $(TESTS) \ test1.out \ @@ -141,6 +143,8 @@ EXTRA_DIST = $(TESTS) \ test67.out \ test68.out \ test69.out \ - test70.out + test70.out \ + test71.out \ + test72.out CLEANFILES = tmp.out diff --git a/tests/test29.out b/tests/test29.out index a8aa5ad..20d1692 100644 --- a/tests/test29.out +++ b/tests/test29.out @@ -1,5 +1,5 @@ PARSE ERROR: Argument: -i (--intTest) - Couldn't read argument value from string '0x2' + Couldn't read argument value from string '0xA' Brief USAGE: ../examples/test2 [-f ] -i -s [-A] [-C] [-B] [--] diff --git a/tests/test29.sh b/tests/test29.sh index b187070..15982d8 100755 --- a/tests/test29.sh +++ b/tests/test29.sh @@ -1,7 +1,7 @@ #!/bin/sh -# failure ! ack no hex :( -../examples/test2 -i 0x2 -f 4.2 -s asdf asdf > tmp.out 2>&1 +# failure... no hex here, but see test19.cpp for how to use hex +../examples/test2 -i 0xA -f 4.2 -s asdf asdf > tmp.out 2>&1 if cmp -s tmp.out $srcdir/test29.out; then exit 0 diff --git a/tests/test71.out b/tests/test71.out new file mode 100644 index 0000000..9fda70a --- /dev/null +++ b/tests/test71.out @@ -0,0 +1 @@ +found int: 10 diff --git a/tests/test71.sh b/tests/test71.sh new file mode 100755 index 0000000..0e418d5 --- /dev/null +++ b/tests/test71.sh @@ -0,0 +1,11 @@ +#!/bin/sh + +# success test hex +../examples/test19 -i 0xA > tmp.out 2>&1 + +if cmp -s tmp.out $srcdir/test71.out; then + exit 0 +else + exit 1 +fi + diff --git a/tests/test72.out b/tests/test72.out new file mode 100644 index 0000000..9fda70a --- /dev/null +++ b/tests/test72.out @@ -0,0 +1 @@ +found int: 10 diff --git a/tests/test72.sh b/tests/test72.sh new file mode 100755 index 0000000..d62b74a --- /dev/null +++ b/tests/test72.sh @@ -0,0 +1,11 @@ +#!/bin/sh + +# success test octal +../examples/test19 -i 012 > tmp.out 2>&1 + +if cmp -s tmp.out $srcdir/test72.out; then + exit 0 +else + exit 1 +fi +