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.

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

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.

Enumeration values:
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.

00049   {
00050    /**
00051     * REQUIRE_ORDER means that processing stops and @c EOF is
00052     * returned as soon as a non-option argument is found. @c opt_ind()
00053     * will return the index of the next @a argv element so the program
00054     * can continue processing the rest of the @a argv elements.
00055     */
00056     REQUIRE_ORDER = 1,
00057 
00058    /**
00059     * PERMUTE_ARGS means the @a argv elements are reordered dynamically
00060     * (permuted) so that all options appear first. When the elements are
00061     * permuted, the order of the options and the following arguments are
00062     * maintained. When the last option has been processed, @c EOF is
00063     * returned and @c opt_ind() returns the index into the next non-option
00064     * element.
00065     */
00066     PERMUTE_ARGS = 2,
00067 
00068    /**
00069     * RETURN_IN_ORDER means each @a argv element is processed in the
00070     * order is it seen.  If the element is not recognized as an option, '1'
00071     * is returned and @c opt_arg() refers to the @a argv element found.
00072     */
00073     RETURN_IN_ORDER = 3
00074   };

enum ACE_Get_Opt::OPTION_ARG_MODE
 

Mutually exclusive option argument mode used by long options.

Enumeration values:
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.

00078   {
00079     /// Doesn't take an argument.
00080     NO_ARG = 0,
00081 
00082     /// Requires an argument, same as passing ":" after a short option
00083     /// character in @a optstring.
00084     ARG_REQUIRED = 1,
00085 
00086     /// Argument is optional, same as passing "::" after a short
00087     /// option character in @a optstring.
00088     ARG_OPTIONAL = 2
00089   };


Constructor & Destructor Documentation

ACE_BEGIN_VERSIONED_NAMESPACE_DECL ACE_Get_Opt::ACE_Get_Opt int  argc,
ACE_TCHAR **  argv,
const ACE_TCHAR optstring = ACE_LIB_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::".

Definition at line 94 of file Get_Opt.cpp.

References ACE_LIB_TEXT, ACE_NEW, ACE_TCHAR, ACE_TRACE, ACE_TString, ACE_OS::getenv(), has_colon_, ordering_, REQUIRE_ORDER, and RETURN_IN_ORDER.

00101   : argc_ (argc),
00102     argv_ (argv),
00103     optind (skip),
00104     opterr (report_errors),
00105     optarg (0),
00106     optstring_ (0),
00107     long_only_ (long_only),
00108     has_colon_ (0),
00109     last_option_ (0),
00110     nextchar_ (0),
00111     optopt_ (0),
00112     ordering_ (ordering),
00113     nonopt_start_ (optind),
00114     nonopt_end_ (optind),
00115     long_option_ (0)
00116 {
00117   ACE_TRACE ("ACE_Get_Opt::ACE_Get_Opt");
00118 
00119   ACE_NEW (this->optstring_, ACE_TString (optstring));
00120   ACE_NEW (this->last_option_, ACE_TString (ACE_LIB_TEXT ("")));
00121 
00122   // First check to see if POSIXLY_CORRECT was set.
00123   // Win32 is the only platform capable of wide-char env var.
00124 #if defined (ACE_WIN32)
00125   const ACE_TCHAR *env_check = ACE_LIB_TEXT ("POSIXLY_CORRECT");
00126 #else
00127   const char *env_check = "POSIXLY_CORRECT";
00128 #endif
00129   if (ACE_OS::getenv (env_check) != 0)
00130     this->ordering_ = REQUIRE_ORDER;
00131 
00132   // Now, check to see if any or the following were passed at
00133   // the begining of optstring: '+' same as POSIXLY_CORRECT;
00134   // '-' turns off POSIXLY_CORRECT; or ':' which signifies we
00135   // should return ':' if a parameter is missing for an option.
00136   // We use a loop here, since a combination of "{+|-}:" in any
00137   // order should be legal.
00138   int done  = 0;
00139   int offset = 0;
00140   while (!done)
00141     {
00142       switch (optstring[offset++])
00143         {
00144         case '+':
00145           this->ordering_ = REQUIRE_ORDER;
00146           break;
00147         case '-':
00148           this->ordering_ = RETURN_IN_ORDER;
00149           break;
00150         case ':':
00151           this->has_colon_ = 1;
00152           break;
00153         default:
00154           // Quit as soon as we see something else...
00155           done = 1;
00156           break;
00157         }
00158     }
00159 }

ACE_Get_Opt::~ACE_Get_Opt void   ) 
 

Default dtor.

Definition at line 161 of file Get_Opt.cpp.

References ACE_TRACE, ACE_Array_Base< ACE_Get_Opt_Long_Option * >::get(), last_option_, long_opts_, optstring_, and ACE_Array_Base< ACE_Get_Opt_Long_Option * >::size().

00162 {
00163   ACE_TRACE ("ACE_Get_Opt::~ACE_Get_Opt");
00164 
00165   size_t i = 0;
00166   size_t size = this->long_opts_.size ();
00167   ACE_Get_Opt_Long_Option *option = 0;
00168   for (i = 0; i < size; ++i)
00169     {
00170      int retval = this->long_opts_.get (option, i);
00171       if (retval != 0)
00172         {
00173           // Should never happen.
00174           retval = 0;
00175           continue;
00176         }
00177       if (option)
00178         {
00179           delete option;
00180           option = 0;
00181         }
00182     }
00183   delete this->optstring_;
00184   delete this->last_option_;
00185 }

ACE_Get_Opt::ACE_Get_Opt const ACE_Get_Opt  )  [private]
 


Member Function Documentation

ACE_INLINE int ACE_Get_Opt::argc void   )  const
 

The number of arguments in the internal argv_.

Definition at line 14 of file Get_Opt.inl.

00015 {
00016   return this->argc_;
00017 }

ACE_INLINE ACE_TCHAR ** ACE_Get_Opt::argv void   )  const
 

Accessor for the internal argv_ pointer.

Definition at line 20 of file Get_Opt.inl.

00021 {
00022   return this->argv_;
00023 }

void ACE_Get_Opt::dump void   )  const
 

Dump the state of an object.

Definition at line 603 of file Get_Opt.cpp.

References ACE_BEGIN_DUMP, ACE_DEBUG, ACE_END_DUMP, ACE_LIB_TEXT, ACE_TRACE, ACE_String_Base< CHAR >::c_str(), has_colon_, last_option_, LM_DEBUG, long_only_, long_opts_, nextchar_, optopt_, ordering_, and ACE_Array_Base< ACE_Get_Opt_Long_Option * >::size().

00604 {
00605 #if defined (ACE_HAS_DUMP)
00606   ACE_TRACE ("ACE_Get_Opt::dump");
00607 
00608   ACE_DEBUG ((LM_DEBUG, ACE_BEGIN_DUMP, this));
00609   ACE_DEBUG ((LM_DEBUG, ACE_LIB_TEXT ("\n")
00610               ACE_LIB_TEXT ("opstring_ = %s\n")
00611               ACE_LIB_TEXT ("long_only_ = %d\n")
00612               ACE_LIB_TEXT ("has_colon_ = %d\n")
00613               ACE_LIB_TEXT ("last_option_ = %s\n")
00614               ACE_LIB_TEXT ("nextchar_ = %s\n")
00615               ACE_LIB_TEXT ("optopt_ = %c\n")
00616               ACE_LIB_TEXT ("ordering_ = %d\n"),
00617               this->optstring_->c_str (),
00618               this->long_only_,
00619               this->has_colon_,
00620               this->last_option_->c_str (),
00621               this->nextchar_,
00622               this->optopt_,
00623               this->ordering_));
00624 
00625   // now loop through the
00626   size_t size = this->long_opts_.size ();
00627   for (u_int i = 0; i < size ; ++i)
00628     {
00629       ACE_DEBUG ((LM_DEBUG, ACE_LIB_TEXT ("\n")
00630                   ACE_LIB_TEXT ("long_option name_ = %s\n")
00631                   ACE_LIB_TEXT ("has_arg_ = %d\n")
00632                   ACE_LIB_TEXT ("val_ = %d\n"),
00633                   this->long_opts_[i]->name_,
00634                   this->long_opts_[i]->has_arg_,
00635                   this->long_opts_[i]->val_));
00636     }
00637   ACE_DEBUG ((LM_DEBUG, ACE_END_DUMP));
00638 #endif /* ACE_HAS_DUMP */
00639 }

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

Set last_option.

Definition at line 597 of file Get_Opt.cpp.

References ACE_TString, and last_option_.

00598 {
00599   *this->last_option_ = last_option;
00600 }

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 591 of file Get_Opt.cpp.

References ACE_String_Base< CHAR >::c_str(), and last_option_.

Referenced by long_option_i(), and short_option_i().

00592 {
00593   return this->last_option_->c_str ();
00594 }

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 582 of file Get_Opt.cpp.

References ACE_TRACE, long_option_, and ACE_Get_Opt::ACE_Get_Opt_Long_Option::name_.

Referenced by long_option().

00583 {
00584   ACE_TRACE ("ACE_Get_Opt::long_option (void)");
00585   if (this->long_option_)
00586     return this->long_option_->name_;
00587   return 0;
00588 }

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 481 of file Get_Opt.cpp.

References ACE_ERROR, ACE_ERROR_RETURN, ACE_OS::ace_isalnum(), ACE_LIB_TEXT, ACE_TCHAR, ACE_TRACE, ARG_OPTIONAL, ARG_REQUIRED, LM_ERROR, long_opts_, NO_ARG, opterr, optstring_, ACE_Array_Base< ACE_Get_Opt_Long_Option * >::set(), ACE_Array_Base< ACE_Get_Opt_Long_Option * >::size(), and ACE_OS::strchr().

00484 {
00485   ACE_TRACE ("ACE_Get_Opt::long_option (const ACE_TCHAR *name, int short_option, OPTION_ARG_MODE has_arg)");
00486 
00487   // We only allow valid alpha-numeric characters as short options.
00488   // If short_options is not a valid alpha-numeric, we can still return it
00489   // when the long option is found, but won't allow the caller to pass it on
00490   // the command line (how could they?).  The special case is 0, but since
00491   // we always return it, we let the caller worry about that.
00492 #if defined (_MSC_VER) && (_MSC_VER >= 1300)
00493   // For MSVC 7.x, we need to prevent "illegal" character getting into
00494   // isalnum, otherwise, it will crash the program.
00495   if (short_option > 0 &&
00496       short_option < 256 &&
00497       ACE_OS::ace_isalnum (static_cast<char> (short_option)) != 0)
00498 #else
00499   if (ACE_OS::ace_isalnum (short_option) != 0)
00500 #endif /* _MSC_VER && _MSC_VER >= 1300 */
00501     {
00502       // If the short_option already exists, make sure it matches, otherwise
00503       // add it.
00504       ACE_TCHAR *s = 0;
00505       if ((s = const_cast<ACE_TCHAR*> (
00506                  ACE_OS::strchr (this->optstring_->c_str (),
00507                                  short_option))) != 0)
00508         {
00509           // Short option exists, so verify the argument options
00510           if (s[1] == ':')
00511             {
00512               if (s[2] == ':')
00513                 {
00514                   if (has_arg != ARG_OPTIONAL)
00515                     {
00516                       if (this->opterr)
00517                         ACE_ERROR
00518                           ((LM_ERROR,
00519                             ACE_LIB_TEXT ("Existing short option '%c' takes ")
00520                             ACE_LIB_TEXT ("optional argument; adding %s ")
00521                             ACE_LIB_TEXT ("requires ARG_OPTIONAL\n"),
00522                             short_option, name));
00523                       return -1;
00524                     }
00525                 }
00526               else
00527                 if (has_arg != ARG_REQUIRED)
00528                   {
00529                     if (this->opterr)
00530                       ACE_ERROR
00531                         ((LM_ERROR,
00532                           ACE_LIB_TEXT ("Existing short option '%c' requires ")
00533                           ACE_LIB_TEXT ("an argument; adding %s ")
00534                           ACE_LIB_TEXT ("requires ARG_REQUIRED\n"),
00535                           short_option, name));
00536                     return -1;
00537                   }
00538             }
00539           else if (has_arg != NO_ARG)
00540             {
00541               if (this->opterr)
00542                 ACE_ERROR
00543                   ((LM_ERROR,
00544                     ACE_LIB_TEXT ("Existing short option '%c' does not ")
00545                     ACE_LIB_TEXT ("accept an argument; adding %s ")
00546                     ACE_LIB_TEXT ("requires NO_ARG\n"),
00547                     short_option, name));
00548               return -1;
00549             }
00550         }
00551       else
00552         {
00553           // Didn't find short option, so add it...
00554           *this->optstring_ += (ACE_TCHAR) short_option;
00555           if (has_arg == ARG_REQUIRED)
00556             *this->optstring_ += ACE_LIB_TEXT (":");
00557           else if (has_arg == ARG_OPTIONAL)
00558             *this->optstring_ += ACE_LIB_TEXT ("::");
00559         }
00560     }
00561 
00562   ACE_Get_Opt_Long_Option *option =
00563     new ACE_Get_Opt_Long_Option (name, has_arg, short_option);
00564 
00565   if (!option)
00566     return -1;
00567 
00568   // Add to array
00569   size_t size = this->long_opts_.size ();
00570   if (this->long_opts_.size (size + 1) != 0
00571       || this->long_opts_.set (option, size) != 0)
00572     {
00573       delete option;
00574       ACE_ERROR_RETURN
00575         ((LM_ERROR, ACE_LIB_TEXT ("Could not add long option to array.\n")),
00576          -1);
00577     }
00578   return 0;
00579 }

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 473 of file Get_Opt.cpp.

References ACE_TCHAR, ACE_TRACE, and long_option().

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

int ACE_Get_Opt::long_option_i void   )  [private]
 

Handles long options.

Definition at line 236 of file Get_Opt.cpp.

References ACE_ASSERT, ACE_ERROR, ACE_LIB_TEXT, ACE_TCHAR, ACE_TRACE, ACE_TString, ARG_REQUIRED, ACE_String_Base< CHAR >::find(), ACE_Get_Opt::ACE_Get_Opt_Long_Option::has_arg_, has_colon_, last_option(), LM_ERROR, long_only_, long_option_, long_opts_, ACE_Get_Opt::ACE_Get_Opt_Long_Option::name_, nextchar_, NO_ARG, optarg, opterr, optind, optopt_, optstring_, short_option_i(), ACE_Array_Base< ACE_Get_Opt_Long_Option * >::size(), ACE_OS::strlen(), ACE_OS::strncmp(), and ACE_Get_Opt::ACE_Get_Opt_Long_Option::val_.

Referenced by operator()(), and short_option_i().

00237 {
00238   ACE_TRACE ("ACE_Get_Opt::long_option_i");
00239 
00240   ACE_Get_Opt_Long_Option *p;
00241   ACE_TCHAR *s = this->nextchar_;
00242   int hits = 0;
00243   int exact = 0;
00244   ACE_Get_Opt_Long_Option *pfound = 0;
00245   int indfound = 0;
00246 
00247   // Advance to the end of the long option name so we can use
00248   // it to get the length for a string compare.
00249   while (*s && *s != '=')
00250     s++;
00251 
00252   size_t len = s - this->nextchar_;
00253   // set last_option_ to nextchar_, up to the '='.
00254   this->last_option (ACE_TString (this->nextchar_, len));
00255 
00256   size_t size = this->long_opts_.size ();
00257   u_int option_index = 0;
00258   for (option_index = 0; option_index < size ; option_index++)
00259     {
00260       p = this->long_opts_[option_index];
00261       ACE_ASSERT (p);
00262 
00263       if (!ACE_OS::strncmp (p->name_, this->nextchar_, len))
00264         {
00265           // Got at least a partial match.
00266           pfound = p;
00267           indfound = option_index;
00268           hits += 1;
00269           if (len == ACE_OS::strlen(p->name_))
00270             {
00271               // And in fact, it's an exact match, so let's use it.
00272               exact = 1;
00273               break;
00274             }
00275         }
00276     }
00277 
00278   if ((hits > 1) && !exact)
00279     {
00280       // Great, we found a match, but unfortunately we found more than
00281       // one and it wasn't exact.
00282       if (this->opterr)
00283         ACE_ERROR ((LM_ERROR,
00284                     ACE_LIB_TEXT ("%s: option `%s' is ambiguous\n"),
00285                     this->argv_[0], this->argv_[this->optind]));
00286       this->nextchar_ = 0;
00287       this->optind++;
00288       return '?';
00289     }
00290 
00291   if (pfound != 0)
00292     {
00293       // Okay, we found a good one (either a single hit or an exact match).
00294       option_index = indfound;
00295       this->optind++;
00296       if (*s)
00297         {
00298           // s must point to '=' which means there's an argument (well
00299           // close enough).
00300           if (pfound->has_arg_ != NO_ARG)
00301             // Good, we want an argument and here it is.
00302             this->optarg = ++s;
00303           else
00304             {
00305               // Whoops, we've got what looks like an argument, but we
00306               // don't want one.
00307               if (this->opterr)
00308                   ACE_ERROR
00309                     ((LM_ERROR,
00310                       ACE_LIB_TEXT ("%s: long option `--%s' doesn't allow ")
00311                       ACE_LIB_TEXT ("an argument\n"),
00312                       this->argv_[0], pfound->name_));
00313               // The spec doesn't cover this, so we keep going and the program
00314               // doesn't know we ignored an argument if opt_err is off!!!
00315             }
00316         }
00317       else if (pfound->has_arg_ == ARG_REQUIRED)
00318         {
00319           // s didn't help us, but we need an argument. Note that
00320           // optional arguments for long options must use the "=" syntax,
00321           // so we won't get here in that case.
00322           if (this->optind < this->argc_)
00323             // We still have some elements left, so use the next one.
00324             this->optarg = this->argv_[this->optind++];
00325           else
00326             {
00327               // All out of elements, so we have to punt...
00328               if (this->opterr)
00329                 ACE_ERROR ((LM_ERROR,
00330                             ACE_LIB_TEXT ("%s: long option '--%s' requires ")
00331                             ACE_LIB_TEXT ("an argument\n"),
00332                             this->argv_[0], pfound->name_));
00333               this->nextchar_ = 0;
00334               this->optopt_ = pfound->val_;   // Remember matching short equiv
00335               return this->has_colon_ ? ':' : '?';
00336             }
00337         }
00338       this->nextchar_ = 0;
00339       this->long_option_ = pfound;
00340       // Since val_ has to be either a valid short option or 0, this works
00341       // great.  If the user really wants to know if a long option was passed.
00342       this->optopt_ = pfound->val_;
00343       return pfound->val_;
00344     }
00345   if (!this->long_only_ || this->argv_[this->optind][1] == '-'
00346       || this->optstring_->find (*this->nextchar_) == ACE_TString::npos)
00347     {
00348       // Okay, we couldn't find a long option.  If it isn't long_only (which
00349       // means try the long first, and if not found try the short) or a long
00350       // signature was passed, e.g. "--", or it's not a short (not sure when
00351       // this could happen) it's an error.
00352       if (this->opterr)
00353         ACE_ERROR ((LM_ERROR,
00354                     ACE_LIB_TEXT ("%s: illegal long option '--%s'\n"),
00355                     this->argv_[0], this->nextchar_));
00356       this->nextchar_ = 0;
00357       this->optind++;
00358       return '?';
00359     }
00360   return this->short_option_i ();
00361 }

int ACE_Get_Opt::nextchar_i void   )  [private]
 

Updates nextchar_.

Definition at line 188 of file Get_Opt.cpp.

References ACE_TRACE, long_opts_, nextchar_, optarg, optind, ordering_, permute(), PERMUTE_ARGS, REQUIRE_ORDER, and ACE_Array_Base< ACE_Get_Opt_Long_Option * >::size().

Referenced by operator()().

00189 {
00190   ACE_TRACE ("ACE_Get_Opt::nextchar_i");
00191 
00192   if (this->ordering_ == PERMUTE_ARGS)
00193     if (this->permute () == EOF)
00194       return EOF;
00195 
00196   // Update scanning pointer.
00197   if (this->optind >= this->argc_)
00198     {
00199       // We're done...
00200       this->nextchar_ = 0;
00201       return EOF;
00202     }
00203   else if (*(this->nextchar_ = this->argv_[this->optind]) != '-'
00204             || this->nextchar_[1] == '\0')
00205     {
00206       // We didn't get an option.
00207 
00208       if (this->ordering_ == REQUIRE_ORDER
00209           || this->ordering_ == PERMUTE_ARGS)
00210         // If we permuted or require the options to be in order, we're done.
00211         return EOF;
00212 
00213       // It must be RETURN_IN_ORDER...
00214       this->optarg = this->argv_[this->optind++];
00215       this->nextchar_ = 0;
00216       return 1;
00217     }
00218   else if (this->nextchar_[1] != 0
00219            && *++this->nextchar_ == '-'
00220            && this->nextchar_[1] == 0)
00221     {
00222       // Found "--" so we're done...
00223       ++this->optind;
00224       this->nextchar_ = 0;
00225       return EOF;
00226     }
00227 
00228   // If it's a long option, and we allow long options advance nextchar_.
00229   if (*this->nextchar_ == '-' && this->long_opts_.size () != 0)
00230     this->nextchar_++;
00231 
00232   return 0;
00233 }

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 441 of file Get_Opt.cpp.

References ACE_TRACE, long_only_, long_option_, long_option_i(), nextchar_, nextchar_i(), optarg, optind, and short_option_i().

00442 {
00443   ACE_TRACE ("ACE_Get_Opt_Long::operator");
00444 
00445   // First of all, make sure we reinitialize any pointers..
00446   this->optarg = 0;
00447   this->long_option_ = 0;
00448 
00449   if (this->argv_ == 0)
00450     {
00451       // It can happen, e.g., on VxWorks.
00452       this->optind = 0;
00453       return -1;
00454     }
00455 
00456   // We check this because we can string short options together if the
00457   // preceding one doesn't take an argument.
00458   if (this->nextchar_ == 0 || *this->nextchar_ == '\0')
00459     {
00460       int retval = this->nextchar_i ();
00461       if (retval != 0)
00462         return retval;
00463     }
00464 
00465   if (((this->argv_[this->optind][0] == '-')
00466        && (this->argv_[this->optind][1] == '-')) || this->long_only_)
00467     return this->long_option_i ();
00468 
00469   return this->short_option_i ();
00470 }

ACE_Get_Opt& ACE_Get_Opt::operator= const ACE_Get_Opt  )  [private]
 

ACE_INLINE ACE_TCHAR * ACE_Get_Opt::opt_arg void   )  const
 

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.

References optarg.

00027 {
00028   return this->optarg;
00029 }

ACE_INLINE int & ACE_Get_Opt::opt_ind void   ) 
 

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.

References optind.

00039 {
00040   return this->optind;
00041 }

ACE_INLINE int ACE_Get_Opt::opt_opt void   ) 
 

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.

References optopt_.

00033 {
00034   return this->optopt_;
00035 }

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 719 of file Get_Opt.cpp.

References ACE_String_Base< CHAR >::c_str(), and optstring_.

00720 {
00721   return this->optstring_->c_str ();
00722 }

int ACE_Get_Opt::permute void   )  [private]
 

Handles reordering -elements.

Definition at line 678 of file Get_Opt.cpp.

References ACE_LIB_TEXT, ACE_TRACE, nonopt_end_, nonopt_start_, optind, permute_args(), and ACE_OS::strcmp().

Referenced by nextchar_i().

00679 {
00680   ACE_TRACE ("ACE_Get_Opt::permute");
00681 
00682   if (this->nonopt_start_ != this->nonopt_end_
00683       && this->nonopt_start_ != this->optind)
00684     this->permute_args ();
00685 
00686   this->nonopt_start_ = this->optind;
00687 
00688   // Skip over args untill we find the next option.
00689   while (this->optind < this->argc_
00690          && (this->argv_[this->optind][0] != '-'
00691              || this->argv_[this->optind][1] == '\0'))
00692     this->optind++;
00693 
00694   // Got an option, so mark this as the end of the non options.
00695   this->nonopt_end_ = this->optind;
00696 
00697   if (this->optind != this->argc_
00698       && ACE_OS::strcmp (this->argv_[this->optind],
00699                          ACE_LIB_TEXT ("--")) == 0)
00700     {
00701       // We found the marker for the end of the options.
00702       this->optind++;
00703 
00704       if (this->nonopt_start_ != this->nonopt_end_
00705           && this->nonopt_end_ != this->optind)
00706         this->permute_args ();
00707     }
00708 
00709   if (this->optind == this->argc_)
00710     {
00711       if (this->nonopt_start_ != this->nonopt_end_)
00712         this->optind = this->nonopt_start_;
00713       return EOF;
00714     }
00715   return 0;
00716 }

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

Definition at line 642 of file Get_Opt.cpp.

References ACE_TCHAR, ACE_TRACE, ACE::gcd(), nonopt_end_, nonopt_start_, and optind.

Referenced by permute().

00643 {
00644   ACE_TRACE ("ACE_Get_Opt::permute_args");
00645 
00646   u_long cyclelen, i, j, ncycle, nnonopts, nopts;
00647   u_long opt_end = this->optind;
00648   int cstart, pos = 0;
00649   ACE_TCHAR *swap = 0;
00650 
00651   nnonopts = this->nonopt_end_ - this->nonopt_start_;
00652   nopts = opt_end - this->nonopt_end_;
00653   ncycle = ACE::gcd (nnonopts, nopts);
00654   cyclelen = (opt_end - this->nonopt_start_) / ncycle;
00655 
00656   this->optind = this->optind - nnonopts;
00657 
00658   for (i = 0; i < ncycle; i++)
00659     {
00660       cstart = this->nonopt_end_ + i;
00661       pos = cstart;
00662       for (j = 0; j < cyclelen; j++)
00663         {
00664           if (pos >= this->nonopt_end_)
00665             pos -= nnonopts;
00666           else
00667             pos += nopts;
00668           swap = this->argv_[pos];
00669 
00670           ((ACE_TCHAR **)this->argv_)[pos] = argv_[cstart];
00671 
00672           ((ACE_TCHAR **)this->argv_)[cstart] = swap;
00673         }
00674     }
00675 }

int ACE_Get_Opt::short_option_i void   )  [private]
 

Handles short options.

Definition at line 364 of file Get_Opt.cpp.

References ACE_ERROR, ACE_LIB_TEXT, ACE_TCHAR, ACE_TRACE, has_colon_, last_option(), LM_ERROR, long_option_i(), nextchar_, optarg, opterr, optind, optopt_, and ACE_OS::strchr().

Referenced by long_option_i(), and operator()().

00365 {
00366   ACE_TRACE ("ACE_Get_Opt::short_option_i");
00367 
00368   /* Look at and handle the next option-character.  */
00369   ACE_TCHAR opt = *this->nextchar_++;
00370   // Set last_option_ to opt
00371   this->last_option (opt);
00372 
00373   ACE_TCHAR *oli = 0;
00374   oli =
00375     const_cast<ACE_TCHAR*> (ACE_OS::strchr (this->optstring_->c_str (), opt));
00376 
00377   /* Increment `optind' when we start to process its last character.  */
00378   if (*this->nextchar_ == '\0')
00379     ++this->optind;
00380 
00381   if (oli == 0 || opt == ':')
00382     {
00383       if (this->opterr)
00384         ACE_ERROR ((LM_ERROR,
00385                     ACE_LIB_TEXT ("%s: illegal short option -- %c\n"),
00386                     this->argv_[0], opt));
00387       return '?';
00388     }
00389   if (opt == 'W' && oli[1] == ';')
00390     {
00391       if (this->nextchar_[0] == 0)
00392         this->nextchar_ = this->argv_[this->optind];
00393       return long_option_i ();
00394     }
00395   this->optopt_ = oli[0];      // Remember the option that matched
00396   if (oli[1] == ':')
00397     {
00398       if (oli[2] == ':')
00399         {
00400           // Takes an optional argument, and since short option args must
00401           // must follow directly in the same argument, a NULL nextchar_
00402           // means we didn't get one.
00403           if (*this->nextchar_ != '\0')
00404             {
00405               this->optarg = this->nextchar_;
00406               this->optind++;
00407             }
00408           else
00409             this->optarg = 0;
00410           this->nextchar_ = 0;
00411         }
00412       else
00413         {
00414           // Takes a required argument.
00415           if (*this->nextchar_ != '\0')
00416             {
00417               // Found argument in same argv-element.
00418               this->optarg = this->nextchar_;
00419               this->optind++;
00420             }
00421           else if (this->optind == this->argc_)
00422             {
00423               // Ran out of arguments before finding required argument.
00424               if (this->opterr)
00425                 ACE_ERROR ((LM_ERROR,
00426                             ACE_LIB_TEXT ("%s: short option requires ")
00427                             ACE_LIB_TEXT ("an argument -- %c\n"),
00428                             this->argv_[0], opt));
00429               opt = this->has_colon_ ? ':' : '?';
00430             }
00431           else
00432             // Use the next argv-element as the argument.
00433             this->optarg = this->argv_[this->optind++];
00434           this->nextchar_ = 0;
00435         }
00436     }
00437   return opt;
00438 }


Member Data Documentation

ACE_Get_Opt::ACE_ALLOC_HOOK_DECLARE [private]
 

Declare the dynamic allocation hooks.

Definition at line 463 of file Get_Opt.h.

int ACE_Get_Opt::argc_
 

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 319 of file Get_Opt.h.

ACE_TCHAR** ACE_Get_Opt::argv_
 

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 328 of file Get_Opt.h.

int ACE_Get_Opt::has_colon_ [private]
 

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

Definition at line 427 of file Get_Opt.h.

Referenced by ACE_Get_Opt(), dump(), long_option_i(), and short_option_i().

ACE_TString* ACE_Get_Opt::last_option_ [private]
 

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 431 of file Get_Opt.h.

Referenced by dump(), last_option(), and ~ACE_Get_Opt().

int ACE_Get_Opt::long_only_ [private]
 

Treat all options as long options.

Definition at line 422 of file Get_Opt.h.

Referenced by dump(), long_option_i(), and operator()().

ACE_Get_Opt_Long_Option* ACE_Get_Opt::long_option_ [private]
 

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

Definition at line 457 of file Get_Opt.h.

Referenced by long_option(), long_option_i(), and operator()().

ACE_Array<ACE_Get_Opt_Long_Option*> ACE_Get_Opt::long_opts_ [private]
 

Array of long options.

Definition at line 460 of file Get_Opt.h.

Referenced by dump(), long_option(), long_option_i(), nextchar_i(), and ~ACE_Get_Opt().

ACE_TCHAR* ACE_Get_Opt::nextchar_ [private]
 

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

Definition at line 440 of file Get_Opt.h.

Referenced by dump(), long_option_i(), nextchar_i(), operator()(), and short_option_i().

int ACE_Get_Opt::nonopt_end_ [private]
 

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

Definition at line 454 of file Get_Opt.h.

Referenced by permute(), and permute_args().

int ACE_Get_Opt::nonopt_start_ [private]
 

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

Definition at line 450 of file Get_Opt.h.

Referenced by permute(), and permute_args().

ACE_TCHAR* ACE_Get_Opt::optarg
 

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 357 of file Get_Opt.h.

Referenced by long_option_i(), nextchar_i(), operator()(), opt_arg(), and short_option_i().

int ACE_Get_Opt::opterr
 

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 347 of file Get_Opt.h.

Referenced by long_option(), long_option_i(), and short_option_i().

int ACE_Get_Opt::optind
 

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 337 of file Get_Opt.h.

Referenced by long_option_i(), nextchar_i(), operator()(), opt_ind(), permute(), permute_args(), and short_option_i().

int ACE_Get_Opt::optopt_ [private]
 

Most recently matched short option character.

Definition at line 443 of file Get_Opt.h.

Referenced by dump(), long_option_i(), opt_opt(), and short_option_i().

ACE_TString* ACE_Get_Opt::optstring_ [private]
 

Holds the option string.

Definition at line 419 of file Get_Opt.h.

Referenced by long_option(), long_option_i(), optstring(), and ~ACE_Get_Opt().

int ACE_Get_Opt::ordering_ [private]
 

Keeps track of ordering mode (default ).

Definition at line 446 of file Get_Opt.h.

Referenced by ACE_Get_Opt(), dump(), and nextchar_i().


The documentation for this class was generated from the following files:
Generated on Thu Nov 9 11:22:37 2006 for ACE by doxygen 1.3.6