Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
ROOT::option::Option Class Reference

A parsed option from the command line together with its argument if it has one.

The Parser chains all parsed options with the same Descriptor::index together to form a linked list. This allows you to easily implement all of the common ways of handling repeated options and enable/disable pairs.

  • Test for presence of a switch in the argument vector:
    if ( options[QUIET] ) ...
  • Evaluate –enable-foo/–disable-foo pair where the last one used wins:
    if ( options[FOO].last()->type() == DISABLE ) ...
    int type() const
    Returns Descriptor::type of this Option's Descriptor, or 0 if this Option is invalid (unused).
    Option * last()
    Returns a pointer to the last element of the linked list.
  • Cumulative option (-v verbose, -vv more verbose, -vvv even more verbose):
    int verbosity = options[VERBOSE].count();
  • Iterate over all –file=<fname> arguments:
    for (Option* opt = options[FILE]; opt; opt = opt->next())
    fname = opt->arg; ...
    A parsed option from the command line together with its argument if it has one.
    Option * next()
    Returns a pointer to the next element of the linked list or NULL if called on last().

Definition at line 453 of file OptionParser.h.

Public Member Functions

 Option ()
 Creates a new Option that is a one-element linked list and has NULL desc, name, arg and namelen.
 
 Option (const Descriptor *desc_, const char *name_, const char *arg_)
 Creates a new Option that is a one-element linked list and has the given values for desc, name and arg.
 
 Option (const Option &orig)
 Makes *this a copy of orig except for the linked list pointers.
 
void append (Option *new_last)
 Makes new_last the new last() by chaining it into the list after last().
 
int count ()
 Returns the number of times this Option (or others with the same Descriptor::index) occurs in the argument vector.
 
Optionfirst ()
 Returns a pointer to the first element of the linked list.
 
int index () const
 Returns Descriptor::index of this Option's Descriptor, or -1 if this Option is invalid (unused).
 
bool isFirst () const
 Returns true iff this is the first element of the linked list.
 
bool isLast () const
 Returns true iff this is the last element of the linked list.
 
Optionlast ()
 Returns a pointer to the last element of the linked list.
 
Optionnext ()
 Returns a pointer to the next element of the linked list or NULL if called on last().
 
Optionnextwrap ()
 Returns a pointer to the next element of the linked list with wrap-around from last() to first().
 
 operator const Option * () const
 Casts from Option to const Option* but only if this Option is valid.
 
 operator Option * ()
 Casts from Option to Option* but only if this Option is valid.
 
Optionoperator= (const Option &orig)
 Makes *this a copy of orig except for the linked list pointers.
 
Optionprev ()
 Returns a pointer to the previous element of the linked list or NULL if called on first().
 
Optionprevwrap ()
 Returns a pointer to the previous element of the linked list with wrap-around from first() to last().
 
int type () const
 Returns Descriptor::type of this Option's Descriptor, or 0 if this Option is invalid (unused).
 

Public Attributes

const char * arg
 Pointer to this Option's argument (if any).
 
const Descriptordesc
 Pointer to this Option's Descriptor.
 
const char * name
 The name of the option as used on the command line.
 
int namelen
 The length of the option name.
 

Private Member Functions

void init (const Descriptor *desc_, const char *name_, const char *arg_)
 

Static Private Member Functions

static bool isTagged (Option *ptr)
 
static Optiontag (Option *ptr)
 
static Optionuntag (Option *ptr)
 

Private Attributes

Optionnext_
 
Optionprev_
 

#include </home/sftnight/build/workspace/root-makedoc-v634/rootspi/rdoc/src/v6-34-00-patches/core/dictgen/res/OptionParser.h>

Constructor & Destructor Documentation

◆ Option() [1/3]

ROOT::option::Option::Option ( )
inline

Creates a new Option that is a one-element linked list and has NULL desc, name, arg and namelen.

Definition at line 766 of file OptionParser.h.

◆ Option() [2/3]

ROOT::option::Option::Option ( const Descriptor desc_,
const char *  name_,
const char *  arg_ 
)
inline

Creates a new Option that is a one-element linked list and has the given values for desc, name and arg.

If name_ points at a character other than '-' it will be assumed to refer to a short option and namelen will be set to 1. Otherwise the length will extend to the first '=' character or the string's 0-terminator.

Definition at line 781 of file OptionParser.h.

◆ Option() [3/3]

ROOT::option::Option::Option ( const Option orig)
inline

Makes *this a copy of orig except for the linked list pointers.

After this operation *this will be a one-element linked list.

Definition at line 803 of file OptionParser.h.

Member Function Documentation

◆ append()

void ROOT::option::Option::append ( Option new_last)
inline

Makes new_last the new last() by chaining it into the list after last().

It doesn't matter which element you call append() on. The new element will always be appended to last().

Attention
new_last must not yet be part of a list, or that list will become corrupted, because this method does not unchain new_last from an existing list.

Definition at line 710 of file OptionParser.h.

◆ count()

int ROOT::option::Option::count ( )
inline

Returns the number of times this Option (or others with the same Descriptor::index) occurs in the argument vector.

This corresponds to the number of elements in the linked list this Option is part of. It doesn't matter on which element you call count(). The return value is always the same.

Use this to implement cumulative options, such as -v, -vv, -vvv for different verbosity levels.

Returns 0 when called for an unused/invalid option.

Definition at line 570 of file OptionParser.h.

◆ first()

Option * ROOT::option::Option::first ( )
inline

Returns a pointer to the first element of the linked list.

Use this when you want the first occurrence of an option on the command line to take precedence. Note that this is not the way most programs handle options. You should probably be using last() instead.

Note
This method may be called on an unused/invalid option and will return a pointer to the option itself.

Definition at line 619 of file OptionParser.h.

◆ index()

int ROOT::option::Option::index ( ) const
inline

Returns Descriptor::index of this Option's Descriptor, or -1 if this Option is invalid (unused).

Definition at line 553 of file OptionParser.h.

◆ init()

void ROOT::option::Option::init ( const Descriptor desc_,
const char *  name_,
const char *  arg_ 
)
inlineprivate

Definition at line 817 of file OptionParser.h.

◆ isFirst()

bool ROOT::option::Option::isFirst ( ) const
inline

Returns true iff this is the first element of the linked list.

The first element in the linked list is the first option on the command line that has the respective Descriptor::index value.

Returns true for an unused/invalid option.

Definition at line 590 of file OptionParser.h.

◆ isLast()

bool ROOT::option::Option::isLast ( ) const
inline

Returns true iff this is the last element of the linked list.

The last element in the linked list is the last option on the command line that has the respective Descriptor::index value.

Returns true for an unused/invalid option.

Definition at line 603 of file OptionParser.h.

◆ isTagged()

static bool ROOT::option::Option::isTagged ( Option ptr)
inlinestaticprivate

Definition at line 844 of file OptionParser.h.

◆ last()

Option * ROOT::option::Option::last ( )
inline

Returns a pointer to the last element of the linked list.

Use this when you want the last occurrence of an option on the command line to take precedence. This is the most common way of handling conflicting options.

Note
This method may be called on an unused/invalid option and will return a pointer to the option itself.
Tip:
If you have options with opposite meanings (e.g. --enable-foo and --disable-foo), you can assign them the same Descriptor::index to get them into the same list. Distinguish them by Descriptor::type and all you have to do is check last()->type() to get the state listed last on the command line.

Definition at line 643 of file OptionParser.h.

◆ next()

Option * ROOT::option::Option::next ( )
inline

Returns a pointer to the next element of the linked list or NULL if called on last().

If called on last() this method returns NULL. Otherwise it will return the option with the same Descriptor::index that follows this option on the command line.

Definition at line 682 of file OptionParser.h.

◆ nextwrap()

Option * ROOT::option::Option::nextwrap ( )
inline

Returns a pointer to the next element of the linked list with wrap-around from last() to first().

If called on last() this method returns first(). Otherwise it will return the option with the same Descriptor::index that follows this option on the command line.

Definition at line 695 of file OptionParser.h.

◆ operator const Option *()

ROOT::option::Option::operator const Option * ( ) const
inline

Casts from Option to const Option* but only if this Option is valid.

If this Option is valid (i.e. desc!=NULL), returns this. Otherwise returns NULL. This allows testing an Option directly in an if-clause to see if it is used:

if (options[CREATE])
{
...
}

It also allows you to write loops like this:

for (Option* opt = options[FILE]; opt; opt = opt->next())
fname = opt->arg; ...

Definition at line 736 of file OptionParser.h.

◆ operator Option *()

ROOT::option::Option::operator Option * ( )
inline

Casts from Option to Option* but only if this Option is valid.

If this Option is valid (i.e. desc!=NULL), returns this. Otherwise returns NULL. This allows testing an Option directly in an if-clause to see if it is used:

if (options[CREATE])
{
...
}

It also allows you to write loops like this:

for (Option* opt = options[FILE]; opt; opt = opt->next())
fname = opt->arg; ...

Definition at line 757 of file OptionParser.h.

◆ operator=()

Option & ROOT::option::Option::operator= ( const Option orig)
inline

Makes *this a copy of orig except for the linked list pointers.

After this operation *this will be a one-element linked list.

Definition at line 792 of file OptionParser.h.

◆ prev()

Option * ROOT::option::Option::prev ( )
inline

Returns a pointer to the previous element of the linked list or NULL if called on first().

If called on first() this method returns NULL. Otherwise it will return the option with the same Descriptor::index that precedes this option on the command line.

Definition at line 656 of file OptionParser.h.

◆ prevwrap()

Option * ROOT::option::Option::prevwrap ( )
inline

Returns a pointer to the previous element of the linked list with wrap-around from first() to last().

If called on first() this method returns last(). Otherwise it will return the option with the same Descriptor::index that precedes this option on the command line.

Definition at line 669 of file OptionParser.h.

◆ tag()

static Option * ROOT::option::Option::tag ( Option ptr)
inlinestaticprivate

Definition at line 834 of file OptionParser.h.

◆ type()

int ROOT::option::Option::type ( ) const
inline

Returns Descriptor::type of this Option's Descriptor, or 0 if this Option is invalid (unused).

Because this method (and last(), too) can be used even on unused Options with desc==0, you can (provided you arrange your types properly) switch on type() without testing validity first.

enum OptionType { UNUSED=0, DISABLED=0, ENABLED=1 };
enum OptionIndex { FOO };
const Descriptor usage[] = {
{ FOO, ENABLED, "", "enable-foo", Arg::None, 0 },
{ FOO, DISABLED, "", "disable-foo", Arg::None, 0 },
{ 0, 0, 0, 0, 0, 0 } };
...
switch(options[FOO].last()->type()) // no validity check required!
{
case ENABLED: ...
case DISABLED: ... // UNUSED==DISABLED !
}
static ArgStatus None(const Option &, bool)
For options that don't take an argument: Returns ARG_NONE.

Definition at line 544 of file OptionParser.h.

◆ untag()

static Option * ROOT::option::Option::untag ( Option ptr)
inlinestaticprivate

Definition at line 839 of file OptionParser.h.

Member Data Documentation

◆ arg

const char* ROOT::option::Option::arg

Pointer to this Option's argument (if any).

NULL if this option has no argument. Do not confuse this with the empty string which is a valid argument.

Definition at line 500 of file OptionParser.h.

◆ desc

const Descriptor* ROOT::option::Option::desc

Pointer to this Option's Descriptor.

Remember that the first dummy descriptor (see Descriptor::longopt) is used for unknown options.

Attention
desc==NULL signals that this Option is unused. This is the default state of elements in the result array. You don't need to test desc explicitly. You can simply write something like this:
if (options[CREATE])
{
...
}
This works because of operator const Option*() .

Definition at line 476 of file OptionParser.h.

◆ name

const char* ROOT::option::Option::name

The name of the option as used on the command line.

The main purpose of this string is to be presented to the user in messages.

In the case of a long option, this is the actual argv pointer, i.e. the first character is a '-'. In the case of a short option this points to the option character within the argv string.

Note that in the case of a short option group or an attached option argument, this string will contain additional characters following the actual name. Use namelen to filter out the actual option name only.

Definition at line 492 of file OptionParser.h.

◆ namelen

int ROOT::option::Option::namelen

The length of the option name.

Because name points into the actual argv string, the option name may be followed by more characters (e.g. other short options in the same short option group). This value is the number of bytes (not characters!) that are part of the actual name.

For a short option, this length is always 1. For a long option this length is always at least 2 if single minus long options are permitted and at least 3 if they are disabled.

Note
In the pathological case of a minus within a short option group (e.g. -xf-z), this length is incorrect, because this case will be misinterpreted as a long option and the name will therefore extend to the string's 0-terminator or a following '=" character if there is one. This is irrelevant for most uses of name and namelen. If you really need to distinguish the case of a long and a short option, compare name to the argv pointers. A long option's name is always identical to one of them, whereas a short option's is never.

Definition at line 521 of file OptionParser.h.

◆ next_

Option* ROOT::option::Option::next_
private

Definition at line 455 of file OptionParser.h.

◆ prev_

Option* ROOT::option::Option::prev_
private

Definition at line 456 of file OptionParser.h.

  • core/dictgen/res/OptionParser.h