From d017b12ca40b7a1158e4733af304ea94f645776c Mon Sep 17 00:00:00 2001 From: mes5k Date: Sun, 4 Jul 2004 02:29:45 +0000 Subject: [PATCH] added tests for allowed --- examples/Makefile.am | 6 +++++- examples/test6.cpp | 47 ++++++++++++++++++++++++++++++++++++++++ examples/test7.cpp | 51 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 103 insertions(+), 1 deletion(-) create mode 100644 examples/test6.cpp create mode 100644 examples/test7.cpp diff --git a/examples/Makefile.am b/examples/Makefile.am index d0225d0..9a22674 100644 --- a/examples/Makefile.am +++ b/examples/Makefile.am @@ -1,17 +1,21 @@ -noinst_PROGRAMS = test1 test2 test3 test4 test5 +noinst_PROGRAMS = test1 test2 test3 test4 test5 test6 test7 test1_SOURCES = test1.cpp test2_SOURCES = test2.cpp test3_SOURCES = test3.cpp test4_SOURCES = test4.cpp test5_SOURCES = test5.cpp +test6_SOURCES = test6.cpp +test7_SOURCES = test7.cpp test1_LDADD = $(top_builddir)/src/libtclap.a test2_LDADD = $(top_builddir)/src/libtclap.a test3_LDADD = $(top_builddir)/src/libtclap.a test4_LDADD = $(top_builddir)/src/libtclap.a test5_LDADD = $(top_builddir)/src/libtclap.a +test6_LDADD = $(top_builddir)/src/libtclap.a +test7_LDADD = $(top_builddir)/src/libtclap.a INCLUDES = -I$(top_builddir)/include diff --git a/examples/test6.cpp b/examples/test6.cpp new file mode 100644 index 0000000..d7aef1b --- /dev/null +++ b/examples/test6.cpp @@ -0,0 +1,47 @@ +#include +#include + +using namespace TCLAP; + +int main(int argc, char** argv) +{ + // Wrap everything in a try block. Do this every time, + // because exceptions will be thrown for problems. + try { + + // Define the command line object. + CmdLine cmd("Command description message", ' ', "0.9"); + + vector allowed; + allowed.push_back("homer"); + allowed.push_back("marge"); + allowed.push_back("bart"); + allowed.push_back("lisa"); + allowed.push_back("maggie"); + + ValueArg nameArg("n","name","Name to print",true,"homer",allowed); + cmd.add( nameArg ); + + vector iallowed; + iallowed.push_back(1); + iallowed.push_back(2); + iallowed.push_back(3); + + UnlabeledValueArg intArg("times","Number of times to print",1, + iallowed,false); + cmd.add( intArg ); + + // Parse the args. + cmd.parse( argc, argv ); + + // Get the value parsed by each arg. + int num = intArg.getValue(); + string name = nameArg.getValue(); + + for ( int i = 0; i < num; i++ ) + cout << "My name is " << name << endl; + + } catch (ArgException e) // catch any exceptions + { cerr << "error: " << e.error() << " for arg " << e.argId() << endl; } +} + diff --git a/examples/test7.cpp b/examples/test7.cpp new file mode 100644 index 0000000..e43de3d --- /dev/null +++ b/examples/test7.cpp @@ -0,0 +1,51 @@ +#include +#include + +using namespace TCLAP; + +int main(int argc, char** argv) +{ + // Wrap everything in a try block. Do this every time, + // because exceptions will be thrown for problems. + try { + + // Define the command line object. + CmdLine cmd("Command description message", ' ', "0.9"); + + vector allowed; + allowed.push_back("homer"); + allowed.push_back("marge"); + allowed.push_back("bart"); + allowed.push_back("lisa"); + allowed.push_back("maggie"); + + MultiArg nameArg("n","name","Name to print",true,allowed); + cmd.add( nameArg ); + + vector iallowed; + iallowed.push_back(1); + iallowed.push_back(2); + iallowed.push_back(3); + + UnlabeledMultiArg intArg("times","Number of times to print",iallowed); + cmd.add( intArg ); + + // Parse the args. + cmd.parse( argc, argv ); + + // Get the value parsed by each arg. + vector num = intArg.getValue(); + + for ( unsigned int i = 0; i < num.size(); i++ ) + cout << "Got num " << num[i] << endl; + + vector name = nameArg.getValue(); + + for ( unsigned int i = 0; i < name.size(); i++ ) + cout << "Got name " << name[i] << endl; + + + } catch (ArgException e) // catch any exceptions + { cerr << "error: " << e.error() << " for arg " << e.argId() << endl; } +} +