From ce60f03fd328e1045358e2b203bfa82e943ae5e6 Mon Sep 17 00:00:00 2001 From: mes5k Date: Fri, 3 Jun 2005 02:35:10 +0000 Subject: [PATCH] fix to handle optional unlabeled args --- include/tclap/Makefile.am | 1 + include/tclap/OptionalUnlabeledTracker.h | 62 ++++++++++++++++++++++++ include/tclap/UnlabeledMultiArg.h | 29 +++++++++-- include/tclap/UnlabeledValueArg.h | 9 ++++ 4 files changed, 97 insertions(+), 4 deletions(-) create mode 100644 include/tclap/OptionalUnlabeledTracker.h diff --git a/include/tclap/Makefile.am b/include/tclap/Makefile.am index 3640f58..a406942 100644 --- a/include/tclap/Makefile.am +++ b/include/tclap/Makefile.am @@ -19,6 +19,7 @@ libtclapinclude_HEADERS = \ CmdLineOutput.h \ StdOutput.h \ DocBookOutput.h \ + OptionalUnlabeledTracker.h \ Constraint.h \ ValuesConstraint.h diff --git a/include/tclap/OptionalUnlabeledTracker.h b/include/tclap/OptionalUnlabeledTracker.h new file mode 100644 index 0000000..c252a30 --- /dev/null +++ b/include/tclap/OptionalUnlabeledTracker.h @@ -0,0 +1,62 @@ + + +/****************************************************************************** + * + * file: OptionalUnlabeledTracker.h + * + * Copyright (c) 2005, Michael E. Smoot . + * All rights reverved. + * + * See the file COPYING in the top directory of this distribution for + * more information. + * + * THE SOFTWARE IS PROVIDED _AS IS_, WITHOUT WARRANTY OF ANY KIND, EXPRESS + * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER + * DEALINGS IN THE SOFTWARE. + * + *****************************************************************************/ + + +#ifndef TCLAP_OPTIONAL_UNLABELED_TRACKER_H +#define TCLAP_OPTIONAL_UNLABELED_TRACKER_H + +#include + +namespace TCLAP { + +class OptionalUnlabeledTracker +{ + + public: + + static void check( bool req, const std::string& argName ); + + static void gotOptional() { alreadyOptionalRef() = true; } + + static bool& alreadyOptional() { return alreadyOptionalRef(); } + + private: + + static bool& alreadyOptionalRef() { static bool ct = false; return ct; } +}; + + +void OptionalUnlabeledTracker::check( bool req, const std::string& argName ) +{ + if ( OptionalUnlabeledTracker::alreadyOptional() ) + throw( SpecificationException( + "You can't specify ANY Unlabeled Arg following an optional Unlabeled Arg", + argName ) ); + + if ( !req ) + OptionalUnlabeledTracker::gotOptional(); +} + + +} // namespace TCLAP + +#endif diff --git a/include/tclap/UnlabeledMultiArg.h b/include/tclap/UnlabeledMultiArg.h index fac250d..e717dfd 100644 --- a/include/tclap/UnlabeledMultiArg.h +++ b/include/tclap/UnlabeledMultiArg.h @@ -27,6 +27,7 @@ #include #include +#include namespace TCLAP { @@ -57,6 +58,8 @@ class UnlabeledMultiArg : public MultiArg * identification, not as a long flag. * \param desc - A description of what the argument is for or * does. + * \param req - Whether the argument is required on the command + * line. * \param typeDesc - A short, human readable description of the * type that this object expects. This is used in the generation * of the USAGE statement. The goal is to be helpful to the end user @@ -68,6 +71,7 @@ class UnlabeledMultiArg : public MultiArg */ UnlabeledMultiArg( const std::string& name, const std::string& desc, + bool req, const std::string& typeDesc, bool ignoreable = false, Visitor* v = NULL ); @@ -77,6 +81,8 @@ class UnlabeledMultiArg : public MultiArg * identification, not as a long flag. * \param desc - A description of what the argument is for or * does. + * \param req - Whether the argument is required on the command + * line. * \param typeDesc - A short, human readable description of the * type that this object expects. This is used in the generation * of the USAGE statement. The goal is to be helpful to the end user @@ -89,6 +95,7 @@ class UnlabeledMultiArg : public MultiArg */ UnlabeledMultiArg( const std::string& name, const std::string& desc, + bool req, const std::string& typeDesc, CmdLineInterface& parser, bool ignoreable = false, @@ -100,6 +107,8 @@ class UnlabeledMultiArg : public MultiArg * identification, not as a long flag. * \param desc - A description of what the argument is for or * does. + * \param req - Whether the argument is required on the command + * line. * \param constraint - A pointer to a Constraint object used * to constrain this Arg. * \param ignoreable - Whether or not this argument can be ignored @@ -109,6 +118,7 @@ class UnlabeledMultiArg : public MultiArg */ UnlabeledMultiArg( const std::string& name, const std::string& desc, + bool req, Constraint* constraint, bool ignoreable = false, Visitor* v = NULL ); @@ -119,6 +129,8 @@ class UnlabeledMultiArg : public MultiArg * identification, not as a long flag. * \param desc - A description of what the argument is for or * does. + * \param req - Whether the argument is required on the command + * line. * \param constraint - A pointer to a Constraint object used * to constrain this Arg. * \param parser - A CmdLine parser object to add this Arg to @@ -129,6 +141,7 @@ class UnlabeledMultiArg : public MultiArg */ UnlabeledMultiArg( const std::string& name, const std::string& desc, + bool req, Constraint* constraint, CmdLineInterface& parser, bool ignoreable = false, @@ -172,24 +185,28 @@ class UnlabeledMultiArg : public MultiArg template UnlabeledMultiArg::UnlabeledMultiArg(const std::string& name, const std::string& desc, + bool req, const std::string& typeDesc, bool ignoreable, Visitor* v) -: MultiArg("", name, desc, false, typeDesc, v) +: MultiArg("", name, desc, req, typeDesc, v) { _ignoreable = ignoreable; + OptionalUnlabeledTracker::check(true, toString()); } template UnlabeledMultiArg::UnlabeledMultiArg(const std::string& name, const std::string& desc, + bool req, const std::string& typeDesc, CmdLineInterface& parser, bool ignoreable, Visitor* v) -: MultiArg("", name, desc, false, typeDesc, v) +: MultiArg("", name, desc, req, typeDesc, v) { _ignoreable = ignoreable; + OptionalUnlabeledTracker::check(true, toString()); parser.add( this ); } @@ -197,24 +214,28 @@ UnlabeledMultiArg::UnlabeledMultiArg(const std::string& name, template UnlabeledMultiArg::UnlabeledMultiArg(const std::string& name, const std::string& desc, + bool req, Constraint* constraint, bool ignoreable, Visitor* v) -: MultiArg("", name, desc, false, constraint, v) +: MultiArg("", name, desc, req, constraint, v) { _ignoreable = ignoreable; + OptionalUnlabeledTracker::check(true, toString()); } template UnlabeledMultiArg::UnlabeledMultiArg(const std::string& name, const std::string& desc, + bool req, Constraint* constraint, CmdLineInterface& parser, bool ignoreable, Visitor* v) -: MultiArg("", name, desc, false, constraint, v) +: MultiArg("", name, desc, req, constraint, v) { _ignoreable = ignoreable; + OptionalUnlabeledTracker::check(true, toString()); parser.add( this ); } diff --git a/include/tclap/UnlabeledValueArg.h b/include/tclap/UnlabeledValueArg.h index e32b90a..19e586f 100644 --- a/include/tclap/UnlabeledValueArg.h +++ b/include/tclap/UnlabeledValueArg.h @@ -28,6 +28,8 @@ #include #include +#include + namespace TCLAP { @@ -200,6 +202,7 @@ class UnlabeledValueArg : public ValueArg * \param argList - The list to add this to. */ virtual void addToList( std::list& argList ) const; + }; /** @@ -216,6 +219,9 @@ UnlabeledValueArg::UnlabeledValueArg(const std::string& name, : ValueArg("", name, desc, req, val, typeDesc, v) { _ignoreable = ignoreable; + + OptionalUnlabeledTracker::check(req, toString()); + } template @@ -230,6 +236,7 @@ UnlabeledValueArg::UnlabeledValueArg(const std::string& name, : ValueArg("", name, desc, req, val, typeDesc, v) { _ignoreable = ignoreable; + OptionalUnlabeledTracker::check(req, toString()); parser.add( this ); } @@ -247,6 +254,7 @@ UnlabeledValueArg::UnlabeledValueArg(const std::string& name, : ValueArg("", name, desc, req, val, constraint, v) { _ignoreable = ignoreable; + OptionalUnlabeledTracker::check(req, toString()); } template @@ -261,6 +269,7 @@ UnlabeledValueArg::UnlabeledValueArg(const std::string& name, : ValueArg("", name, desc, req, val, constraint, v) { _ignoreable = ignoreable; + OptionalUnlabeledTracker::check(req, toString()); parser.add( this ); }