Classes | Public Types | Public Member Functions | Public Attributes | Private Member Functions | Private Attributes

ACE_Get_Opt Class Reference

Iterator for parsing command-line arguments. More...

#include <Get_Opt.h>

Collaboration diagram for ACE_Get_Opt:
Collaboration graph
[legend]

List of all members.

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_TCHARopt_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_TCHARlong_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_TCHARlast_option (void) const
void dump (void) const
 Dump the state of an object.
const ACE_TCHARoptstring (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_TCHARoptarg

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_Optoperator= (const ACE_Get_Opt &)

Private Attributes

ACE_TStringoptstring_
 Holds the option string.
int long_only_
 Treat all options as long options.
int has_colon_
ACE_TStringlast_option_
ACE_TCHARnextchar_
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_Optionlong_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.

Detailed Description

Iterator for parsing command-line arguments.

This is a C++ wrapper for getopt(3c) and getopt_long(3c).

Definition at line 44 of file Get_Opt.h.


Member Enumeration Documentation

anonymous enum

Mutually exclusive ordering values.

Enumerator:
REQUIRE_ORDER 

REQUIRE_ORDER means that processing stops and EOF is returned as soon as a non-option argument is found. opt_ind() will return the index of the next argv element so the program can continue processing the rest of the argv elements.

PERMUTE_ARGS 

PERMUTE_ARGS means the argv elements are reordered dynamically (permuted) so that all options appear first. When the elements are permuted, the order of the options and the following arguments are maintained. When the last option has been processed, EOF is returned and opt_ind() returns the index into the next non-option element.

RETURN_IN_ORDER 

RETURN_IN_ORDER means each argv element is processed in the order is it seen. If the element is not recognized as an option, '1' is returned and opt_arg() refers to the argv element found.

Definition at line 48 of file Get_Opt.h.

  {
   /**
    * REQUIRE_ORDER means that processing stops and @c EOF is
    * returned as soon as a non-option argument is found. @c opt_ind()
    * will return the index of the next @a argv element so the program
    * can continue processing the rest of the @a argv elements.
    */
    REQUIRE_ORDER = 1,

   /**
    * PERMUTE_ARGS means the @a argv elements are reordered dynamically
    * (permuted) so that all options appear first. When the elements are
    * permuted, the order of the options and the following arguments are
    * maintained. When the last option has been processed, @c EOF is
    * returned and @c opt_ind() returns the index into the next non-option
    * element.
    */
    PERMUTE_ARGS = 2,

   /**
    * RETURN_IN_ORDER means each @a argv element is processed in the
    * order is it seen.  If the element is not recognized as an option, '1'
    * is returned and @c opt_arg() refers to the @a argv element found.
    */
    RETURN_IN_ORDER = 3
  };

Mutually exclusive option argument mode used by long options.

Enumerator:
NO_ARG 

Doesn't take an argument.

ARG_REQUIRED 

Requires an argument, same as passing ":" after a short option character in optstring.

ARG_OPTIONAL 

Argument is optional, same as passing "::" after a short option character in optstring.

Definition at line 77 of file Get_Opt.h.

  {
    /// Doesn't take an argument.
    NO_ARG = 0,

    /// Requires an argument, same as passing ":" after a short option
    /// character in @a optstring.
    ARG_REQUIRED = 1,

    /// Argument is optional, same as passing "::" after a short
    /// option character in @a optstring.
    ARG_OPTIONAL = 2
  };


Constructor & Destructor Documentation

ACE_Get_Opt::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 
)

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:

  1. The POSIXLY_CORRECT environment variable. If this environment variable is set, the ordering is changed to REQUIRE_ORDER.
  2. 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::".

ACE_Get_Opt::~ACE_Get_Opt ( void   ) 

Default dtor.

Definition at line 165 of file Get_Opt.cpp.

{
  ACE_TRACE ("ACE_Get_Opt::~ACE_Get_Opt");

  size_t i = 0;
  size_t size = this->long_opts_.size ();
  ACE_Get_Opt_Long_Option *option = 0;
  for (i = 0; i < size; ++i)
    {
      int retval = this->long_opts_.get (option, i);
      if (retval != 0)
        {
          // Should never happen.
          retval = 0;
          continue;
        }
      if (option)
        {
          delete option;
          option = 0;
        }
    }
  delete this->optstring_;
  delete this->last_option_;
}

ACE_Get_Opt::ACE_Get_Opt ( const ACE_Get_Opt  )  [private]

Member Function Documentation

int ACE_Get_Opt::argc ( void   )  const [inline]

The number of arguments in the internal argv_.

Definition at line 14 of file Get_Opt.inl.

{
  return this->argc_;
}

ACE_TCHAR ** ACE_Get_Opt::argv ( void   )  const [inline]

Accessor for the internal argv_ pointer.

Definition at line 20 of file Get_Opt.inl.

{
  return this->argv_;
}

void ACE_Get_Opt::dump ( void   )  const

Dump the state of an object.

Definition at line 599 of file Get_Opt.cpp.

{
#if defined (ACE_HAS_DUMP)
  ACE_TRACE ("ACE_Get_Opt::dump");

  ACE_DEBUG ((LM_DEBUG, ACE_BEGIN_DUMP, this));
  ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("\n")
              ACE_TEXT ("opstring_ = %s\n")
              ACE_TEXT ("long_only_ = %d\n")
              ACE_TEXT ("has_colon_ = %d\n")
              ACE_TEXT ("last_option_ = %s\n")
              ACE_TEXT ("nextchar_ = %s\n")
              ACE_TEXT ("optopt_ = %c\n")
              ACE_TEXT ("ordering_ = %d\n"),
              this->optstring_->c_str (),
              this->long_only_,
              this->has_colon_,
              this->last_option_->c_str (),
              this->nextchar_,
              this->optopt_,
              this->ordering_));

  // now loop through the
  size_t size = this->long_opts_.size ();
  for (u_int i = 0; i < size ; ++i)
    {
      ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("\n")
                  ACE_TEXT ("long_option name_ = %s\n")
                  ACE_TEXT ("has_arg_ = %d\n")
                  ACE_TEXT ("val_ = %d\n"),
                  this->long_opts_[i]->name_,
                  this->long_opts_[i]->has_arg_,
                  this->long_opts_[i]->val_));
    }
  ACE_DEBUG ((LM_DEBUG, ACE_END_DUMP));
#endif /* ACE_HAS_DUMP */
}

void ACE_Get_Opt::last_option ( const ACE_TString s  )  [private]

Set last_option.

Definition at line 593 of file Get_Opt.cpp.

{
  *this->last_option_ = last_option;
}

const ACE_TCHAR * ACE_Get_Opt::last_option ( void   )  const

Accessor for the last_option that was processed. This allows applications to know if the found option was a short or long option, and is especially useful in cases where it was invalid and the caller wants to print out the invalid value.

Definition at line 587 of file Get_Opt.cpp.

{
  return this->last_option_->c_str ();
}

int ACE_Get_Opt::long_option ( const ACE_TCHAR name,
OPTION_ARG_MODE  has_arg = NO_ARG 
)

Adds a long option with no corresponding short option.

If the name option is seen, operator() returns 0.

Parameters:
name The long option to add.
has_arg Defines the argument requirements for the new option.
Return values:
0 Success
-1 The long option can not be added.

Definition at line 477 of file Get_Opt.cpp.

{
  ACE_TRACE ("ACE_Get_Opt::long_option (const ACE_TCHAR *name, OPTION_ARG_MODE has_arg)");
  return this->long_option (name, 0, has_arg);
}

int ACE_Get_Opt::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.

Parameters:
name The long option to add.
short_option A character, the short option that corresponds to name.
has_arg Defines the argument requirements for the new option. If the short option has already been supplied in the optstring, has_arg must match or an error is returned; otherwise, the new short option is added to the optstring.
Return values:
0 Success
-1 The long option can not be added.

Definition at line 485 of file Get_Opt.cpp.

{
  ACE_TRACE ("ACE_Get_Opt::long_option (const ACE_TCHAR *name, int short_option, OPTION_ARG_MODE has_arg)");

  // We only allow valid alpha-numeric characters as short options.
  // If short_options is not a valid alpha-numeric, we can still return it
  // when the long option is found, but won't allow the caller to pass it on
  // the command line (how could they?).  The special case is 0, but since
  // we always return it, we let the caller worry about that.
  if (ACE_OS::ace_isalnum (short_option) != 0)
    {
      // If the short_option already exists, make sure it matches, otherwise
      // add it.
      ACE_TCHAR *s = 0;
      if ((s = const_cast<ACE_TCHAR*> (
                 ACE_OS::strchr (this->optstring_->c_str (),
                                 short_option))) != 0)
        {
          // Short option exists, so verify the argument options
          if (s[1] == ':')
            {
              if (s[2] == ':')
                {
                  if (has_arg != ARG_OPTIONAL)
                    {
                      if (this->opterr)
                        ACE_ERROR
                          ((LM_ERROR,
                            ACE_TEXT ("Existing short option '%c' takes ")
                            ACE_TEXT ("optional argument; adding %s ")
                            ACE_TEXT ("requires ARG_OPTIONAL\n"),
                            short_option, name));
                      return -1;
                    }
                }
              else
                if (has_arg != ARG_REQUIRED)
                  {
                    if (this->opterr)
                      ACE_ERROR
                        ((LM_ERROR,
                          ACE_TEXT ("Existing short option '%c' requires ")
                          ACE_TEXT ("an argument; adding %s ")
                          ACE_TEXT ("requires ARG_REQUIRED\n"),
                          short_option, name));
                    return -1;
                  }
            }
          else if (has_arg != NO_ARG)
            {
              if (this->opterr)
                ACE_ERROR
                  ((LM_ERROR,
                    ACE_TEXT ("Existing short option '%c' does not ")
                    ACE_TEXT ("accept an argument; adding %s ")
                    ACE_TEXT ("requires NO_ARG\n"),
                    short_option, name));
              return -1;
            }
        }
      else
        {
          // Didn't find short option, so add it...
          *this->optstring_ += (ACE_TCHAR) short_option;
          if (has_arg == ARG_REQUIRED)
            *this->optstring_ += ACE_TEXT (":");
          else if (has_arg == ARG_OPTIONAL)
            *this->optstring_ += ACE_TEXT ("::");
        }
    }

  ACE_Get_Opt_Long_Option *option =
    new ACE_Get_Opt_Long_Option (name, has_arg, short_option);

  if (!option)
    return -1;

  // Add to array
  size_t size = this->long_opts_.size ();
  if (this->long_opts_.size (size + 1) != 0
      || this->long_opts_.set (option, size) != 0)
    {
      delete option;
      ACE_ERROR_RETURN
        ((LM_ERROR, ACE_TEXT ("Could not add long option to array.\n")),
         -1);
    }
  return 0;
}

const ACE_TCHAR * ACE_Get_Opt::long_option ( void   )  const

Returns the name of the long option found on the last call to operator() or 0 if none was found.

Definition at line 578 of file Get_Opt.cpp.

{
  ACE_TRACE ("ACE_Get_Opt::long_option (void)");
  if (this->long_option_)
    return this->long_option_->name_;
  return 0;
}

int ACE_Get_Opt::long_option_i ( void   )  [private]

Handles long options.

Definition at line 240 of file Get_Opt.cpp.

{
  ACE_TRACE ("ACE_Get_Opt::long_option_i");

  ACE_Get_Opt_Long_Option *p;
  ACE_TCHAR *s = this->nextchar_;
  int hits = 0;
  int exact = 0;
  ACE_Get_Opt_Long_Option *pfound = 0;
  int indfound = 0;

  // Advance to the end of the long option name so we can use
  // it to get the length for a string compare.
  while (*s && *s != '=')
    s++;

  size_t len = s - this->nextchar_;
  // set last_option_ to nextchar_, up to the '='.
  this->last_option (ACE_TString (this->nextchar_, len));

  size_t size = this->long_opts_.size ();
  u_int option_index = 0;
  for (option_index = 0; option_index < size ; option_index++)
    {
      p = this->long_opts_[option_index];
      ACE_ASSERT (p);

      if (!ACE_OS::strncmp (p->name_, this->nextchar_, len))
        {
          // Got at least a partial match.
          pfound = p;
          indfound = option_index;
          hits += 1;
          if (len == ACE_OS::strlen(p->name_))
            {
              // And in fact, it's an exact match, so let's use it.
              exact = 1;
              break;
            }
        }
    }

  if ((hits > 1) && !exact)
    {
      // Great, we found a match, but unfortunately we found more than
      // one and it wasn't exact.
      if (this->opterr)
        ACE_ERROR ((LM_ERROR,
                    ACE_TEXT ("%s: option `%s' is ambiguous\n"),
                    this->argv_[0], this->argv_[this->optind]));
      this->nextchar_ = 0;
      this->optind++;
      return '?';
    }

  if (pfound != 0)
    {
      // Okay, we found a good one (either a single hit or an exact match).
      option_index = indfound;
      this->optind++;
      if (*s)
        {
          // s must point to '=' which means there's an argument (well
          // close enough).
          if (pfound->has_arg_ != NO_ARG)
            // Good, we want an argument and here it is.
            this->optarg = ++s;
          else
            {
              // Whoops, we've got what looks like an argument, but we
              // don't want one.
              if (this->opterr)
                  ACE_ERROR
                    ((LM_ERROR,
                      ACE_TEXT ("%s: long option `--%s' doesn't allow ")
                      ACE_TEXT ("an argument\n"),
                      this->argv_[0], pfound->name_));
              // The spec doesn't cover this, so we keep going and the program
              // doesn't know we ignored an argument if opt_err is off!!!
            }
        }
      else if (pfound->has_arg_ == ARG_REQUIRED)
        {
          // s didn't help us, but we need an argument. Note that
          // optional arguments for long options must use the "=" syntax,
          // so we won't get here in that case.
          if (this->optind < this->argc_)
            // We still have some elements left, so use the next one.
            this->optarg = this->argv_[this->optind++];
          else
            {
              // All out of elements, so we have to punt...
              if (this->opterr)
                ACE_ERROR ((LM_ERROR,
                            ACE_TEXT ("%s: long option '--%s' requires ")
                            ACE_TEXT ("an argument\n"),
                            this->argv_[0], pfound->name_));
              this->nextchar_ = 0;
              this->optopt_ = pfound->val_;   // Remember matching short equiv
              return this->has_colon_ ? ':' : '?';
            }
        }
      this->nextchar_ = 0;
      this->long_option_ = pfound;
      // Since val_ has to be either a valid short option or 0, this works
      // great.  If the user really wants to know if a long option was passed.
      this->optopt_ = pfound->val_;
      return pfound->val_;
    }
  if (!this->long_only_ || this->argv_[this->optind][1] == '-'
      || this->optstring_->find (*this->nextchar_) == ACE_TString::npos)
    {
      // Okay, we couldn't find a long option.  If it isn't long_only (which
      // means try the long first, and if not found try the short) or a long
      // signature was passed, e.g. "--", or it's not a short (not sure when
      // this could happen) it's an error.
      if (this->opterr)
        ACE_ERROR ((LM_ERROR,
                    ACE_TEXT ("%s: illegal long option '--%s'\n"),
                    this->argv_[0], this->nextchar_));
      this->nextchar_ = 0;
      this->optind++;
      return '?';
    }
  return this->short_option_i ();
}

int ACE_Get_Opt::nextchar_i ( void   )  [private]

Updates nextchar_.

Definition at line 192 of file Get_Opt.cpp.

{
  ACE_TRACE ("ACE_Get_Opt::nextchar_i");

  if (this->ordering_ == PERMUTE_ARGS)
    if (this->permute () == EOF)
      return EOF;

  // Update scanning pointer.
  if (this->optind >= this->argc_)
    {
      // We're done...
      this->nextchar_ = 0;
      return EOF;
    }
  else if (*(this->nextchar_ = this->argv_[this->optind]) != '-'
            || this->nextchar_[1] == '\0')
    {
      // We didn't get an option.

      if (this->ordering_ == REQUIRE_ORDER
          || this->ordering_ == PERMUTE_ARGS)
        // If we permuted or require the options to be in order, we're done.
        return EOF;

      // It must be RETURN_IN_ORDER...
      this->optarg = this->argv_[this->optind++];
      this->nextchar_ = 0;
      return 1;
    }
  else if (this->nextchar_[1] != 0
           && *++this->nextchar_ == '-'
           && this->nextchar_[1] == 0)
    {
      // Found "--" so we're done...
      ++this->optind;
      this->nextchar_ = 0;
      return EOF;
    }

  // If it's a long option, and we allow long options advance nextchar_.
  if (*this->nextchar_ == '-' && this->long_opts_.size () != 0)
    this->nextchar_++;

  return 0;
}

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.

Definition at line 445 of file Get_Opt.cpp.

{
  ACE_TRACE ("ACE_Get_Opt_Long::operator");

  // First of all, make sure we reinitialize any pointers..
  this->optarg = 0;
  this->long_option_ = 0;

  if (this->argv_ == 0)
    {
      // It can happen, e.g., on VxWorks.
      this->optind = 0;
      return -1;
    }

  // We check this because we can string short options together if the
  // preceding one doesn't take an argument.
  if (this->nextchar_ == 0 || *this->nextchar_ == '\0')
    {
      int retval = this->nextchar_i ();
      if (retval != 0)
        return retval;
    }

  if (((this->argv_[this->optind][0] == '-')
       && (this->argv_[this->optind][1] == '-')) || this->long_only_)
    return this->long_option_i ();

  return this->short_option_i ();
}

ACE_Get_Opt& ACE_Get_Opt::operator= ( const ACE_Get_Opt  )  [private]
ACE_TCHAR * ACE_Get_Opt::opt_arg ( void   )  const [inline]

For communication from operator() to the caller. When operator() finds an option that takes an argument, the argument value is returned from this method, otherwise it returns 0.

Definition at line 26 of file Get_Opt.inl.

{
  return this->optarg;
}

int & ACE_Get_Opt::opt_ind ( void   )  [inline]

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.

Definition at line 38 of file Get_Opt.inl.

{
  return this->optind;
}

int ACE_Get_Opt::opt_opt ( void   )  [inline]

Returns the most recently matched option character. Especially useful when operator() returns ':' for an unspecified argument that's required, since this allows the caller to learn what option was specified without its required argument.

Definition at line 32 of file Get_Opt.inl.

{
  return this->optopt_;
}

const ACE_TCHAR * ACE_Get_Opt::optstring ( void   )  const

Return the optstring. This is handy to verify that calls to long_option added short options as expected.

Definition at line 715 of file Get_Opt.cpp.

{
  return this->optstring_->c_str ();
}

int ACE_Get_Opt::permute ( void   )  [private]

Handles reordering <argv>-elements.

Definition at line 674 of file Get_Opt.cpp.

{
  ACE_TRACE ("ACE_Get_Opt::permute");

  if (this->nonopt_start_ != this->nonopt_end_
      && this->nonopt_start_ != this->optind)
    this->permute_args ();

  this->nonopt_start_ = this->optind;

  // Skip over args untill we find the next option.
  while (this->optind < this->argc_
         && (this->argv_[this->optind][0] != '-'
             || this->argv_[this->optind][1] == '\0'))
    this->optind++;

  // Got an option, so mark this as the end of the non options.
  this->nonopt_end_ = this->optind;

  if (this->optind != this->argc_
      && ACE_OS::strcmp (this->argv_[this->optind],
                         ACE_TEXT ("--")) == 0)
    {
      // We found the marker for the end of the options.
      ++this->optind;

      if (this->nonopt_start_ != this->nonopt_end_
          && this->nonopt_end_ != this->optind)
        this->permute_args ();
    }

  if (this->optind == this->argc_)
    {
      if (this->nonopt_start_ != this->nonopt_end_)
        this->optind = this->nonopt_start_;
      return EOF;
    }
  return 0;
}

void ACE_Get_Opt::permute_args ( void   )  [private]

If permuting args, this functions manages the nonopt_start_ and nonopt_end_ indexes and makes calls to permute to actually reorder the <argv>-elements.

Definition at line 638 of file Get_Opt.cpp.

{
  ACE_TRACE ("ACE_Get_Opt::permute_args");

  u_long cyclelen, i, j, ncycle, nnonopts, nopts;
  u_long opt_end = this->optind;
  int cstart, pos = 0;
  ACE_TCHAR *swap = 0;

  nnonopts = this->nonopt_end_ - this->nonopt_start_;
  nopts = opt_end - this->nonopt_end_;
  ncycle = ACE::gcd (nnonopts, nopts);
  cyclelen = (opt_end - this->nonopt_start_) / ncycle;

  this->optind = this->optind - nnonopts;

  for (i = 0; i < ncycle; i++)
    {
      cstart = this->nonopt_end_ + i;
      pos = cstart;
      for (j = 0; j < cyclelen; j++)
        {
          if (pos >= this->nonopt_end_)
            pos -= nnonopts;
          else
            pos += nopts;
          swap = this->argv_[pos];

          ((ACE_TCHAR **)this->argv_)[pos] = argv_[cstart];

          ((ACE_TCHAR **)this->argv_)[cstart] = swap;
        }
    }
}

int ACE_Get_Opt::short_option_i ( void   )  [private]

Handles short options.

Definition at line 368 of file Get_Opt.cpp.

{
  ACE_TRACE ("ACE_Get_Opt::short_option_i");

  /* Look at and handle the next option-character.  */
  ACE_TCHAR opt = *this->nextchar_++;
  // Set last_option_ to opt
  this->last_option (opt);

  ACE_TCHAR *oli = 0;
  oli =
    const_cast<ACE_TCHAR*> (ACE_OS::strchr (this->optstring_->c_str (), opt));

  /* Increment `optind' when we start to process its last character.  */
  if (*this->nextchar_ == '\0')
    ++this->optind;

  if (oli == 0 || opt == ':')
    {
      if (this->opterr)
        ACE_ERROR ((LM_ERROR,
                    ACE_TEXT ("%s: illegal short option -- %c\n"),
                    this->argv_[0], opt));
      return '?';
    }
  if (opt == 'W' && oli[1] == ';')
    {
      if (this->nextchar_[0] == 0)
        this->nextchar_ = this->argv_[this->optind];
      return long_option_i ();
    }
  this->optopt_ = oli[0];      // Remember the option that matched
  if (oli[1] == ':')
    {
      if (oli[2] == ':')
        {
          // Takes an optional argument, and since short option args must
          // must follow directly in the same argument, a NULL nextchar_
          // means we didn't get one.
          if (*this->nextchar_ != '\0')
            {
              this->optarg = this->nextchar_;
              this->optind++;
            }
          else
            this->optarg = 0;
          this->nextchar_ = 0;
        }
      else
        {
          // Takes a required argument.
          if (*this->nextchar_ != '\0')
            {
              // Found argument in same argv-element.
              this->optarg = this->nextchar_;
              this->optind++;
            }
          else if (this->optind == this->argc_)
            {
              // Ran out of arguments before finding required argument.
              if (this->opterr)
                ACE_ERROR ((LM_ERROR,
                            ACE_TEXT ("%s: short option requires ")
                            ACE_TEXT ("an argument -- %c\n"),
                            this->argv_[0], opt));
              opt = this->has_colon_ ? ':' : '?';
            }
          else
            // Use the next argv-element as the argument.
            this->optarg = this->argv_[this->optind++];
          this->nextchar_ = 0;
        }
    }
  return opt;
}


Member Data Documentation

Declare the dynamic allocation hooks.

Definition at line 483 of file Get_Opt.h.

Holds the argc count.

Deprecated:
This is public for backwards compatibility only. It will be made private in a release of ACE past 5.3. Do not write code that relies on this member being public; use the argc() accessor method instead.

Definition at line 339 of file Get_Opt.h.

Holds the argv pointer.

Deprecated:
This is public for backwards compatibility only. It will be made private in a release of ACE past 5.3. Do not write code that relies on this member being public; use the argv() accessor method instead.

Definition at line 348 of file Get_Opt.h.

int ACE_Get_Opt::has_colon_ [private]

Keeps track of whether or not a colon was passed in <optstring>. This is used to determine the return value when required arguments are missing.

Definition at line 447 of file Get_Opt.h.

This is the last option, short or long, that was processed. This is handy to have in cases where the option passed was invalid.

Definition at line 451 of file Get_Opt.h.

int ACE_Get_Opt::long_only_ [private]

Treat all options as long options.

Definition at line 442 of file Get_Opt.h.

Points to the long_option found on last call to <operator()>.

Definition at line 477 of file Get_Opt.h.

Array of long options.

Definition at line 480 of file Get_Opt.h.

The next char to be scanned in the option-element in which the last option character we returned was found. This allows us to pick up the scan where we left off * If this is zero, or a null string, it means resume the scan by advancing to the next <argv>-element.

Definition at line 460 of file Get_Opt.h.

int ACE_Get_Opt::nonopt_end_ [private]

Index of the <argv>-element following the last non-option element (only valid when permuting).

Definition at line 474 of file Get_Opt.h.

Index of the first non-option <argv>-element found (only valid when permuting).

Definition at line 470 of file Get_Opt.h.

Points to the option argument when one is found on last call to operator().

Deprecated:
This is public for backwards compatibility only. It will be made private in a release of ACE past 5.3. Do not write code that relies on this member being public; use the opt_arg() accessor method instead.

Definition at line 377 of file Get_Opt.h.

Callers store zero here to inhibit the error message for unrecognized options.

Deprecated:
This is public for backwards compatibility only. It will be made private in a release of ACE past 5.3. Do not write code that relies on this member being public; use the report_errors argument to this class's constructor instead.

Definition at line 367 of file Get_Opt.h.

Index in argv_ of the next element to be scanned.

Deprecated:
This is public for backwards compatibility only. It will be made private in a release of ACE past 5.3. Do not write code that relies on this member being public; use the opt_ind() accessor method instead.

Definition at line 357 of file Get_Opt.h.

int ACE_Get_Opt::optopt_ [private]

Most recently matched short option character.

Definition at line 463 of file Get_Opt.h.

Holds the option string.

Definition at line 439 of file Get_Opt.h.

int ACE_Get_Opt::ordering_ [private]

Keeps track of ordering mode (default <PERMUTE_ARGS>).

Definition at line 466 of file Get_Opt.h.


The documentation for this class was generated from the following files:
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines