From 54db78bd9760c859860a0a7af580f877ca76885f Mon Sep 17 00:00:00 2001 From: mes5k Date: Sat, 4 Nov 2006 22:05:32 +0000 Subject: [PATCH] printing more useful message when missing required args and catching ArgException reference --- include/tclap/CmdLine.h | 36 ++++++++++++++++++++++++++++++++++-- tests/Makefile.am | 6 ++++-- tests/test10.out | 2 +- tests/test17.out | 2 +- tests/test4.out | 2 +- tests/test51.out | 2 +- tests/test62.out | 10 ++++++++++ tests/test62.sh | 13 +++++++++++++ 8 files changed, 65 insertions(+), 8 deletions(-) create mode 100644 tests/test62.out create mode 100755 tests/test62.sh diff --git a/include/tclap/CmdLine.h b/include/tclap/CmdLine.h index 49774d7..6634358 100644 --- a/include/tclap/CmdLine.h +++ b/include/tclap/CmdLine.h @@ -114,6 +114,11 @@ class CmdLine : public CmdLineInterface */ CmdLineOutput* _output; + /** + * Throws an exception listing the missing args. + */ + void missingArgsException(); + /** * Checks whether a name/flag string matches entirely matches * the Arg::blankChar. Used when multiple switches are combined @@ -408,12 +413,12 @@ inline void CmdLine::parse(int argc, char** argv) } if ( requiredCount < _numRequired ) - throw(CmdLineParseException("One or more required arguments missing!")); + missingArgsException(); if ( requiredCount > _numRequired ) throw(CmdLineParseException("Too many arguments!")); - } catch ( ArgException e ) { _output->failure(*this,e); exit(1); } + } catch ( ArgException& e ) { _output->failure(*this,e); exit(1); } } inline bool CmdLine::_emptyCombined(const std::string& s) @@ -428,6 +433,33 @@ inline bool CmdLine::_emptyCombined(const std::string& s) return true; } +inline void CmdLine::missingArgsException() +{ + int count = 0; + + std::string missingArgList; + for (ArgListIterator it = _argList.begin(); it != _argList.end(); it++) + { + if ( (*it)->isRequired() && !(*it)->isSet() ) + { + missingArgList += (*it)->getName(); + missingArgList += ", "; + count++; + } + } + missingArgList = missingArgList.substr(0,missingArgList.length()-2); + + std::string msg; + if ( count > 1 ) + msg = "Required arguments missing: "; + else + msg = "Required argument missing: "; + + msg += missingArgList; + + throw(CmdLineParseException(msg)); +} + inline void CmdLine::deleteOnExit(Arg* ptr) { _argDeleteOnExitList.push_back(ptr); diff --git a/tests/Makefile.am b/tests/Makefile.am index 0ac90dc..7268bf5 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -60,7 +60,8 @@ TESTS = test1.sh \ test58.sh \ test59.sh \ test60.sh \ - test61.sh + test61.sh \ + test62.sh EXTRA_DIST = $(TESTS) \ test1.out \ @@ -123,6 +124,7 @@ EXTRA_DIST = $(TESTS) \ test58.out \ test59.out \ test60.out \ - test61.out + test61.out \ + test62.out CLEANFILES = tmp.out diff --git a/tests/test10.out b/tests/test10.out index a991980..da6dd86 100644 --- a/tests/test10.out +++ b/tests/test10.out @@ -1,5 +1,5 @@ PARSE ERROR: - One or more required arguments missing! + Required argument missing: unTest Brief USAGE: ../examples/test2 [-f ] -i -s [-A] [-C] [-B] [--] diff --git a/tests/test17.out b/tests/test17.out index efdadb9..37f86c3 100644 --- a/tests/test17.out +++ b/tests/test17.out @@ -1,5 +1,5 @@ PARSE ERROR: - One or more required arguments missing! + Required argument missing: unTest2 Brief USAGE: ../examples/test3 [-f=] ... [-i=] ... diff --git a/tests/test4.out b/tests/test4.out index c69fab0..caa2664 100644 --- a/tests/test4.out +++ b/tests/test4.out @@ -1,5 +1,5 @@ PARSE ERROR: - One or more required arguments missing! + Required argument missing: name Brief USAGE: ../examples/test1 [-r] -n [--] [--version] [-h] diff --git a/tests/test51.out b/tests/test51.out index 8c59094..e957e5c 100644 --- a/tests/test51.out +++ b/tests/test51.out @@ -1,5 +1,5 @@ PARSE ERROR: - One or more required arguments missing! + Required argument missing: unTest2 Brief USAGE: ../examples/test8 [-f=] ... [-i=] ... -s= [-B] diff --git a/tests/test62.out b/tests/test62.out new file mode 100644 index 0000000..1fd8d7c --- /dev/null +++ b/tests/test62.out @@ -0,0 +1,10 @@ +PARSE ERROR: + Required arguments missing: intTest, stringTest, unTest + +Brief USAGE: + ../examples/test2 [-f ] -i -s [-A] [-C] [-B] [--] + [--version] [-h] ... + +For complete USAGE and HELP type: + ../examples/test2 --help + diff --git a/tests/test62.sh b/tests/test62.sh new file mode 100755 index 0000000..9b365e3 --- /dev/null +++ b/tests/test62.sh @@ -0,0 +1,13 @@ +#!/bin/sh + +# this tests whether all required args are listed as +# missing when no arguments are specified +# failure +../examples/test2 > tmp.out 2>&1 + +if cmp -s tmp.out $srcdir/test62.out; then + exit 0 +else + exit 1 +fi +