CommandOption Class Reference
[Command-Line Options]

#include <CommandOption.hpp>

Inheritance diagram for CommandOption:

Inheritance graph
[legend]
List of all members.

Detailed Description

This class is part of a replacement for getopt.

Each CommandOption represents an option you would enter at a command line. You can specify whether the option does or does not require an argument, if the option is of a certain type (string or number), then the short option (i.e. '-f') and long option ('--foo'), whether it's a required option or not, then a short description for a help display. By default, options can appear an unlimited number of times on a command line. Use setMaxCount() to set a maximum limit to this. In that case, additional appearances of that option will trigger an error once parsed.

When a CommandOption is created (not using the default constructor), it automatically adds itself to a list that will be used by gpstk::CommandOptionParser. If you want to manage your own list, pass in your own std::vector<gpstk::CommandOptionParser> with the CommandOption constructor. After parsing the command line, you can use the getValue() and getCount() methods to see what arguments the options had and how many times the option was listed on the command line.

This class is strongly connected to gpstk::CommandOptionParser, so if you change anything here, make sure you don't side affect the other.

See also:
getopttest.cpp in the test directory for some examples.
Warning:
DO NOT USE THE DEFAULT CONSTRUCTOR FOR THIS CLASS. That's for the STL use requirements only.

Do not repeat characters or strings used in the short or long command options. Doing so will cause one of them not to work.

Do not reuse the CommandOption objects. Make a separate one for each option you want on the command line.

Make the description an understandable, grammatically correct sentence.

The resulting behavior of not heeding the above advice is undefined, and I take no responsibility for the results of you not taking appropriate action in light of this warning.

Definition at line 120 of file CommandOption.hpp.

Public Types

enum  CommandOptionFlag { noArgument = 0, hasArgument = 1 }
 Every option must either have or not have an argument. More...
enum  CommandOptionType { trailingType, stdType, metaType }
 This is so you can limit what type or argument an option can have. More...

Public Member Functions

 CommandOption (const CommandOptionFlag of, const CommandOptionType ot, const char shOpt, const std::string &loOpt, const std::string &desc, const bool req=false, CommandOptionVec &optVectorList=defaultCommandOptionList)
 Constructor.
CommandOptionsetMaxCount (const unsigned long l)
 Sets the maximum number of times this should appear on the command line.
virtual std::string getOptionString () const
 Returns a string with the flags for this CommandOption.
std::string getFullOptionString () const
 Returns a formatted string with the flags for this CommandOption.
virtual std::string getArgString () const
 Returns a string with the argument format.
option toGetoptLongOption () const
 Returns a struct option for use with getopt_long.
std::string toGetoptShortOption () const
 Returns a string for use with getopt.
virtual unsigned long getCount () const
 Returns the number of times this option was found on the command line.
std::vector< std::string > getValue () const
 Returns the arguments this option had passed in from the command line.
unsigned int getOrder () const
 the last occurance of this option.
std::ostream & dumpValue (std::ostream &out) const
 Displays this->value to the stream out.
std::string getDescription () const
 Returns a formatted string with the description of this option.
virtual std::string checkArguments ()
 If you specified a format for the arguments (for example, digit or string), this function checks them to see if they match.
virtual ~CommandOption ()
 Destructor.

Protected Member Functions

 CommandOption ()
 Default Constructor.

Protected Attributes

CommandOptionFlag optFlag
 Flag for determining whether this option has an argument or not.
CommandOptionType optType
 Flag for determining whether this option has a specific argument type.
char shortOpt
 The character for the short option (for example, '-f').
std::string longOpt
 The string for the long option (for example, "--foo").
std::string description
 The description for the help text.
std::vector< std::string > value
 Any arguments passed with this option get put in here.
bool required
 Whether or not this is a required command line option.
unsigned long count
 The number of times this option was encountered on the command line.
unsigned long maxCount
 The maximum number of times this can appear on the command line.
unsigned long order
 The order in which this option was encountered on the command line.

Friends

class CommandOptionParser
 let's the CommandOptionParser see it's private parts =)


Member Enumeration Documentation

enum CommandOptionFlag
 

Every option must either have or not have an argument.

There are no optional arguments because Solaris doesn't support it.

Enumeration values:
noArgument  option requires no arguments
hasArgument  option requires an argument

Definition at line 128 of file CommandOption.hpp.

enum CommandOptionType
 

This is so you can limit what type or argument an option can have.

If specified, it will be checked when the argument is encountered. Errors will get set appropriately if there are any.

Enumeration values:
trailingType  Special case, no option, just the remaining args.
stdType  The argument of this option can be any type.
metaType  A meta-option that has its own special validation.

Definition at line 137 of file CommandOption.hpp.


Constructor & Destructor Documentation

CommandOption const CommandOptionFlag  of,
const CommandOptionType  ot,
const char  shOpt,
const std::string &  loOpt,
const std::string &  desc,
const bool  req = false,
CommandOptionVec optVectorList = defaultCommandOptionList
[inline]
 

Constructor.

Parameters:
of Whether or not this command requires an argument
ot The type of option (string, number, any, etc.)
shOpt The one character command line option. Set to 0 if unused.
loOpt The long command option. Set to std::string() if unused.
desc A string describing what this option does.
req Set to true if this is a required option.
optVectorList Use this to create your own command option list if you want to use an alternate method of parsing the command options.

Definition at line 158 of file CommandOption.hpp.

virtual ~CommandOption  )  [inline, virtual]
 

Destructor.

Definition at line 228 of file CommandOption.hpp.

CommandOption  )  [inline, protected]
 

Default Constructor.

Definition at line 256 of file CommandOption.hpp.


Member Function Documentation

string checkArguments  )  [virtual]
 

If you specified a format for the arguments (for example, digit or string), this function checks them to see if they match.

If they don't, an error string is returned. If they do, an empty string is returned.

Parameters:
optVec complete set of processed command line options being processed (used by some option types).

Reimplemented in CommandOptionWithStringArg, CommandOptionWithNumberArg, CommandOptionRest, CommandOptionOneOf, CommandOptionAllOf, CommandOptionMutex, CommandOptionDependent, CommandOptionGroupOr, CommandOptionWithCommonTimeArg, CommandOptionWithPositionArg, and CommandOptionWithTimeArg.

Definition at line 173 of file CommandOption.cpp.

References CommandOption::count, CommandOption::getOptionString(), and CommandOption::required.

Referenced by CommandOptionParser::parseOptions().

std::ostream & dumpValue std::ostream &  out  )  const
 

Displays this->value to the stream out.

Definition at line 121 of file CommandOption.cpp.

References CommandOption::value.

virtual std::string getArgString  )  const [inline, virtual]
 

Returns a string with the argument format.

Reimplemented in CommandOptionWithStringArg, CommandOptionWithNumberArg, CommandOptionWithCommonTimeArg, CommandOptionWithPositionArg, and CommandOptionWithTimeArg.

Definition at line 185 of file CommandOption.hpp.

Referenced by CommandOption::getDescription(), and CommandOption::getFullOptionString().

virtual unsigned long getCount  )  const [inline, virtual]
 

Returns the number of times this option was found on the command line.

Reimplemented in CommandOptionAllOf, CommandOptionGroupOr, and CommandOptionGroupAnd.

Definition at line 197 of file CommandOption.hpp.

Referenced by CommandOptionDependent::checkArguments(), CommandOptionMutex::checkArguments(), CommandOptionGroupAnd::getCount(), CommandOptionGroupOr::getCount(), CommandOptionAllOf::getCount(), ObsEphReaderFramework::initialize(), InOutFramework::initialize(), and main().

std::string getDescription  )  const
 

Returns a formatted string with the description of this option.

Definition at line 137 of file CommandOption.cpp.

References CommandOption::description, CommandOption::getArgString(), CommandOption::longOpt, CommandOption::maxCount, CommandOption::optFlag, gpstk::StringUtils::prettyPrint(), and CommandOption::shortOpt.

string getFullOptionString  )  const
 

Returns a formatted string with the flags for this CommandOption.

(i.e. " -f, --foo=ARG")

Definition at line 83 of file CommandOption.cpp.

References CommandOption::getArgString(), CommandOption::longOpt, CommandOption::optFlag, and CommandOption::shortOpt.

string getOptionString  )  const [virtual]
 

Returns a string with the flags for this CommandOption.

(i.e. "-f | --foo")

Reimplemented in CommandOptionGroupOr.

Definition at line 65 of file CommandOption.cpp.

References CommandOption::longOpt, and CommandOption::shortOpt.

Referenced by CommandOptionDependent::checkArguments(), CommandOptionAllOf::checkArguments(), CommandOptionOneOf::checkArguments(), CommandOptionMutex::checkArguments(), CommandOptionWithStringArg::checkArguments(), CommandOptionWithNumberArg::checkArguments(), CommandOption::checkArguments(), CommandOptionGroupOr::getOptionString(), and CommandOptionParser::parseOptions().

unsigned int getOrder  )  const [inline]
 

the last occurance of this option.

Definition at line 209 of file CommandOption.hpp.

std::vector<std::string> getValue  )  const [inline]
 

Returns the arguments this option had passed in from the command line.

Definition at line 203 of file CommandOption.hpp.

Referenced by ObsEphReaderFramework::initialize(), InOutFramework::initialize(), main(), and MinSfTest::process().

CommandOption& setMaxCount const unsigned long  l  )  [inline]
 

Sets the maximum number of times this should appear on the command line.

Definition at line 173 of file CommandOption.hpp.

Referenced by ObsEphReaderFramework::initialize().

struct option toGetoptLongOption  )  const
 

Returns a struct option for use with getopt_long.

Definition at line 106 of file CommandOption.cpp.

Referenced by CommandOptionParser::parseOptions().

std::string toGetoptShortOption  )  const
 

Returns a string for use with getopt.

Definition at line 113 of file CommandOption.cpp.

References CommandOption::optFlag, and CommandOption::shortOpt.

Referenced by CommandOptionParser::parseOptions().


Friends And Related Function Documentation

friend class CommandOptionParser [friend]
 

let's the CommandOptionParser see it's private parts =)

Definition at line 124 of file CommandOption.hpp.


Member Data Documentation

unsigned long count [protected]
 

The number of times this option was encountered on the command line.

Definition at line 248 of file CommandOption.hpp.

Referenced by CommandOption::checkArguments(), and CommandOptionParser::parseOptions().

std::string description [protected]
 

The description for the help text.

Definition at line 241 of file CommandOption.hpp.

Referenced by CommandOptionParser::displayUsage(), and CommandOption::getDescription().

std::string longOpt [protected]
 

The string for the long option (for example, "--foo").

Definition at line 239 of file CommandOption.hpp.

Referenced by CommandOption::getDescription(), CommandOption::getFullOptionString(), and CommandOption::getOptionString().

unsigned long maxCount [protected]
 

The maximum number of times this can appear on the command line.

If it's 0, then it's unlimited.

Definition at line 251 of file CommandOption.hpp.

Referenced by CommandOption::getDescription().

CommandOptionFlag optFlag [protected]
 

Flag for determining whether this option has an argument or not.

Definition at line 232 of file CommandOption.hpp.

Referenced by CommandOption::getDescription(), CommandOption::getFullOptionString(), CommandOptionParser::parseOptions(), and CommandOption::toGetoptShortOption().

CommandOptionType optType [protected]
 

Flag for determining whether this option has a specific argument type.

Definition at line 235 of file CommandOption.hpp.

unsigned long order [protected]
 

The order in which this option was encountered on the command line.

Definition at line 253 of file CommandOption.hpp.

Referenced by CommandOptionParser::parseOptions().

bool required [protected]
 

Whether or not this is a required command line option.

Definition at line 245 of file CommandOption.hpp.

Referenced by CommandOption::checkArguments().

char shortOpt [protected]
 

The character for the short option (for example, '-f').

Definition at line 237 of file CommandOption.hpp.

Referenced by CommandOption::getDescription(), CommandOption::getFullOptionString(), CommandOption::getOptionString(), and CommandOption::toGetoptShortOption().

std::vector<std::string> value [protected]
 

Any arguments passed with this option get put in here.

Definition at line 243 of file CommandOption.hpp.

Referenced by CommandOption::dumpValue(), and CommandOptionParser::parseOptions().


The documentation for this class was generated from the following files:
Generated on Fri May 24 03:31:46 2013 for GPS ToolKit Software Library by  doxygen 1.3.9.1