printing more useful message when missing required args and catching ArgException reference

This commit is contained in:
mes5k 2006-11-04 22:05:32 +00:00
parent db4133a981
commit 54db78bd97
8 changed files with 65 additions and 8 deletions

View File

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

View File

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

View File

@ -1,5 +1,5 @@
PARSE ERROR:
One or more required arguments missing!
Required argument missing: unTest
Brief USAGE:
../examples/test2 [-f <float>] -i <int> -s <string> [-A] [-C] [-B] [--]

View File

@ -1,5 +1,5 @@
PARSE ERROR:
One or more required arguments missing!
Required argument missing: unTest2
Brief USAGE:
../examples/test3 [-f=<float>] ... [-i=<int>] ...

View File

@ -1,5 +1,5 @@
PARSE ERROR:
One or more required arguments missing!
Required argument missing: name
Brief USAGE:
../examples/test1 [-r] -n <string> [--] [--version] [-h]

View File

@ -1,5 +1,5 @@
PARSE ERROR:
One or more required arguments missing!
Required argument missing: unTest2
Brief USAGE:
../examples/test8 [-f=<float>] ... [-i=<int>] ... -s=<string> [-B]

10
tests/test62.out Normal file
View File

@ -0,0 +1,10 @@
PARSE ERROR:
Required arguments missing: intTest, stringTest, unTest
Brief USAGE:
../examples/test2 [-f <float>] -i <int> -s <string> [-A] [-C] [-B] [--]
[--version] [-h] <string> <string> ...
For complete USAGE and HELP type:
../examples/test2 --help

13
tests/test62.sh Executable file
View File

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