Public Types | Public Member Functions | Protected Member Functions | Protected Attributes | Friends | List of all members
gnsstk::CommandOption Class Reference

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 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.

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 115 of file CommandOption.hpp.

#include <CommandOption.hpp>

Inheritance diagram for gnsstk::CommandOption:
Inheritance graph
[legend]

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)
 
CommandOptionsetMaxCount (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...
 
CommandOptionParserparser
 
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...
 

Member Enumeration Documentation

◆ CommandOptionFlag

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.

◆ 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.

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.

Constructor & Destructor Documentation

◆ CommandOption() [1/2]

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.

Parameters
ofWhether or not this command requires an argument
otThe type of option (string, number, any, etc.)
shOptThe one character command line option. Set to 0 if unused.
loOptThe long command option. Set to std::string() if unused.
descA string describing what this option does.
reqSet to true if this is a required option.
optVectorListUse 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.

◆ ~CommandOption()

virtual gnsstk::CommandOption::~CommandOption ( )
inlinevirtual

Destructor.

Definition at line 235 of file CommandOption.hpp.

◆ CommandOption() [2/2]

gnsstk::CommandOption::CommandOption ( )
inlineprotected

Default Constructor.

Definition at line 266 of file CommandOption.hpp.

Member Function Documentation

◆ checkArguments()

string gnsstk::CommandOption::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.

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.

◆ dumpValue()

std::ostream & gnsstk::CommandOption::dumpValue ( std::ostream &  out) const

Displays this->value to the stream out.

Definition at line 173 of file CommandOption.cpp.

◆ getArgString()

virtual std::string gnsstk::CommandOption::getArgString ( ) const
inlinevirtual

◆ getCount()

virtual unsigned long gnsstk::CommandOption::getCount ( ) const
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.

◆ getDescription()

std::string gnsstk::CommandOption::getDescription ( ) const

Returns a formatted string with the description of this option.

Definition at line 189 of file CommandOption.cpp.

◆ getFullOptionString()

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.

◆ getOptionString()

string gnsstk::CommandOption::getOptionString ( ) const
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.

◆ getOrder()

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.

Parameters
idxthe 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.

◆ getValue()

const std::vector<std::string>& gnsstk::CommandOption::getValue ( ) const
inline

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

Definition at line 194 of file CommandOption.hpp.

◆ setDescription()

void gnsstk::CommandOption::setDescription ( const std::string &  desc)
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.

Parameters
[in]descThe new option description/documentation.

Definition at line 223 of file CommandOption.hpp.

◆ setMaxCount()

CommandOption& gnsstk::CommandOption::setMaxCount ( const unsigned long  l)
inline

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

Definition at line 164 of file CommandOption.hpp.

◆ toGetoptLongOption()

struct option gnsstk::CommandOption::toGetoptLongOption ( ) const

Returns a struct option for use with getopt_long.

Definition at line 143 of file CommandOption.cpp.

◆ toGetoptShortOption()

std::string gnsstk::CommandOption::toGetoptShortOption ( ) const

Returns a string for use with getopt.

Definition at line 150 of file CommandOption.cpp.

Friends And Related Function Documentation

◆ CommandOptionParser

friend class CommandOptionParser
friend

lets the CommandOptionParser see its private parts =)

Definition at line 119 of file CommandOption.hpp.

Member Data Documentation

◆ count

unsigned long gnsstk::CommandOption::count
protected

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

Definition at line 255 of file CommandOption.hpp.

◆ description

std::string gnsstk::CommandOption::description
protected

The description for the help text.

Definition at line 248 of file CommandOption.hpp.

◆ longOpt

std::string gnsstk::CommandOption::longOpt
protected

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

Definition at line 246 of file CommandOption.hpp.

◆ maxCount

unsigned long gnsstk::CommandOption::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 258 of file CommandOption.hpp.

◆ optFlag

CommandOptionFlag gnsstk::CommandOption::optFlag
protected

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

Definition at line 239 of file CommandOption.hpp.

◆ optType

CommandOptionType gnsstk::CommandOption::optType
protected

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

Definition at line 242 of file CommandOption.hpp.

◆ order

std::vector<unsigned long> gnsstk::CommandOption::order
protected

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

Definition at line 260 of file CommandOption.hpp.

◆ parser

CommandOptionParser* gnsstk::CommandOption::parser
protected

CommandOptionParser used to parse this CommandOption. Set in CommandOptionParser::parseOptions.

Definition at line 263 of file CommandOption.hpp.

◆ required

bool gnsstk::CommandOption::required
protected

Whether or not this is a required command line option.

Definition at line 252 of file CommandOption.hpp.

◆ shortOpt

char gnsstk::CommandOption::shortOpt
protected

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

Definition at line 244 of file CommandOption.hpp.

◆ value

std::vector<std::string> gnsstk::CommandOption::value
protected

Any arguments passed with this option get put in here.

Definition at line 250 of file CommandOption.hpp.


The documentation for this class was generated from the following files:


gnsstk
Author(s):
autogenerated on Wed Oct 25 2023 02:40:44