a bug fix for parsing vectors of strings and making sure that combined switches dont get confused

This commit is contained in:
mes5k 2007-10-02 06:27:42 +00:00
parent f2f8b8d2bc
commit 1edfa65f88
6 changed files with 93 additions and 3 deletions

View File

@ -1,6 +1,6 @@
noinst_PROGRAMS = test1 test2 test3 test4 test5 test6 test7 test8 test9 \
test10 test11 test12
test10 test11 test12 test13
test1_SOURCES = test1.cpp
test2_SOURCES = test2.cpp
@ -14,6 +14,7 @@ test9_SOURCES = test9.cpp
test10_SOURCES = test10.cpp
test11_SOURCES = test11.cpp
test12_SOURCES = test12.cpp
test13_SOURCES = test13.cpp
INCLUDES = -I$(top_srcdir)/include

55
examples/test13.cpp Normal file
View File

@ -0,0 +1,55 @@
#include <iostream>
#include <string>
#include <tclap/CmdLine.h>
using namespace TCLAP;
//
// This file tests that we can parse args from a vector
// of strings rather than argv. This also tests a bug
// where a single element in the vector contains both
// the flag and value AND the value contains the flag
// from another switch arg. This would fool the parser
// into thinking that the string was a combined switches
// string rather than a flag value combo.
//
// This should not print an error
//
int main(int argc, char ** argv)
{
try
{
CmdLine cmd("Test", ' ', "not versioned",true);
MultiArg<std::string> Arg("X","fli","fli module",false,"string");
cmd.add(Arg);
MultiSwitchArg ArgMultiSwitch("d","long_d","example");
cmd.add(ArgMultiSwitch);
std::vector<std::string> in;
in.push_back("prog name");
in.push_back("-X module");
cmd.parse(in);
std::vector<std::string> s = Arg.getValue();
for(unsigned int i = 0 ; i < s.size() ; i++)
{
std::cout << s[i] << "\n";
}
std::cout << "MultiSwtichArg was found " << ArgMultiSwitch.getValue() << " times.\n";
}
catch (ArgException &e) // catch any exceptions
{
std::cerr << "error: " << e.error() << " for arg " << e.argId() << std::endl;
}
std::cout << "done...\n";
return 0;
}

View File

@ -147,6 +147,10 @@ inline bool SwitchArg::combinedSwitchesMatch(std::string& combinedSwitches )
Arg::nameStartString() )
return false;
// make sure the delimiter isn't in the string
if ( combinedSwitches.find_first_of( Arg::delimiter() ) != std::string::npos )
return false;
// ok, we're not specifying a ValueArg, so we know that we have
// a combined switch list.
for ( unsigned int i = 1; i < combinedSwitches.length(); i++ )

View File

@ -61,7 +61,13 @@ TESTS = test1.sh \
test59.sh \
test60.sh \
test61.sh \
test62.sh
test62.sh \
test63.sh \
test64.sh \
test65.sh \
test66.sh \
test67.sh \
test68.sh
EXTRA_DIST = $(TESTS) \
test1.out \
@ -125,6 +131,12 @@ EXTRA_DIST = $(TESTS) \
test59.out \
test60.out \
test61.out \
test62.out
test62.out \
test63.out \
test64.out \
test65.out \
test66.out \
test67.out \
test68.out
CLEANFILES = tmp.out

3
tests/test68.out Normal file
View File

@ -0,0 +1,3 @@
module
MultiSwtichArg was found 0 times.
done...

15
tests/test68.sh Executable file
View File

@ -0,0 +1,15 @@
#!/bin/sh
# this tests whether we can parse args from
# a vector of strings and that combined switch
# handling doesn't get fooled if the delimiter
# is in the string
# success
../examples/test13 > tmp.out 2>&1
if cmp -s tmp.out $srcdir/test68.out; then
exit 0
else
exit 1
fi