Rethrow ExitExceptions if we're not handling exceptions.

This commit is contained in:
zeekec 2008-08-19 22:18:47 +00:00
parent 8769a07cee
commit a15700d36d
5 changed files with 48 additions and 5 deletions

View File

@ -10,7 +10,7 @@ int main(int argc, char** argv)
{
try {
CmdLine cmd("Command description message", ' ', "0.9", false);
CmdLine cmd("Command description message", ' ', "0.9", true);
cmd.setExceptionHandling(false);
@ -19,6 +19,9 @@ int main(int argc, char** argv)
} catch (ArgException &e) { // catch any exceptions
cerr << "error: " << e.error() << " for arg " << e.argId() << endl;
return 1;
} catch (ExitException &e) { // catch any exceptions
cerr << "Exiting on ExitException." << endl;
return e.getExitStatus();
}
}

View File

@ -133,7 +133,7 @@ class CmdLine : public CmdLineInterface
* Should CmdLine handle parsing exceptions internally?
*/
bool _handleExceptions;
/**
* Throws an exception listing the missing args.
*/
@ -469,7 +469,7 @@ inline void CmdLine::parse(std::vector<std::string>& args)
if ( !_handleExceptions) {
throw;
}
try {
_output->failure(*this,e);
} catch ( ExitException &ee ) {
@ -477,6 +477,11 @@ inline void CmdLine::parse(std::vector<std::string>& args)
shouldExit = true;
}
} catch (ExitException &ee) {
// If we're not handling the exceptions, rethrow.
if ( !_handleExceptions) {
throw;
}
estat = ee.getExitStatus();
shouldExit = true;
}

View File

@ -68,7 +68,8 @@ TESTS = test1.sh \
test66.sh \
test67.sh \
test68.sh \
test69.sh
test69.sh \
test70.sh
EXTRA_DIST = $(TESTS) \
test1.out \
@ -139,6 +140,7 @@ EXTRA_DIST = $(TESTS) \
test66.out \
test67.out \
test68.out \
test69.out
test69.out \
test70.out
CLEANFILES = tmp.out

21
tests/test70.out Normal file
View File

@ -0,0 +1,21 @@
USAGE:
../examples/test18 [--] [--version] [-h]
Where:
--, --ignore_rest
Ignores the rest of the labeled arguments following this flag.
--version
Displays version information and exits.
-h, --help
Displays usage information and exits.
Command description message
Exiting on ExitException.

12
tests/test70.sh Executable file
View File

@ -0,0 +1,12 @@
#!/bin/sh
# Checks that parsing exceptions are properly
# propagated to the caller.
../examples/test18 --help > tmp.out 2>&1
if cmp -s tmp.out $srcdir/test70.out; then
exit 0
else
exit 1
fi