diff --git a/examples/Makefile.am b/examples/Makefile.am index ecd7cd2..9a8bae4 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 + test25 test26 test27 test1_SOURCES = test1.cpp test2_SOURCES = test2.cpp @@ -30,6 +30,7 @@ test23_SOURCES = test23.cpp test24_SOURCES = test24.cpp test25_SOURCES = test25.cpp test26_SOURCES = test26.cpp +test27_SOURCES = test27.cpp AM_CPPFLAGS = -I$(top_srcdir)/include diff --git a/examples/test27.cpp b/examples/test27.cpp new file mode 100644 index 0000000..74adbaa --- /dev/null +++ b/examples/test27.cpp @@ -0,0 +1,26 @@ +#include "tclap/CmdLine.h" +#include +#include + +using namespace TCLAP; +using namespace std; + +int main(int argc, char** argv) +{ + + CmdLine cmd("test arg conversion operator"); + SwitchArg falseSwitch("f","false", "test false condition", cmd, false); + SwitchArg trueSwitch("t","true", "tests true condition", cmd, true); + ValueArg strArg("s","str", "test string arg", false, "defStr", "string", cmd); + ValueArg intArg("i","int", "tests int arg", false, 4711, "integer", cmd); + + cmd.parse(argc, argv); + + string s = strArg; + int i = intArg; + + cout << "for falseSwitch we got : " << falseSwitch << endl + << "for trueSwitch we got : " << trueSwitch << endl + << "for strArg we got : " << s << endl + << "for intArg we got : " << i << endl; +} diff --git a/include/tclap/SwitchArg.h b/include/tclap/SwitchArg.h index 3916109..7f587da 100644 --- a/include/tclap/SwitchArg.h +++ b/include/tclap/SwitchArg.h @@ -112,7 +112,14 @@ class SwitchArg : public Arg /** * Returns bool, whether or not the switch has been set. */ - bool getValue(); + bool getValue() /* TODO(macbishop) should be const */; + + /** + * A SwitchArg can be used as a boolean, indicating + * whether or not the switch has been set. This is the + * same as calling getValue() + */ + operator bool() const { return _value; } virtual void reset(); diff --git a/include/tclap/ValueArg.h b/include/tclap/ValueArg.h index 7ac2952..be2e734 100644 --- a/include/tclap/ValueArg.h +++ b/include/tclap/ValueArg.h @@ -220,7 +220,13 @@ class ValueArg : public Arg /** * Returns the value of the argument. */ - T& getValue() ; + /* const */ T& getValue() /* TODO(macbishop): should be const */; + + /** + * A ValueArg can be used as as its value type (T) This is the + * same as calling getValue() + */ + operator const T&() const { return _value; } /** * Specialization of shortID. @@ -308,7 +314,8 @@ ValueArg::ValueArg(const std::string& flag, : Arg(flag, name, desc, req, true, v), _value( val ), _default( val ), - _typeDesc( constraint->shortID() ), + _typeDesc( constraint->shortID() ), // TODO(macbishop): Will crash + // if constraint is NULL _constraint( constraint ) { parser.add( this ); diff --git a/tests/test88.out b/tests/test88.out new file mode 100644 index 0000000..94bf86e --- /dev/null +++ b/tests/test88.out @@ -0,0 +1,4 @@ +for falseSwitch we got : 0 +for trueSwitch we got : 1 +for strArg we got : defStr +for intArg we got : 4711 diff --git a/tests/test88.sh b/tests/test88.sh new file mode 100755 index 0000000..6a44410 --- /dev/null +++ b/tests/test88.sh @@ -0,0 +1,9 @@ +#!/bin/bash + +../examples/test27 -v "1 2 3" > tmp.out 2>&1 + +if cmp -s tmp.out $srcdir/test88.out; then + exit 0 +else + exit 1 +fi