Feature Request #8 Implicit Conversion Operators for SwitchArg and ValueArg.

This commit is contained in:
Daniel Aarno 2015-05-11 21:41:56 +02:00
parent 12cee38782
commit b162f59fcb
6 changed files with 58 additions and 4 deletions

View File

@ -2,7 +2,7 @@
noinst_PROGRAMS = test1 test2 test3 test4 test5 test6 test7 test8 test9 \ noinst_PROGRAMS = test1 test2 test3 test4 test5 test6 test7 test8 test9 \
test10 test11 test12 test13 test14 test15 test16 \ test10 test11 test12 test13 test14 test15 test16 \
test17 test18 test19 test20 test21 test22 test23 test24 \ test17 test18 test19 test20 test21 test22 test23 test24 \
test25 test26 test25 test26 test27
test1_SOURCES = test1.cpp test1_SOURCES = test1.cpp
test2_SOURCES = test2.cpp test2_SOURCES = test2.cpp
@ -30,6 +30,7 @@ test23_SOURCES = test23.cpp
test24_SOURCES = test24.cpp test24_SOURCES = test24.cpp
test25_SOURCES = test25.cpp test25_SOURCES = test25.cpp
test26_SOURCES = test26.cpp test26_SOURCES = test26.cpp
test27_SOURCES = test27.cpp
AM_CPPFLAGS = -I$(top_srcdir)/include AM_CPPFLAGS = -I$(top_srcdir)/include

26
examples/test27.cpp Normal file
View File

@ -0,0 +1,26 @@
#include "tclap/CmdLine.h"
#include <iostream>
#include <string>
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<string> strArg("s","str", "test string arg", false, "defStr", "string", cmd);
ValueArg<int> 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;
}

View File

@ -112,7 +112,14 @@ class SwitchArg : public Arg
/** /**
* Returns bool, whether or not the switch has been set. * 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(); virtual void reset();

View File

@ -220,7 +220,13 @@ class ValueArg : public Arg
/** /**
* Returns the value of the argument. * 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. * Specialization of shortID.
@ -308,7 +314,8 @@ ValueArg<T>::ValueArg(const std::string& flag,
: Arg(flag, name, desc, req, true, v), : Arg(flag, name, desc, req, true, v),
_value( val ), _value( val ),
_default( val ), _default( val ),
_typeDesc( constraint->shortID() ), _typeDesc( constraint->shortID() ), // TODO(macbishop): Will crash
// if constraint is NULL
_constraint( constraint ) _constraint( constraint )
{ {
parser.add( this ); parser.add( this );

4
tests/test88.out Normal file
View File

@ -0,0 +1,4 @@
for falseSwitch we got : 0
for trueSwitch we got : 1
for strArg we got : defStr
for intArg we got : 4711

9
tests/test88.sh Executable file
View File

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