diff --git a/examples/Makefile.am b/examples/Makefile.am index 9a8bae4..cad4369 100644 --- a/examples/Makefile.am +++ b/examples/Makefile.am @@ -2,7 +2,7 @@ noinst_PROGRAMS = test1 test2 test3 test4 test5 test6 test7 test8 test9 \ test10 test11 test12 test13 test14 test15 test16 \ test17 test18 test19 test20 test21 test22 test23 test24 \ - test25 test26 test27 + test25 test26 test27 test28 test1_SOURCES = test1.cpp test2_SOURCES = test2.cpp @@ -31,6 +31,7 @@ test24_SOURCES = test24.cpp test25_SOURCES = test25.cpp test26_SOURCES = test26.cpp test27_SOURCES = test27.cpp +test28_SOURCES = test28.cpp AM_CPPFLAGS = -I$(top_srcdir)/include diff --git a/examples/test28.cpp b/examples/test28.cpp new file mode 100644 index 0000000..a8960a1 --- /dev/null +++ b/examples/test28.cpp @@ -0,0 +1,35 @@ +#include + +#include "tclap/CmdLine.h" + +using namespace TCLAP; +using namespace std; + +int main() +{ + try { + CmdLine cmd("test constraint bug"); + ValueArg arg("i","int", "tests int arg", false, 4711, NULL, cmd); + cout << "Expected exception" << endl; + } catch(std::logic_error &e) { /* expected */ } + + try { + CmdLine cmd("test constraint bug"); + ValueArg arg1("i","int", "tests int arg", false, 4711, NULL, NULL); + cout << "Expected exception" << endl; + } catch(std::logic_error &e) { /* expected */ } + + try { + CmdLine cmd("test constraint bug"); + MultiArg arg1("i","int", "tests int arg", false, NULL, NULL); + cout << "Expected exception" << endl; + } catch(std::logic_error &e) { /* expected */ } + + try { + CmdLine cmd("test constraint bug"); + MultiArg arg1("i","int", "tests int arg", false, NULL, cmd); + cout << "Expected exception" << endl; + } catch(std::logic_error &e) { /* expected */ } + + cout << "Passed" << endl; +} diff --git a/include/tclap/Constraint.h b/include/tclap/Constraint.h index a92acf9..4644fc8 100644 --- a/include/tclap/Constraint.h +++ b/include/tclap/Constraint.h @@ -28,6 +28,7 @@ #include #include #include +#include namespace TCLAP { @@ -62,6 +63,12 @@ class Constraint * functions but without a virtual destructor. */ virtual ~Constraint() { ; } + + static std::string shortID(Constraint *constraint) { + if (!constraint) + throw std::logic_error("Cannot create a ValueArg with a NULL constraint"); + return constraint->shortID(); + } }; } //namespace TCLAP diff --git a/include/tclap/MultiArg.h b/include/tclap/MultiArg.h index 28fd7cc..5442540 100644 --- a/include/tclap/MultiArg.h +++ b/include/tclap/MultiArg.h @@ -276,7 +276,7 @@ MultiArg::MultiArg(const std::string& flag, Visitor* v) : Arg( flag, name, desc, req, true, v ), _values(std::vector()), - _typeDesc( constraint->shortID() ), + _typeDesc( Constraint::shortID(constraint) ), _constraint( constraint ), _allowMore(false) { @@ -293,7 +293,7 @@ MultiArg::MultiArg(const std::string& flag, Visitor* v) : Arg( flag, name, desc, req, true, v ), _values(std::vector()), - _typeDesc( constraint->shortID() ), + _typeDesc( Constraint::shortID(constraint) ), _constraint( constraint ), _allowMore(false) { diff --git a/include/tclap/ValueArg.h b/include/tclap/ValueArg.h index 7b1752b..e81e2d4 100644 --- a/include/tclap/ValueArg.h +++ b/include/tclap/ValueArg.h @@ -302,7 +302,7 @@ ValueArg::ValueArg(const std::string& flag, : Arg(flag, name, desc, req, true, v), _value( val ), _default( val ), - _typeDesc( constraint->shortID() ), + _typeDesc( Constraint::shortID(constraint) ), _constraint( constraint ) { } @@ -318,7 +318,7 @@ ValueArg::ValueArg(const std::string& flag, : Arg(flag, name, desc, req, true, v), _value( val ), _default( val ), - _typeDesc( constraint->shortID() ), // TODO(macbishop): Will crash + _typeDesc( Constraint::shortID(constraint) ), // TODO(macbishop): Will crash // if constraint is NULL _constraint( constraint ) { diff --git a/tests/runtests.sh b/tests/runtests.sh index 53d4a61..9aafe4f 100755 --- a/tests/runtests.sh +++ b/tests/runtests.sh @@ -6,7 +6,7 @@ cd $DIR let "suc = 0" let "fail = 0" -NUMTEST=87 +NUMTEST=89 for (( tno = 1 ; $tno <= $NUMTEST ; tno = $tno + 1 )); do ./testCheck.sh $tno diff --git a/tests/test89.out b/tests/test89.out new file mode 100644 index 0000000..863339f --- /dev/null +++ b/tests/test89.out @@ -0,0 +1 @@ +Passed diff --git a/tests/test89.sh b/tests/test89.sh new file mode 100644 index 0000000..68482cb --- /dev/null +++ b/tests/test89.sh @@ -0,0 +1,9 @@ +#!/bin/bash + +../examples/test28 > tmp.out 2>&1 + +if cmp -s tmp.out $srcdir/test89.out; then + exit 0 +else + exit 1 +fi