From 6018b0ba6886dd07bfe7e8cfacc17bdcc706ee7b Mon Sep 17 00:00:00 2001 From: mes5k Date: Mon, 22 Dec 2003 01:52:01 +0000 Subject: [PATCH] added delimiter --- examples/test3.cpp | 27 +++++++++++------------- src/Arg.cpp | 52 ++++++++++++++++++++++++++++++++++++---------- src/CmdLine.cpp | 18 ++++++++++------ src/SwitchArg.cpp | 2 +- 4 files changed, 66 insertions(+), 33 deletions(-) diff --git a/examples/test3.cpp b/examples/test3.cpp index 8476225..01d4a19 100644 --- a/examples/test3.cpp +++ b/examples/test3.cpp @@ -6,13 +6,10 @@ using namespace TCLAP; -int _intTest; -float _floatTest; -bool _boolTestA; bool _boolTestB; -bool _boolTestC; string _stringTest; string _utest; +string _ztest; void parseOptions(int argc, char** argv); @@ -21,13 +18,10 @@ int main(int argc, char** argv) parseOptions(argc,argv); - cout << "for float we got : " << _floatTest << endl - << "for int we got : " << _intTest<< endl - << "for string we got : " << _stringTest<< endl - << "for ulabeled we got : " << _utest << endl - << "for bool A we got : " << _boolTestA << endl - << "for bool B we got : " << _boolTestB << endl - << "for bool C we got : " << _boolTestC << endl; + cout << "for string we got : " << _stringTest<< endl + << "for ulabeled one we got : " << _utest << endl + << "for ulabeled two we got : " << _ztest << endl + << "for bool B we got : " << _boolTestB << endl; } @@ -36,7 +30,7 @@ void parseOptions(int argc, char** argv) { try { - CmdLine cmd(argv[0], "this is a message", "0.99" ); + CmdLine cmd("this is a message", '=', "0.99" ); // // Define arguments @@ -49,29 +43,32 @@ void parseOptions(int argc, char** argv) "string"); cmd.add( stest ); - UnlabeledValueArg utest("unTest","unlabeld test", + UnlabeledValueArg utest("unTest1","unlabeled test one", "default","string"); cmd.add( utest ); + UnlabeledValueArg ztest("unTest2","unlabeled test two", + "default","string"); + cmd.add( ztest ); + MultiArg itest("i", "intTest", "multi int test", false,"int" ); cmd.add( itest ); UnlabeledMultiArg mtest("fileName","file names","fileNameString"); cmd.add( mtest ); - // // Parse the command line. // cmd.parse(argc,argv); - // // Set variables // _stringTest = stest.getValue(); _boolTestB = btest.getValue(); _utest = utest.getValue(); + _ztest = ztest.getValue(); vector vi = itest.getValue(); for ( int i = 0; i < vi.size(); i++ ) diff --git a/src/Arg.cpp b/src/Arg.cpp index 191b0fe..64176f9 100644 --- a/src/Arg.cpp +++ b/src/Arg.cpp @@ -24,7 +24,9 @@ namespace TCLAP { +// defaults bool Arg::_ignoreRest = false; +char Arg::_delimiter = ' '; Arg::Arg( const string& flag, const string& name, @@ -32,14 +34,15 @@ Arg::Arg( const string& flag, bool req, bool valreq, Visitor* v) -: _name(name), - _description(desc), +: _flag(flag), + _name(name), + _description(desc), _required(req), _valueRequired(valreq), _alreadySet(false), - _ignoreable(true), - _visitor( v ) + _visitor( v ), + _ignoreable(true) { if ( _flag.length() > 1 ) throw(ArgException("Argument flag can only be one character long", @@ -47,18 +50,20 @@ Arg::Arg( const string& flag, }; Arg::Arg() -: _name(""), - _description(""), +: _flag(""), + _name(""), + _description(""), _required(false), _valueRequired(false), _alreadySet(false) { }; Arg::Arg(const Arg& a) -: _name(a._name), - _description(a._description), +: _flag(a._flag), + _name(a._name), + _description(a._description), _required(a._required), _valueRequired(a._valueRequired), _alreadySet(a._alreadySet) @@ -70,9 +75,9 @@ Arg& Arg::operator=(const Arg& a) { if ( this != &a ) { + _flag = a._flag; _name = a._name; _description = a._description; - _flag = a._flag; _required = a._required; _valueRequired = a._valueRequired; _alreadySet = a._alreadySet; @@ -85,9 +90,12 @@ string Arg::shortID( const string& valueId ) const string id = ""; id = "-" + _flag; + + string delim = " "; + delim[0] = Arg::_delimiter; // ugly!!! if ( _valueRequired ) - id += " <" + valueId + ">"; + id += delim + "<" + valueId + ">"; if ( !_required ) id = "[" + id + "]"; @@ -125,7 +133,7 @@ bool Arg::operator==(const Arg& a) } // should be overridden -bool Arg::processArg(int* i, vector& args ) +bool Arg::processArg(int* i, vector& args) { cerr << "WARNING: Ignoring unknown argument: " << args[*i] << endl; return false; @@ -173,4 +181,26 @@ void Arg::_checkWithVisitor() const _visitor->visit(); } +/** + * Implementation of trimFlag. + */ +void Arg::trimFlag(string& flag, string& value) const +{ + int stop = 0; + for ( int i = 0; (unsigned int)i < flag.length(); i++ ) + if ( flag[i] == Arg::_delimiter ) + { + stop = i; + break; + } + + if ( stop > 1 ) + { + value = flag.substr(stop+1); + flag = flag.substr(0,stop); + } + + cout << "flag: " << flag << endl << "value: " << value << endl; +} + } diff --git a/src/CmdLine.cpp b/src/CmdLine.cpp index 27b2e29..e6ae7b8 100644 --- a/src/CmdLine.cpp +++ b/src/CmdLine.cpp @@ -24,12 +24,15 @@ namespace TCLAP { -CmdLine::CmdLine(char *progName, const string& m, const string& v ) -: _progName(progName), +CmdLine::CmdLine(const string& m, char delim, const string& v ) +: _progName("not_set_yet"), _message(m), _version(v), - _numRequired(0) + _numRequired(0), + _delimiter(delim) { + Arg::setDelimiter( _delimiter ); + SwitchArg* help = new SwitchArg("h","help", "Displays usage information and exits.", false, new HelpVisitor( this ) ); @@ -92,6 +95,8 @@ void CmdLine::parse(int argc, char** argv) { try { + _progName = argv[0]; + // this step is necessary so that we have easy access to mutable strings. vector args; for (int i = 1; i < argc; i++) @@ -99,14 +104,15 @@ void CmdLine::parse(int argc, char** argv) int requiredCount = 0; - for (int i = 0; i < args.size(); i++) + for (int i = 0; (unsigned int)i < args.size(); i++) { bool matched = false; for (ArgIterator it = _argList.begin(); it != _argList.end(); it++) { if ( (*it)->processArg( &i, args ) ) { - if ( (*it)->isRequired() ) requiredCount++; + if ( (*it)->isRequired() ) + requiredCount++; matched = true; break; } @@ -124,7 +130,7 @@ void CmdLine::parse(int argc, char** argv) } catch ( ArgException e ) { - cerr << "PARSE ERROR: for argument: " << e.argId() << endl + cerr << "PARSE ERROR: " << e.argId() << endl << " " << e.error() << endl << endl; usage(1); diff --git a/src/SwitchArg.cpp b/src/SwitchArg.cpp index ace9c40..820099a 100644 --- a/src/SwitchArg.cpp +++ b/src/SwitchArg.cpp @@ -52,7 +52,7 @@ bool SwitchArg::combinedSwitchesMatch(string& combinedSwitches ) // ok, we're not specifying a ValueArg, so we know that we have // a combined switch list. - for ( int i = 1; i < combinedSwitches.length(); i++ ) + for ( unsigned int i = 1; i < combinedSwitches.length(); i++ ) if ( combinedSwitches[i] == _flag[0] ) { // update the combined switches so this one is no longer present