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 gnsstk::CommandOptionParser. If you want to manage your own list, pass in your own std::vector<gnsstk::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 gnsstk::CommandOptionParser, so if you change anything here, make sure you don't side affect the other.
For additional details, see Application Building.
Definition at line 115 of file CommandOption.hpp.
#include <CommandOption.hpp>
Public Types | |
enum | CommandOptionFlag { noArgument = 0, hasArgument = 1 } |
enum | CommandOptionType { trailingType, stdType, metaType } |
Public Member Functions | |
virtual std::string | checkArguments () |
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) | |
std::ostream & | dumpValue (std::ostream &out) const |
Displays this->value to the stream out . More... | |
virtual std::string | getArgString () const |
Returns a string with the argument format. More... | |
virtual unsigned long | getCount () const |
std::string | getDescription () const |
Returns a formatted string with the description of this option. More... | |
std::string | getFullOptionString () const |
virtual std::string | getOptionString () const |
unsigned long | getOrder (unsigned long idx=-1) const |
const std::vector< std::string > & | getValue () const |
void | setDescription (const std::string &desc) |
CommandOption & | setMaxCount (const unsigned long l) |
struct option | toGetoptLongOption () const |
Returns a struct option for use with getopt_long. More... | |
std::string | toGetoptShortOption () const |
Returns a string for use with getopt. More... | |
virtual | ~CommandOption () |
Destructor. More... | |
Protected Member Functions | |
CommandOption () | |
Default Constructor. More... | |
Protected Attributes | |
unsigned long | count |
std::string | description |
The description for the help text. More... | |
std::string | longOpt |
The string for the long option (for example, "--foo"). More... | |
unsigned long | maxCount |
CommandOptionFlag | optFlag |
Flag for determining whether this option has an argument or not. More... | |
CommandOptionType | optType |
std::vector< unsigned long > | order |
The order in which this option was encountered on the command line. More... | |
CommandOptionParser * | parser |
bool | required |
Whether or not this is a required command line option. More... | |
char | shortOpt |
The character for the short option (for example, '-f'). More... | |
std::vector< std::string > | value |
Any arguments passed with this option get put in here. More... | |
Friends | |
class | CommandOptionParser |
lets the CommandOptionParser see its private parts =) More... | |
Every option must either have or not have an argument. There are no optional arguments because Solaris doesn't support it.
Enumerator | |
---|---|
noArgument | option requires no arguments |
hasArgument | option requires an argument |
Definition at line 123 of file CommandOption.hpp.
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.
Enumerator | |
---|---|
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 132 of file CommandOption.hpp.
gnsstk::CommandOption::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.
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 57 of file CommandOption.cpp.
|
inlinevirtual |
Destructor.
Definition at line 235 of file CommandOption.hpp.
|
inlineprotected |
Default Constructor.
Definition at line 266 of file CommandOption.hpp.
|
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.
Reimplemented in gnsstk::CommandOptionGroupOr, gnsstk::CommandOptionDependent, gnsstk::CommandOptionMutex, gnsstk::CommandOptionAllOf, gnsstk::CommandOptionOneOf, gnsstk::CommandOptionNOf, gnsstk::CommandOptionRest, gnsstk::CommandOptionWithDecimalArg, gnsstk::CommandOptionWithNumberArg, gnsstk::CommandOptionWithStringArg, gnsstk::CommandOptionWithPositionArg, gnsstk::CommandOptionWithCommonTimeArg, and gnsstk::CommandOptionWithTimeArg.
Definition at line 225 of file CommandOption.cpp.
std::ostream & gnsstk::CommandOption::dumpValue | ( | std::ostream & | out | ) | const |
Displays this->value to the stream out
.
Definition at line 173 of file CommandOption.cpp.
|
inlinevirtual |
Returns a string with the argument format.
Reimplemented in gnsstk::CommandOptionWithDecimalArg, gnsstk::CommandOptionWithNumberArg, gnsstk::CommandOptionWithStringArg, gnsstk::CommandOptionWithPositionArg, gnsstk::CommandOptionWithCommonTimeArg, and gnsstk::CommandOptionWithTimeArg.
Definition at line 176 of file CommandOption.hpp.
|
inlinevirtual |
Returns the number of times this option was found on the command line.
Reimplemented in gnsstk::CommandOptionGroupAnd, gnsstk::CommandOptionGroupOr, and gnsstk::CommandOptionAllOf.
Definition at line 188 of file CommandOption.hpp.
std::string gnsstk::CommandOption::getDescription | ( | ) | const |
Returns a formatted string with the description of this option.
Definition at line 189 of file CommandOption.cpp.
string gnsstk::CommandOption::getFullOptionString | ( | ) | const |
Returns a formatted string with the flags for this CommandOption. (i.e. " -f, --foo=ARG")
Definition at line 115 of file CommandOption.cpp.
|
virtual |
Returns a string with the flags for this CommandOption. (i.e. "-f | --foo")
Reimplemented in gnsstk::CommandOptionGroupOr.
Definition at line 97 of file CommandOption.cpp.
unsigned long gnsstk::CommandOption::getOrder | ( | unsigned long | idx = -1 | ) | const |
Returns the order which this command option was seen on the command line, with 1 being the first option. If it can be repeated and idx is unspecified, this order represents the order of the last occurance of this option.
idx | the index of the instance of this command option to get the order of. This is used when the command option can appear multiple times, and idx is the same index as in the vector of values. The first instance of this command option will be index 0, and so on. If idx is invalid (i.e. it does not exist in the vector), a zero is returned. |
Definition at line 158 of file CommandOption.cpp.
|
inline |
Returns the arguments this option had passed in from the command line.
Definition at line 194 of file CommandOption.hpp.
|
inline |
Set the command-line option's description after the constructor. This differs in behavior from getDescription() in that getDescription() combines all of the long/short option, the actual description, and so on, where this only changes the description itself.
[in] | desc | The new option description/documentation. |
Definition at line 223 of file CommandOption.hpp.
|
inline |
Sets the maximum number of times this should appear on the command line.
Definition at line 164 of file CommandOption.hpp.
struct option gnsstk::CommandOption::toGetoptLongOption | ( | ) | const |
Returns a struct option for use with getopt_long.
Definition at line 143 of file CommandOption.cpp.
std::string gnsstk::CommandOption::toGetoptShortOption | ( | ) | const |
Returns a string for use with getopt.
Definition at line 150 of file CommandOption.cpp.
|
friend |
lets the CommandOptionParser see its private parts =)
Definition at line 119 of file CommandOption.hpp.
|
protected |
The number of times this option was encountered on the command line.
Definition at line 255 of file CommandOption.hpp.
|
protected |
The description for the help text.
Definition at line 248 of file CommandOption.hpp.
|
protected |
The string for the long option (for example, "--foo").
Definition at line 246 of file CommandOption.hpp.
|
protected |
The maximum number of times this can appear on the command line. If it's 0, then it's unlimited.
Definition at line 258 of file CommandOption.hpp.
|
protected |
Flag for determining whether this option has an argument or not.
Definition at line 239 of file CommandOption.hpp.
|
protected |
Flag for determining whether this option has a specific argument type.
Definition at line 242 of file CommandOption.hpp.
|
protected |
The order in which this option was encountered on the command line.
Definition at line 260 of file CommandOption.hpp.
|
protected |
CommandOptionParser used to parse this CommandOption. Set in CommandOptionParser::parseOptions.
Definition at line 263 of file CommandOption.hpp.
|
protected |
Whether or not this is a required command line option.
Definition at line 252 of file CommandOption.hpp.
|
protected |
The character for the short option (for example, '-f').
Definition at line 244 of file CommandOption.hpp.
|
protected |
Any arguments passed with this option get put in here.
Definition at line 250 of file CommandOption.hpp.