3 //==========================================================================
7 * @author Douglas C. Schmidt <d.schmidt@vanderbilt.edu>
8 * @author Don Hinton <dhinton@gmx.net> (added long option support)
10 //==========================================================================
14 #include /**/ "ace/pre.h"
16 #include "ace/SStringfwd.h"
17 #include "ace/Containers.h"
19 #if !defined (ACE_LACKS_PRAGMA_ONCE)
21 #endif /* ACE_LACKS_PRAGMA_ONCE */
27 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
30 * These definitions are for backward compatibility with previous versions.
37 * @brief Iterator for parsing command-line arguments.
39 * This is a C++ wrapper for getopt(3c) and getopt_long(3c).
41 class ACE_Export ACE_Get_Opt
44 /// Mutually exclusive ordering values.
48 * REQUIRE_ORDER means that processing stops and @c EOF is
49 * returned as soon as a non-option argument is found. @c opt_ind()
50 * will return the index of the next @a argv element so the program
51 * can continue processing the rest of the @a argv elements.
56 * PERMUTE_ARGS means the @a argv elements are reordered dynamically
57 * (permuted) so that all options appear first. When the elements are
58 * permuted, the order of the options and the following arguments are
59 * maintained. When the last option has been processed, @c EOF is
60 * returned and @c opt_ind() returns the index into the next non-option
66 * RETURN_IN_ORDER means each @a argv element is processed in the
67 * order is it seen. If the element is not recognized as an option, '1'
68 * is returned and @c opt_arg() refers to the @a argv element found.
73 /// Mutually exclusive option argument mode used by long options.
76 /// Doesn't take an argument.
79 /// Requires an argument, same as passing ":" after a short option
80 /// character in @a optstring.
83 /// Argument is optional, same as passing "::" after a short
84 /// option character in @a optstring.
89 * Constructor initializes the command line to be parsed. All information
90 * for parsing must be supplied to this constructor.
92 * @param argc The number of @a argv elements to parse.
93 * @param argv Command line tokens, such as would be passed
95 * @param optstring Nul-terminated string containing the legitimate
96 * short option characters. A single colon ":"
97 * following an option character means the option
98 * requires an argument. A double colon "::" following
99 * an option character means the argument is optional.
100 * The argument is taken from the rest of the current
101 * @a argv element, or from the following @a argv
102 * element (only valid for required arguments;
103 * optional arguments must always reside in the same
104 * @a argv element). The argument value, if any is
105 * returned by the @c opt_arg() method.
106 * @a optstring can be extended by adding long options
107 * with corresponding short options via the
108 * @c long_option() method. If the short option
109 * already appears in @a optstring, the argument
110 * characteristics must match, otherwise it is added.
111 * See @c long_option() for more information.
112 * If 'W', followed by a semi-colon ';' appears in
113 * @a optstring, then any time a 'W' appears on the
114 * command line, the following argument is treated as
115 * a long option. For example, if the command line
116 * contains "program -W foo", "foo" is treated as a
117 * long option, that is, as if "program --foo" had
119 * The following characters can appear in @a optstring
120 * before any option characters, with the described
122 * - '+' changes the @a ordering to @a REQUIRE_ORDER.
123 * - '-' changes the @a ordering to @a RETURN_IN_ORDER.
124 * - ':' changes the return value from @c operator()
125 * and get_opt() from '?' to ':' when an option
126 * requires an argument but none is specified.
128 * @param skip_args Optional (default 1). The specified number of
129 * initial elements in @a argv are skipped before
130 * parsing begins. Thus, the default prevents
131 * @a argv[0] (usually the command name) from being
132 * parsed. @a argc includes all @a argv elements,
133 * including any skipped elements.
134 * @param report_errors Optional, if non-zero then parsing errors cause
135 * an error message to be displayed from the
136 * @c operator() method before it returns. The
137 * error message is suppressed if this argument is 0.
138 * This setting also controls whether or not an error
139 * message is displayed in @c long_option() encounters
141 * @param ordering Optional (default is @c PERMUTE_ARGS); determines
142 * how the @a argv elements are processed. This argument
143 * is overridden by two factors:
144 * -# The @c POSIXLY_CORRECT environment variable. If
145 * this environment variable is set, the ordering
146 * is changed to @c REQUIRE_ORDER.
147 * -# Leading characters in @a optstring (see above).
148 * Any leading ordering characters override both
149 * the @a ordering argument and any effect of the
150 * @c POSIXLY_CORRECT environment variable.
151 * @param long_only Optional. If non-zero, then long options can be
152 * specified using a single '-' on the command line.
153 * If the token is not a long option, it is processed
154 * as usual, that is, as a short option or set of
157 * Multiple short options can be combined as long as only the last
158 * one can takes an argument. For example, if @a optstring is defined as
159 * @c "abc:" or @c "abc::" then the command line @e "program -abcxxx" short
160 * options @e a, @e b, and @e c are found with @e "xxx" as the argument for
162 * However, if the command line is specified as @e "program -acb" only
163 * options @e a and @e c are found with @e "b" as the argument for @e c.
164 * Also, for options with optional arguments, that is, those followed by
165 * "::", the argument must be in the same @a argv element, so "program -abc
166 * xxx" will only find "xxx" as the argument for @e c if @a optstring is
167 * specified as @c "abc:" not @c "abc::".
169 #ifndef ACE_USES_WCHAR
170 ACE_Get_Opt (int argc
,
172 const ACE_TCHAR
*optstring
= ACE_TEXT (""),
174 int report_errors
= 0,
175 int ordering
= PERMUTE_ARGS
,
180 void ACE_Get_Opt_Init (const ACE_TCHAR
*optstring
);
182 ACE_Get_Opt (int argc
,
184 const ACE_TCHAR
*optstring
= ACE_TEXT (""),
186 int report_errors
= 0,
187 int ordering
= PERMUTE_ARGS
,
189 ACE_Get_Opt (int argc
,
191 const char *optstring
,
193 int report_errors
= 0,
194 int ordering
= PERMUTE_ARGS
,
201 * Scan elements of @a argv (whose length is @a argc) for short option
202 * characters given in @a optstring or long options (with no short
203 * option equivalents).
205 * If an element of @a argv starts with '-', and is not exactly "-"
206 * or "--", then it is a short option element. The characters of this
207 * element (aside from the initial '-') are option characters. If
208 * it starts with "--" followed by other characters it is treated as
209 * a long option. If @c operator() is called repeatedly, it returns
210 * each of the option characters from each of the option elements.
212 * @return The parsed option character. The following characters have
213 * special significance.
214 * @retval 0 A long option was found
215 * @retval '\?' Either an unknown option character was found, or the
216 * option is known but requires an argument, none was
217 * specified, and @a optstring did not contain a leading
219 * @retval ':' A known option character was found but it requires an
220 * argument and none was supplied, and the first character
221 * of @a optstring was a colon. @c opt_opt() indicates
222 * which option was specified.
223 * @retval '1' @c RETURN_IN_ORDER was specified and a non-option argument
225 * @retval EOF No more option characters were found. @c opt_ind() will
226 * return the index in @a argv of the first @a argv element
227 * that is not an option. If @c PERMUTE_ARGS was
228 * specified, the @a argv elements have been permuted so that
229 * those that are not options now come last.
231 * @note The standards are unclear with respect to the conditions under
232 * which '?' and ':' are returned, so we scan the initial characters of
233 * @a optstring up unto the first short option character for '+', '-',
234 * and ':' in order to determine ordering and missing argument behavior.
239 * For communication from @c operator() to the caller. When
240 * @c operator() finds an option that takes an argument, the argument
241 * value is returned from this method, otherwise it returns 0.
243 ACE_TCHAR
*opt_arg () const;
246 * Returns the most recently matched option character. Especially
247 * useful when operator() returns ':' for an unspecified argument
248 * that's required, since this allows the caller to learn what option
249 * was specified without its required argument.
254 * Index in @a argv of the next element to be scanned. This is used
255 * for communication to and from the caller and for communication
256 * between successive calls to @c operator(). On entry to
257 * @c operator(), zero means this is the first call; initialize.
259 * When @c operator() returns @c EOF, this is the index of the first of
260 * the non-option elements that the caller should itself scan.
262 * Otherwise, @c opt_ind() communicates from one call to the next how
263 * much of @a argv has been scanned so far.
267 /// Adds a long option with no corresponding short option.
269 * If the @a name option is seen, @c operator() returns 0.
271 * @param name The long option to add.
272 * @param has_arg Defines the argument requirements for
276 * @retval -1 The long option can not be added.
278 int long_option (const ACE_TCHAR
*name
,
279 OPTION_ARG_MODE has_arg
= NO_ARG
);
281 /// Adds a long option with a corresponding short option.
283 * @param name The long option to add.
284 * @param short_option A character, the short option that corresponds
286 * @param has_arg Defines the argument requirements for
287 * the new option. If the short option has already
288 * been supplied in the @a optstring, @a has_arg
289 * must match or an error is returned; otherwise, the
290 * new short option is added to the @a optstring.
293 * @retval -1 The long option can not be added.
295 int long_option (const ACE_TCHAR
*name
,
297 OPTION_ARG_MODE has_arg
= NO_ARG
);
299 /// Returns the name of the long option found on the last call to
300 /// @c operator() or 0 if none was found.
301 const ACE_TCHAR
*long_option () const;
303 /// The number of arguments in the internal @c argv_.
306 /// Accessor for the internal @c argv_ pointer.
307 ACE_TCHAR
**argv () const;
309 /// Accessor for the @c last_option that was processed. This allows
310 /// applications to know if the found option was a short or long
311 /// option, and is especially useful in cases where it was invalid
312 /// and the caller wants to print out the invalid value.
313 const ACE_TCHAR
*last_option () const;
315 /// Dump the state of an object.
318 /// Return the @a optstring. This is handy to verify that calls to
319 /// long_option added short options as expected.
320 const ACE_TCHAR
*optstring () const;
324 * The following five data members should be private, but that
325 * would break backwards compatibility. However, we recommend not
326 * writing code that uses these fields directly.
329 /// Holds the @a argc count.
331 * @deprecated This is public for backwards compatibility only.
332 * It will be made private in a release of ACE past 5.3. Do not
333 * write code that relies on this member being public; use the
334 * @c argc() accessor method instead.
338 /// Holds the @a argv pointer.
340 * @deprecated This is public for backwards compatibility only.
341 * It will be made private in a release of ACE past 5.3. Do not
342 * write code that relies on this member being public; use the
343 * @c argv() accessor method instead.
347 /// Index in @c argv_ of the next element to be scanned.
349 * @deprecated This is public for backwards compatibility only.
350 * It will be made private in a release of ACE past 5.3. Do not
351 * write code that relies on this member being public; use the
352 * @c opt_ind() accessor method instead.
356 /// Callers store zero here to inhibit the error message for
357 /// unrecognized options.
359 * @deprecated This is public for backwards compatibility only.
360 * It will be made private in a release of ACE past 5.3. Do not
361 * write code that relies on this member being public; use the
362 * @a report_errors argument to this class's constructor instead.
366 /// Points to the option argument when one is found on last call to
369 * @deprecated This is public for backwards compatibility only.
370 * It will be made private in a release of ACE past 5.3. Do not
371 * write code that relies on this member being public; use the
372 * @c opt_arg() accessor method instead.
378 * @class ACE_Get_Opt_Long_Option This class is for internal use
379 * in the ACE_Get_Opt class, and is inaccessible to users.
381 class ACE_Get_Opt_Long_Option
385 ACE_Get_Opt_Long_Option (const ACE_TCHAR
*name
,
390 ~ACE_Get_Opt_Long_Option ();
392 bool operator < (const ACE_Get_Opt_Long_Option
&rhs
);
394 /// Long option name.
395 const ACE_TCHAR
*name_
;
397 /// Contains value for <OPTION_ARG_MODE>.
400 /// Contains a valid short option character or zero if it doesn't
401 /// have a corresponding short option. It can also contain a
402 /// non-printable value that cannot be passed to <optstring> but
403 /// will be returned by <operator()>. This is handy for
404 /// simplifying long option handling, see tests/Get_Opt_Test.cpp
405 /// for an example of this technique.
408 ACE_ALLOC_HOOK_DECLARE
;
411 /// Updates nextchar_.
414 /// Handles long options.
415 int long_option_i ();
417 /// Handles short options.
418 int short_option_i ();
420 /// If permuting args, this functions manages the nonopt_start_ and
421 /// nonopt_end_ indexes and makes calls to permute to actually
422 /// reorder the <argv>-elements.
423 void permute_args ();
425 /// Handles reordering <argv>-elements.
429 void last_option (const ACE_TString
&s
);
431 ACE_Get_Opt (const ACE_Get_Opt
&) = delete;
432 ACE_Get_Opt
&operator= (const ACE_Get_Opt
&) = delete;
433 ACE_Get_Opt (ACE_Get_Opt
&&) = delete;
434 ACE_Get_Opt
&operator= (ACE_Get_Opt
&&) = delete;
437 /// Holds the option string.
438 ACE_TString
*optstring_
;
440 /// Treat all options as long options.
443 /// Keeps track of whether or not a colon was passed in <optstring>.
444 /// This is used to determine the return value when required
445 /// arguments are missing.
448 /// This is the last option, short or long, that was processed. This
449 /// is handy to have in cases where the option passed was invalid.
450 ACE_TString
*last_option_
;
453 * The next char to be scanned in the option-element in which the
454 * last option character we returned was found. This allows us to
455 * pick up the scan where we left off *
456 * If this is zero, or a null string, it means resume the scan
457 * by advancing to the next <argv>-element.
459 ACE_TCHAR
*nextchar_
;
461 /// Most recently matched short option character.
464 /// Keeps track of ordering mode (default <PERMUTE_ARGS>).
467 /// Index of the first non-option <argv>-element found (only valid
471 /// Index of the <argv>-element following the last non-option element
472 /// (only valid when permuting).
475 /// Points to the long_option found on last call to <operator()>.
476 ACE_Get_Opt_Long_Option
*long_option_
;
478 /// Array of long options.
479 ACE_Array
<ACE_Get_Opt_Long_Option
*> long_opts_
;
481 /// Declare the dynamic allocation hooks.
482 ACE_ALLOC_HOOK_DECLARE
;
485 ACE_END_VERSIONED_NAMESPACE_DECL
487 #if defined (__ACE_INLINE__)
488 #include "ace/Get_Opt.inl"
489 #endif /* __ACE_INLINE__ */
491 #include /**/ "ace/post.h"
492 #endif /* ACE_GET_OPT_H */