diff --git a/docs/manual.html b/docs/manual.html index 9b0f04c..dd75d66 100644 --- a/docs/manual.html +++ b/docs/manual.html @@ -708,8 +708,13 @@ By default, if TCLAP sees an argument tha match a specified Arg, it will produce an exception. This strict handling provides some assurance that all input to a program is controlled. However, there are times when -this strict handling of arguments might not be desirable. The alternative -that TCLAP provides is to simply ignore any unmatched +this strict handling of arguments might not be desirable. +TCLAP provides two alternatives. The first is to +add an UnlabeledMultiArg to the command line. If +this is done, all unmatched arguments will get added to this arg. + +The second option is that +that TCLAP can simply ignore any unmatched arguments on the command line. This is accomplished by calling the ignoreUnmatched method with true on the @@ -744,6 +749,11 @@ The program would succeed and the name ValueArg would be populated with "Mike" but the strings "something", "to", and "ignore" would simply be ignored by the parser. +

+NOTE: If both ignoreUnmatched +is set to true and an UnlabeledMultiArg is added to +the command line, then the UnlabeledMultiArg will +"win" and all extra arguments will be added to it rather than be ignored.

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 diff --git a/docs/manual.xml b/docs/manual.xml index 6a068a8..3a25878 100644 --- a/docs/manual.xml +++ b/docs/manual.xml @@ -910,8 +910,13 @@ By default, if TCLAP sees an argument that doesn't match a specified Arg, it will produce an exception. This strict handling provides some assurance that all input to a program is controlled. However, there are times when -this strict handling of arguments might not be desirable. The alternative -that TCLAP provides is to simply ignore any unmatched +this strict handling of arguments might not be desirable. +TCLAP provides two alternatives. The first is to +add an UnlabeledMultiArg to the command line. If +this is done, all unmatched arguments will get added to this arg. + +The second option is that +that TCLAP can simply ignore any unmatched arguments on the command line. This is accomplished by calling the ignoreUnmatched method with true on the @@ -947,6 +952,12 @@ would be populated with "Mike" but the strings "something", "to", and "ignore" would simply be ignored by the parser. + +NOTE: If both ignoreUnmatched +is set to true and an UnlabeledMultiArg is added to +the command line, then the UnlabeledMultiArg will +"win" and all extra arguments will be added to it rather than be ignored. + diff --git a/examples/Makefile.am b/examples/Makefile.am index e342e3f..f75025f 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 test19 test20 test21 test22 + test17 test18 test19 test20 test21 test22 test23 test1_SOURCES = test1.cpp test2_SOURCES = test2.cpp @@ -25,6 +25,7 @@ test19_SOURCES = test19.cpp test20_SOURCES = test20.cpp test21_SOURCES = test21.cpp test22_SOURCES = test22.cpp +test23_SOURCES = test23.cpp AM_CPPFLAGS = -I$(top_srcdir)/include diff --git a/examples/test23.cpp b/examples/test23.cpp new file mode 100644 index 0000000..7087d9e --- /dev/null +++ b/examples/test23.cpp @@ -0,0 +1,80 @@ + + +#include "tclap/CmdLine.h" +#include +#include + +using namespace TCLAP; +using namespace std; + +bool _boolTestB; +string _stringTest; +string _utest; +string _ztest; + +void parseOptions(int argc, char** argv); + +int main(int argc, char** argv) +{ + + parseOptions(argc,argv); + + cout << "for string we got : " << _stringTest<< endl + << "for bool B we got : " << _boolTestB << endl; + +} + + +void parseOptions(int argc, char** argv) +{ + try { + + CmdLine cmd("this is a message", '=', "0.99" ); + cmd.ignoreUnmatched(true); + + // + // Define arguments + // + + SwitchArg btest("B","existTestB", "exist Test B", cmd, false); + + ValueArg stest("s", "stringTest", "string test", true, "homer", + "string", cmd ); + + MultiArg itest("i", "intTest", "multi int test", false,"int", cmd ); + + MultiArg ftest("f", "floatTest", "multi float test", false,"float", + cmd ); + + UnlabeledMultiArg mtest("fileName","file names", false, + "fileNameString", cmd); + // + // Parse the command line. + // + cmd.parse(argc,argv); + + + // + // Set variables + // + _stringTest = stest.getValue(); + _boolTestB = btest.getValue(); + + vector vi = itest.getValue(); + for ( int i = 0; static_cast(i) < vi.size(); i++ ) + cout << "[-i] " << i << " " << vi[i] << endl; + + vector vf = ftest.getValue(); + for ( int i = 0; static_cast(i) < vf.size(); i++ ) + cout << "[-f] " << i << " " << vf[i] << endl; + + vector v = mtest.getValue(); + for ( int i = 0; static_cast(i) < v.size(); i++ ) + cout << "[ ] " << i << " " << v[i] << endl; + + } catch ( ArgException& e ) + { cout << "ERROR: " << e.error() << " " << e.argId() << endl; } +} + + + diff --git a/tests/Makefile.am b/tests/Makefile.am index 8e466f8..d3953d0 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -79,7 +79,9 @@ TESTS = test1.sh \ test77.sh \ test78.sh \ test79.sh \ - test80.sh + test80.sh \ + test81.sh \ + test82.sh EXTRA_DIST = $(TESTS) \ test1.out \ @@ -161,6 +163,8 @@ EXTRA_DIST = $(TESTS) \ test77.out \ test78.out \ test79.out \ - test80.out + test80.out \ + test81.out \ + test82.out CLEANFILES = tmp.out diff --git a/tests/test81.out b/tests/test81.out new file mode 100644 index 0000000..eda65fb --- /dev/null +++ b/tests/test81.out @@ -0,0 +1,9 @@ +PARSE ERROR: + Required argument missing: name + +Brief USAGE: + ../examples/test22 [-r] -n [--] [--version] [-h] + +For complete USAGE and HELP type: + ../examples/test22 --help + diff --git a/tests/test81.sh b/tests/test81.sh new file mode 100755 index 0000000..673ada3 --- /dev/null +++ b/tests/test81.sh @@ -0,0 +1,11 @@ +#!/bin/sh + +# failure, still looking for -n +../examples/test22 asdf asdf -r fds xxx > tmp.out 2>&1 + +if cmp -s tmp.out $srcdir/test81.out; then + exit 0 +else + exit 1 +fi + diff --git a/tests/test82.out b/tests/test82.out new file mode 100644 index 0000000..e24b39c --- /dev/null +++ b/tests/test82.out @@ -0,0 +1,9 @@ +[-i] 0 9 +[-i] 1 8 +[ ] 0 blah +[ ] 1 --blah +[ ] 2 homer +[ ] 3 marge +[ ] 4 bart +for string we got : bill +for bool B we got : 1 diff --git a/tests/test82.sh b/tests/test82.sh new file mode 100755 index 0000000..dba6a18 --- /dev/null +++ b/tests/test82.sh @@ -0,0 +1,11 @@ +#!/bin/sh + +# success - all unmatched args get slurped up in the UnlabeledMultiArg +../examples/test23 blah --blah -s=bill -i=9 -i=8 -B homer marge bart > tmp.out 2>&1 + +if cmp -s tmp.out $srcdir/test82.out; then + exit 0 +else + exit 1 +fi +