Classes |
class | ACE_Get_Opt_Long_Option |
Public Types |
enum | { REQUIRE_ORDER = 1,
PERMUTE_ARGS = 2,
RETURN_IN_ORDER = 3
} |
| Mutually exclusive ordering values. More...
|
enum | OPTION_ARG_MODE { NO_ARG = 0,
ARG_REQUIRED = 1,
ARG_OPTIONAL = 2
} |
| Mutually exclusive option argument mode used by long options. More...
|
Public Member Functions |
| ACE_Get_Opt (int argc, ACE_TCHAR **argv, const ACE_TCHAR *optstring=ACE_TEXT(""), int skip_args=1, int report_errors=0, int ordering=PERMUTE_ARGS, int long_only=0) |
| ~ACE_Get_Opt (void) |
| Default dtor.
|
int | operator() (void) |
ACE_TCHAR * | opt_arg (void) const |
int | opt_opt (void) |
int & | opt_ind (void) |
int | long_option (const ACE_TCHAR *name, OPTION_ARG_MODE has_arg=NO_ARG) |
| Adds a long option with no corresponding short option.
|
int | long_option (const ACE_TCHAR *name, int short_option, OPTION_ARG_MODE has_arg=NO_ARG) |
| Adds a long option with a corresponding short option.
|
const ACE_TCHAR * | long_option (void) const |
int | argc (void) const |
| The number of arguments in the internal argv_ .
|
ACE_TCHAR ** | argv (void) const |
| Accessor for the internal argv_ pointer.
|
const ACE_TCHAR * | last_option (void) const |
void | dump (void) const |
| Dump the state of an object.
|
const ACE_TCHAR * | optstring (void) const |
Public Attributes |
int | argc_ |
| Holds the argc count.
|
ACE_TCHAR ** | argv_ |
| Holds the argv pointer.
|
int | optind |
| Index in argv_ of the next element to be scanned.
|
int | opterr |
ACE_TCHAR * | optarg |
Private Member Functions |
int | nextchar_i (void) |
| Updates nextchar_.
|
int | long_option_i (void) |
| Handles long options.
|
int | short_option_i (void) |
| Handles short options.
|
void | permute_args (void) |
int | permute (void) |
| Handles reordering <argv>-elements.
|
void | last_option (const ACE_TString &s) |
| Set last_option.
|
| ACE_Get_Opt (const ACE_Get_Opt &) |
ACE_Get_Opt & | operator= (const ACE_Get_Opt &) |
Private Attributes |
ACE_TString * | optstring_ |
| Holds the option string.
|
int | long_only_ |
| Treat all options as long options.
|
int | has_colon_ |
ACE_TString * | last_option_ |
ACE_TCHAR * | nextchar_ |
int | optopt_ |
| Most recently matched short option character.
|
int | ordering_ |
| Keeps track of ordering mode (default <PERMUTE_ARGS>).
|
int | nonopt_start_ |
int | nonopt_end_ |
ACE_Get_Opt_Long_Option * | long_option_ |
| Points to the long_option found on last call to <operator()>.
|
ACE_Array
< ACE_Get_Opt_Long_Option * > | long_opts_ |
| Array of long options.
|
| ACE_ALLOC_HOOK_DECLARE |
| Declare the dynamic allocation hooks.
|
Iterator for parsing command-line arguments.
This is a C++ wrapper for getopt(3c) and getopt_long(3c).
Constructor initializes the command line to be parsed. All information for parsing must be supplied to this constructor.
- Parameters:
-
argc | The number of argv elements to parse. |
argv | Command line tokens, such as would be passed to main() . |
optstring | Nul-terminated string containing the legitimate short option characters. A single colon ":" following an option character means the option requires an argument. A double colon "::" following an option character means the argument is optional. The argument is taken from the rest of the current argv element, or from the following argv element (only valid for required arguments; optional arguments must always reside in the same argv element). The argument value, if any is returned by the opt_arg() method. optstring can be extended by adding long options with corresponding short options via the long_option() method. If the short option already appears in optstring, the argument characteristics must match, otherwise it is added. See long_option() for more information. If 'W', followed by a semi-colon ';' appears in optstring, then any time a 'W' appears on the command line, the following argument is treated as a long option. For example, if the command line contains "program -W foo", "foo" is treated as a long option, that is, as if "program --foo" had been passed. The following characters can appear in optstring before any option characters, with the described effect:
- '+' changes the ordering to REQUIRE_ORDER.
- '-' changes the ordering to RETURN_IN_ORDER.
- ':' changes the return value from
operator() and get_opt() from '?' to ':' when an option requires an argument but none is specified.
|
skip_args | Optional (default 1). The specified number of initial elements in argv are skipped before parsing begins. Thus, the default prevents argv[0] (usually the command name) from being parsed. argc includes all argv elements, including any skipped elements. |
report_errors | Optional, if non-zero then parsing errors cause an error message to be displayed from the operator() method before it returns. The error message is suppressed if this argument is 0. This setting also controls whether or not an error message is displayed in long_option() encounters an error. |
ordering | Optional (default is PERMUTE_ARGS ); determines how the argv elements are processed. This argument is overridden by two factors:
- The
POSIXLY_CORRECT environment variable. If this environment variable is set, the ordering is changed to REQUIRE_ORDER .
- Leading characters in optstring (see above). Any leading ordering characters override both the ordering argument and any effect of the
POSIXLY_CORRECT environment variable.
|
long_only | Optional. If non-zero, then long options can be specified using a single '-' on the command line. If the token is not a long option, it is processed as usual, that is, as a short option or set of short options. |
Multiple short options can be combined as long as only the last one can takes an argument. For example, if optstring is defined as "abc:"
or "abc::"
then the command line "program -abcxxx" short options a, b, and c are found with "xxx" as the argument for c. However, if the command line is specified as "program -acb" only options a and c are found with "b" as the argument for c. Also, for options with optional arguments, that is, those followed by "::", the argument must be in the same argv element, so "program -abc
xxx" will only find "xxx" as the argument for c if optstring is specified as "abc:"
not "abc::"
.
int ACE_Get_Opt::operator() |
( |
void |
| ) |
|
Scan elements of argv (whose length is argc) for short option characters given in optstring or long options (with no short option equivalents).
If an element of argv starts with '-', and is not exactly "-" or "--", then it is a short option element. The characters of this element (aside from the initial '-') are option characters. If it starts with "--" followed by other characters it is treated as a long option. If operator()
is called repeatedly, it returns each of the option characters from each of the option elements.
- Returns:
- The parsed option character. The following characters have special significance.
- Return values:
-
0 | A long option was found |
'\?' | Either an unknown option character was found, or the option is known but requires an argument, none was specified, and optstring did not contain a leading colon. |
':' | A known option character was found but it requires an argument and none was supplied, and the first character of optstring was a colon. opt_opt() indicates which option was specified. |
'1' | RETURN_IN_ORDER was specified and a non-option argument was found. |
EOF | No more option characters were found. opt_ind() will return the index in argv of the first argv element that is not an option. If PERMUTE_ARGS was specified, the argv elements have been permuted so that those that are not options now come last. |
- Note:
- The standards are unclear with respect to the conditions under which '?' and ':' are returned, so we scan the initial characters of optstring up unto the first short option character for '+', '-', and ':' in order to determine ordering and missing argument behavior.
Index in argv of the next element to be scanned. This is used for communication to and from the caller and for communication between successive calls to operator()
. On entry to operator()
, zero means this is the first call; initialize.
When operator()
returns EOF
, this is the index of the first of the non-option elements that the caller should itself scan.
Otherwise, opt_ind()
communicates from one call to the next how much of argv has been scanned so far.