Welcome

首页 / 软件开发 / C++ / 如何使用ACE_Get_Opt解析命令行

如何使用ACE_Get_Opt解析命令行2015-05-11当我们用C++开发一些C++控制台小工具时,会需要一些用户输入的参数来决定程序如何工作和执行,而用户输入参数的方式大部分都是采用命令行参数的方式。

比如上一篇文章 玩转Windows服务系列——命令行管理Windows服务 中介绍的sc和net工具。

既然命令行参数这么普遍也这么有用,那么就有必要学习一下如何解析命令行参数。

如何解析命令行参数

那么命令行参数要如何解析呢。

最最最笨的办法就是每次程序中需要解析命令行参数时,就写一堆解析的代码,但是这样的效率其实很低的,不如使用开源库中的帮助类来解析。

我所了解的开源库的帮助类有

ACE库的ACE_Get_Opt

boost的Program_options

ACE_Get_Opt类的主要使用方法介绍

那么我们主要看一下ACE_Get_Opt类

先看它的构造方法

CollapseACE_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);
下面是代码中的注释

Collapse/*** Constructor initializes the command line to be parsed. All information* for parsing must be supplied to this constructor.** @param argcThe number of @a argv elements to parse.* @param argvCommand line tokens, such as would be passed*to @c main().* @param 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*@a argv element, or from the following @a argv*element (only valid for required arguments;*optional arguments must always reside in the same*@a argv element). The argument value, if any is*returned by the @c opt_arg() method.*@a optstring can be extended by adding long options*with corresponding short options via the*@c long_option() method.If the short option*already appears in @a optstring, the argument*characteristics must match, otherwise it is added.*See @c long_option() for more information.*If "W", followed by a semi-colon ";" appears in*@a 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 @a optstring*before any option characters, with the described*effect:*- "+" changes the @a ordering to @a REQUIRE_ORDER.*- "-" changes the @a ordering to @a RETURN_IN_ORDER.*- ":" changes the return value from @c operator()*and get_opt() from "?" to ":" when an option*requires an argument but none is specified.** @param skip_args Optional (default 1). The specified number of*initial elements in @a argv are skipped before*parsing begins. Thus, the default prevents*@a argv[0] (usually the command name) from being*parsed. @a argc includes all @a argv elements,*including any skipped elements.* @param report_errors Optional, if non-zero then parsing errors cause*an error message to be displayed from the*@c 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 @c long_option() encounters*an error.* @param orderingOptional (default is @c PERMUTE_ARGS); determines*how the @a argv elements are processed. This argument*is overridden by two factors:*-# The @c POSIXLY_CORRECT environment variable. If* this environment variable is set, the ordering* is changed to @c REQUIRE_ORDER.*-# Leading characters in @a optstring (see above).* Any leading ordering characters override both* the @a ordering argument and any effect of the* @c POSIXLY_CORRECT environment variable.* @param 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 @a optstring is defined as* @c "abc:" or @c "abc::" then the command line @e "program -abcxxx" short* options @e a, @e b, and @e c are found with @e "xxx" as the argument for* @e c.* However, if the command line is specified as @e "program -acb" only* options @e a and @e c are found with @e "b" as the argument for @e c.* Also, for options with optional arguments, that is, those followed by* "::", the argument must be in the same @a argv element, so "program -abc* xxx" will only find "xxx" as the argument for @e c if @a optstring is* specified as @c "abc:" not @c "abc::".*/