added a bool to the constructor that allows automatic -h and -v to be turned off

This commit is contained in:
mes5k 2005-01-24 00:26:41 +00:00
parent 2d34e9d1a1
commit 2f941785c3
3 changed files with 59 additions and 23 deletions

View File

@ -24,6 +24,7 @@
#define TCLAP_CMDLINE_H #define TCLAP_CMDLINE_H
#include <tclap/SwitchArg.h> #include <tclap/SwitchArg.h>
#include <tclap/MultiSwitchArg.h>
#include <tclap/UnlabeledValueArg.h> #include <tclap/UnlabeledValueArg.h>
#include <tclap/UnlabeledMultiArg.h> #include <tclap/UnlabeledMultiArg.h>
@ -145,6 +146,11 @@ class CmdLine : public CmdLineInterface
*/ */
bool _userSetOutput; bool _userSetOutput;
/**
* Whether or not to automatically create help and version switches.
*/
bool _helpAndVersion;
public: public:
/** /**
@ -170,10 +176,13 @@ class CmdLine : public CmdLineInterface
* the argument flag/name from the value. Defaults to ' ' (space). * the argument flag/name from the value. Defaults to ' ' (space).
* \param version - The version number to be used in the * \param version - The version number to be used in the
* --version switch. * --version switch.
* \param helpAndVersion - Whether or not to create the Help and
* Version switches. Defaults to true.
*/ */
CmdLine(const std::string& message, CmdLine(const std::string& message,
const char delimiter = ' ', const char delimiter = ' ',
const std::string& version = "none" ); const std::string& version = "none",
bool helpAndVersion = true);
/** /**
* Deletes any resources allocated by a CmdLine object. * Deletes any resources allocated by a CmdLine object.
@ -253,6 +262,11 @@ class CmdLine : public CmdLineInterface
* *
*/ */
std::string& getMessage(); std::string& getMessage();
/**
*
*/
bool hasHelpAndVersion();
}; };
@ -268,20 +282,23 @@ inline CmdLine::CmdLine(const std::string& n,
_version(v), _version(v),
_numRequired(0), _numRequired(0),
_delimiter(' '), _delimiter(' '),
_userSetOutput(false) _userSetOutput(false),
_helpAndVersion(true)
{ {
_constructor(); _constructor();
} }
inline CmdLine::CmdLine(const std::string& m, inline CmdLine::CmdLine(const std::string& m,
char delim, char delim,
const std::string& v ) const std::string& v,
bool help )
: _progName("not_set_yet"), : _progName("not_set_yet"),
_message(m), _message(m),
_version(v), _version(v),
_numRequired(0), _numRequired(0),
_delimiter(delim), _delimiter(delim),
_userSetOutput(false) _userSetOutput(false),
_helpAndVersion(help)
{ {
_constructor(); _constructor();
} }
@ -309,25 +326,28 @@ inline void CmdLine::_constructor()
{ {
_output = new StdOutput; _output = new StdOutput;
Visitor *v;
Arg::setDelimiter( _delimiter ); Arg::setDelimiter( _delimiter );
v = new HelpVisitor( this, &_output ); Visitor* v;
SwitchArg* help = new SwitchArg("h","help",
"Displays usage information and exits.", if ( _helpAndVersion )
false, v); {
add( help ); v = new HelpVisitor( this, &_output );
deleteOnExit(help); SwitchArg* help = new SwitchArg("h","help",
deleteOnExit(v); "Displays usage information and exits.",
false, v);
add( help );
deleteOnExit(help);
deleteOnExit(v);
v = new VersionVisitor( this, &_output ); v = new VersionVisitor( this, &_output );
SwitchArg* vers = new SwitchArg("v","version", SwitchArg* vers = new SwitchArg("v","version",
"Displays version information and exits.", "Displays version information and exits.",
false, v); false, v);
add( vers ); add( vers );
deleteOnExit(vers); deleteOnExit(vers);
deleteOnExit(v); deleteOnExit(v);
}
v = new IgnoreRestVisitor(); v = new IgnoreRestVisitor();
SwitchArg* ignore = new SwitchArg(Arg::flagStartString(), SwitchArg* ignore = new SwitchArg(Arg::flagStartString(),
@ -487,6 +507,11 @@ inline std::string& CmdLine::getMessage()
return _message; return _message;
} }
inline bool CmdLine::hasHelpAndVersion()
{
return _helpAndVersion;
}
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
//End CmdLine.cpp //End CmdLine.cpp
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////

View File

@ -123,6 +123,12 @@ class CmdLineInterface
* Returns the message string. * Returns the message string.
*/ */
virtual std::string& getMessage()=0; virtual std::string& getMessage()=0;
/**
* Indicates whether or not the help and version switches were created
* automatically.
*/
virtual bool hasHelpAndVersion()=0;
}; };
} //namespace } //namespace

View File

@ -135,13 +135,18 @@ inline void StdOutput::failure( CmdLineInterface& _cmd,
std::cerr << "PARSE ERROR: " << e.argId() << std::endl std::cerr << "PARSE ERROR: " << e.argId() << std::endl
<< " " << e.error() << std::endl << std::endl; << " " << e.error() << std::endl << std::endl;
std::cerr << "Brief USAGE: " << std::endl; if ( _cmd.hasHelpAndVersion() )
{
std::cerr << "Brief USAGE: " << std::endl;
_shortUsage( _cmd, std::cerr ); _shortUsage( _cmd, std::cerr );
std::cerr << std::endl << "For complete USAGE and HELP type: " std::cerr << std::endl << "For complete USAGE and HELP type: "
<< std::endl << " " << progName << " --help" << std::endl << " " << progName << " --help"
<< std::endl << std::endl; << std::endl << std::endl;
}
else
usage(_cmd);
} }