From a15700d36d0331e4c79210ca2bfb770c41768382 Mon Sep 17 00:00:00 2001 From: zeekec Date: Tue, 19 Aug 2008 22:18:47 +0000 Subject: [PATCH] Rethrow ExitExceptions if we're not handling exceptions. --- examples/test18.cpp | 5 ++++- include/tclap/CmdLine.h | 9 +++++++-- tests/Makefile.am | 6 ++++-- tests/test70.out | 21 +++++++++++++++++++++ tests/test70.sh | 12 ++++++++++++ 5 files changed, 48 insertions(+), 5 deletions(-) create mode 100644 tests/test70.out create mode 100755 tests/test70.sh diff --git a/examples/test18.cpp b/examples/test18.cpp index 298db54..f797745 100644 --- a/examples/test18.cpp +++ b/examples/test18.cpp @@ -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(); } } diff --git a/include/tclap/CmdLine.h b/include/tclap/CmdLine.h index d215d4c..afe8fef 100644 --- a/include/tclap/CmdLine.h +++ b/include/tclap/CmdLine.h @@ -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& args) if ( !_handleExceptions) { throw; } - + try { _output->failure(*this,e); } catch ( ExitException &ee ) { @@ -477,6 +477,11 @@ inline void CmdLine::parse(std::vector& args) shouldExit = true; } } catch (ExitException &ee) { + // If we're not handling the exceptions, rethrow. + if ( !_handleExceptions) { + throw; + } + estat = ee.getExitStatus(); shouldExit = true; } diff --git a/tests/Makefile.am b/tests/Makefile.am index 4536856..82b779c 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -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 diff --git a/tests/test70.out b/tests/test70.out new file mode 100644 index 0000000..afba767 --- /dev/null +++ b/tests/test70.out @@ -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. diff --git a/tests/test70.sh b/tests/test70.sh new file mode 100755 index 0000000..596d404 --- /dev/null +++ b/tests/test70.sh @@ -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 +