Added more unit tests and clarified difference to UnlabeledMultiArg.

This commit is contained in:
Mike Smoot 2012-11-25 14:46:34 -08:00
parent 634cbb01f5
commit a910939b9a
9 changed files with 153 additions and 7 deletions

View File

@ -708,8 +708,13 @@ By default, if <span class="emphasis"><em>TCLAP</em></span> sees an argument tha
match a specified <code class="classname">Arg</code>, it will produce an exception. match a specified <code class="classname">Arg</code>, it will produce an exception.
This strict handling provides some assurance that all input to a program This strict handling provides some assurance that all input to a program
is controlled. However, there are times when is controlled. However, there are times when
this strict handling of arguments might not be desirable. The alternative this strict handling of arguments might not be desirable.
that <span class="emphasis"><em>TCLAP</em></span> provides is to simply ignore any unmatched <span class="emphasis"><em>TCLAP</em></span> provides two alternatives. The first is to
add an <code class="classname">UnlabeledMultiArg</code> to the command line. If
this is done, all unmatched arguments will get added to this arg.
The second option is that
that <span class="emphasis"><em>TCLAP</em></span> can simply ignore any unmatched
arguments on the command line. This is accomplished by calling the arguments on the command line. This is accomplished by calling the
<code class="methodname">ignoreUnmatched</code> method with <code class="methodname">ignoreUnmatched</code> method with
<em class="parameter"><code>true</code></em> on the <em class="parameter"><code>true</code></em> on the
@ -744,6 +749,11 @@ The program would succeed and the name <code class="classname">ValueArg</code>
would be populated with "Mike" but would be populated with "Mike" but
the strings "something", "to", and "ignore" would simply be ignored by the the strings "something", "to", and "ignore" would simply be ignored by the
parser. parser.
</p><p>
<span class="emphasis"><em>NOTE:</em></span> If both <code class="methodname">ignoreUnmatched</code>
is set to true and an <code class="classname">UnlabeledMultiArg</code> is added to
the command line, then the <code class="classname">UnlabeledMultiArg</code> will
"win" and all extra arguments will be added to it rather than be ignored.
</p></div><div class="sect1" lang="en" xml:lang="en"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a id="READING_HEX_INTEGERS"></a>I want to read hex integers as arguments...</h2></div></div></div><p> </p></div><div class="sect1" lang="en" xml:lang="en"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a id="READING_HEX_INTEGERS"></a>I want to read hex integers as arguments...</h2></div></div></div><p>
Sometimes it's desirable to read integers formatted in decimal, hexadecimal, Sometimes it's desirable to read integers formatted in decimal, hexadecimal,
and octal format. This is now possible by #defining the <em class="parameter"><code>TCLAP_SETBASE_ZERO</code></em> and octal format. This is now possible by #defining the <em class="parameter"><code>TCLAP_SETBASE_ZERO</code></em>

View File

@ -910,8 +910,13 @@ By default, if <emphasis>TCLAP</emphasis> sees an argument that doesn't
match a specified <classname>Arg</classname>, it will produce an exception. match a specified <classname>Arg</classname>, it will produce an exception.
This strict handling provides some assurance that all input to a program This strict handling provides some assurance that all input to a program
is controlled. However, there are times when is controlled. However, there are times when
this strict handling of arguments might not be desirable. The alternative this strict handling of arguments might not be desirable.
that <emphasis>TCLAP</emphasis> provides is to simply ignore any unmatched <emphasis>TCLAP</emphasis> provides two alternatives. The first is to
add an <classname>UnlabeledMultiArg</classname> to the command line. If
this is done, all unmatched arguments will get added to this arg.
The second option is that
that <emphasis>TCLAP</emphasis> can simply ignore any unmatched
arguments on the command line. This is accomplished by calling the arguments on the command line. This is accomplished by calling the
<methodname>ignoreUnmatched</methodname> method with <methodname>ignoreUnmatched</methodname> method with
<parameter>true</parameter> on the <parameter>true</parameter> on the
@ -947,6 +952,12 @@ would be populated with "Mike" but
the strings "something", "to", and "ignore" would simply be ignored by the the strings "something", "to", and "ignore" would simply be ignored by the
parser. parser.
</para> </para>
<para>
<emphasis>NOTE:</emphasis> If both <methodname>ignoreUnmatched</methodname>
is set to true and an <classname>UnlabeledMultiArg</classname> is added to
the command line, then the <classname>UnlabeledMultiArg</classname> will
"win" and all extra arguments will be added to it rather than be ignored.
</para>
</sect1> </sect1>
<sect1 id="READING_HEX_INTEGERS"> <sect1 id="READING_HEX_INTEGERS">

View File

@ -1,7 +1,7 @@
noinst_PROGRAMS = test1 test2 test3 test4 test5 test6 test7 test8 test9 \ noinst_PROGRAMS = test1 test2 test3 test4 test5 test6 test7 test8 test9 \
test10 test11 test12 test13 test14 test15 test16 \ test10 test11 test12 test13 test14 test15 test16 \
test17 test18 test19 test20 test21 test22 test17 test18 test19 test20 test21 test22 test23
test1_SOURCES = test1.cpp test1_SOURCES = test1.cpp
test2_SOURCES = test2.cpp test2_SOURCES = test2.cpp
@ -25,6 +25,7 @@ test19_SOURCES = test19.cpp
test20_SOURCES = test20.cpp test20_SOURCES = test20.cpp
test21_SOURCES = test21.cpp test21_SOURCES = test21.cpp
test22_SOURCES = test22.cpp test22_SOURCES = test22.cpp
test23_SOURCES = test23.cpp
AM_CPPFLAGS = -I$(top_srcdir)/include AM_CPPFLAGS = -I$(top_srcdir)/include

80
examples/test23.cpp Normal file
View File

@ -0,0 +1,80 @@
#include "tclap/CmdLine.h"
#include <iostream>
#include <string>
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<string> stest("s", "stringTest", "string test", true, "homer",
"string", cmd );
MultiArg<int> itest("i", "intTest", "multi int test", false,"int", cmd );
MultiArg<float> ftest("f", "floatTest", "multi float test", false,"float",
cmd );
UnlabeledMultiArg<string> mtest("fileName","file names", false,
"fileNameString", cmd);
//
// Parse the command line.
//
cmd.parse(argc,argv);
//
// Set variables
//
_stringTest = stest.getValue();
_boolTestB = btest.getValue();
vector<int> vi = itest.getValue();
for ( int i = 0; static_cast<unsigned int>(i) < vi.size(); i++ )
cout << "[-i] " << i << " " << vi[i] << endl;
vector<float> vf = ftest.getValue();
for ( int i = 0; static_cast<unsigned int>(i) < vf.size(); i++ )
cout << "[-f] " << i << " " << vf[i] << endl;
vector<string> v = mtest.getValue();
for ( int i = 0; static_cast<unsigned int>(i) < v.size(); i++ )
cout << "[ ] " << i << " " << v[i] << endl;
} catch ( ArgException& e )
{ cout << "ERROR: " << e.error() << " " << e.argId() << endl; }
}

View File

@ -79,7 +79,9 @@ TESTS = test1.sh \
test77.sh \ test77.sh \
test78.sh \ test78.sh \
test79.sh \ test79.sh \
test80.sh test80.sh \
test81.sh \
test82.sh
EXTRA_DIST = $(TESTS) \ EXTRA_DIST = $(TESTS) \
test1.out \ test1.out \
@ -161,6 +163,8 @@ EXTRA_DIST = $(TESTS) \
test77.out \ test77.out \
test78.out \ test78.out \
test79.out \ test79.out \
test80.out test80.out \
test81.out \
test82.out
CLEANFILES = tmp.out CLEANFILES = tmp.out

9
tests/test81.out Normal file
View File

@ -0,0 +1,9 @@
PARSE ERROR:
Required argument missing: name
Brief USAGE:
../examples/test22 [-r] -n <string> [--] [--version] [-h]
For complete USAGE and HELP type:
../examples/test22 --help

11
tests/test81.sh Executable file
View File

@ -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

9
tests/test82.out Normal file
View File

@ -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

11
tests/test82.sh Executable file
View File

@ -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