ACE_Get_Opt是一种解析命令行参数选项的迭代器。

  1:构造方法

  ACE_Get_Opt需要引用头文件,#include "ace/Get_Opt.h"。

ACE_Get_Opt (int argc,
ACE_TCHAR **argv,
const ACE_TCHAR *optstring = ACE_TEXT (""),
int skip_args = ,
int report_errors = ,
int ordering = PERMUTE_ARGS,
int long_only = );

  

  argc、argv为程序入口函数main方法的参数,也就是程序的命令行。

  optstring 为指定的参数选项,并且是一个字符作为一个选项,主要包含三种形式

    • 单纯的一个字符选项,比如 s,表示此选项后面不能添加选项的参数
    • 一个字符选项后跟一个冒号,比如 s:,表示此选项后面会有一个参数
    • 一个字符后面跟两个冒号,比如 s::,表示此选项后面即可以有参数也可以无参数

  skip_args 表示从argv的第几个元素开始,默认为1,一般情况下argv[0]为程序的path

  report_errors遇到不识别的参数时是否提示错误

  long_only表示是否只包含字符串的选项参数。

  下面解释一下 字符选项和字符串选项,也就是 short option 和 long option.

  • short option 以 字符’-’开始,比如 -s
  • long option 以两个字符’-’开始,比入 --server

  这是在默认的情况下,也就是long_only = 0的情况下。而当long_only不等于0的时候,就可以通过-server来表示long option了。

  2:操作

  遍历命令行取出参数项依赖2个基本操作

   /*
* Scan elements of @a argv (whose length is @a argc) for short option
* characters given in @a optstring or long options (with no short
* option equivalents).
*/
int operator () (void); /*
* For communication from @c operator() to the caller. When
* @c operator() finds an option that takes an argument, the argument
* value is returned from this method, otherwise it returns 0.
*/
ACE_TCHAR *opt_arg (void) const;

  int operator () (void);用于遍历参数项

  opt_arg用于取出参数项的值。

3:示例程序

  命令行参数:ACE_TEST.exe -e 2000 -i 1000

#include "ace/Get_Opt.h"
#include "ace/OS_NS_unistd.h"
#include "ace/OS_NS_sys_time.h" #include <iostream>
const ACE_Time_Value max_interval(*); int main(int argc, char **argv)
{
ACE_Time_Value expiration = ACE_OS::gettimeofday();
ACE_Time_Value interval; ACE_Get_Opt opt(argc,argv,"e:i:");
for (int c;(c = opt()) != -;)
{
switch(c)
{
case 'e': expiration += ACE_Time_Value(atoi(opt.opt_arg()));
break;
case 'i':interval = ACE_Time_Value(atoi(opt.opt_arg()));
break;
}
}
if (interval > max_interval)
{
cout<<"interval must be less than "<<max_interval.sec()<<endl;
}
else if (expiration > (ACE_Time_Value::max_time - interval))
{
cout<<"expiration + interval must be less than"<<ACE_Time_Value::max_time.sec()<<endl;
}
return ;
}

  部分转载自http://www.cnblogs.com/hbccdf/p/use_acegetopt_parse_commandline.html。

ACE_Get_Opt解析命令行的更多相关文章

  1. 使用ACE_Get_Opt解析命令行

    当我们用C++开发一些C++控制台小工具时,会需要一些用户输入的参数来决定程序如何工作和执行,而用户输入参数的方式大部分都是采用命令行参数的方式. 比如上一篇文章 玩转Windows服务系列--命令行 ...

  2. boost之program_options库,解析命令行参数、读取配置文件

    一.命令行解析 tprogram_options解析命令行参数示例代码: #include <iostream> using namespace std; #include <boo ...

  3. shell解析命令行的过程以及eval命令

    本文说明的是一条linux命令在执行时大致要经过哪些过程?以及这些过程的大致顺序. 1.1 shell解析命令行 shell读取和执行命令时的大致操作过程如下图: 以执行以下命令为例: echo -e ...

  4. optparse模块解析命令行参数的说明及优化

    一.关于解析命令行参数的方法 关于“解析命令行参数”的方法我们一般都会用到sys.argv跟optparse模块.关于sys.argv,网上有一篇非常优秀的博客已经介绍的很详细了,大家可以去这里参考: ...

  5. python解析命令行参数

    常常需要解析命令行参数,经常忘记,好烦,总结下来吧. 1.Python 中也可以所用 sys 的 sys.argv 来获取命令行参数: sys.argv 是命令行参数列表 参数个数:len(sys.a ...

  6. C#/.NET 使用 CommandLineParser 来标准化地解析命令行

    CommandLineParser 是一款用于解析命令行参数的 NuGet 包.你只需要关注你的业务,而命令行解析只需要极少量的配置代码. 本文将介绍如何使用 CommandLineParser 高效 ...

  7. 使用 Apache Commons CLI 解析命令行参数示例

    很好的输入参数解析方法 ,转载记录下 转载在: https://www.cnblogs.com/onmyway20xx/p/7346709.html Apache Commons CLI 简介 Apa ...

  8. linux 中解析命令行参数(getopt_long用法)

    linux 中解析命令行参数(getopt_long用法) http://www.educity.cn/linux/518242.html 详细解析命令行的getopt_long()函数 http:/ ...

  9. (转)shell解析命令行的过程以及eval命令

    shell解析命令行的过程以及eval命令   本文说明的是一条linux命令在执行时大致要经过哪些过程?以及这些过程的大致顺序. 1.1 shell解析命令行 shell读取和执行命令时的大致操作过 ...

随机推荐

  1. React - Stores

    Event emmiters that make data available, handle business logic, send events to React, and listen for ...

  2. JVM基础(3)-多态性实现机制

    一.方法解析 Class 文件的编译过程中不包含传统编译中的连接步骤,一切方法调用在 Class 文件里面存储的都只是符号引用,而不是方法在实际运行时内存布局中的入口地址. 因此,想要使用这些符号引用 ...

  3. hdu 1028 Ignatius and the Princess III(母函数入门+模板)

    Description "Well, it seems the first problem is too easy. I will let you know how foolish you ...

  4. 获取用户IP地址

    // <summary> /// 取得客户端真实IP.如果有代理则取第一个非内网地址 /// by flower.b /// </summary> public static ...

  5. vmware虚拟机如何安装ubuntu14.10系统

    vmware虚拟机安装ubuntu14.10系统安装步骤如下:

  6. vb6加载时提示出错,窗体log文件中错误信息为:控件 XX 的类 MSComctlLib.ListView 不是一个已加载的控件类。

    解决办法:单击[工程] -- [部件] 添加此Microsoft Windows Common Controls-6.0 (SP6)部件,如果列表中没有,浏览到~\project\包\Support中 ...

  7. mvc的IIS 配置问题 runAllManagedModulesForAllRequests 与 HtmlFileHandler

    runAllManagedModulesForAllRequests 一般设置为false,当为true时所有的资源将进入mvc处理,无疑会给服务器加大压力. 解决办法是时使用HtmlFileHand ...

  8. HDU1007--Quoit Design(平面最近点对)

    Problem Description Have you ever played quoit in a playground? Quoit is a game in which flat rings ...

  9. POJ 3417 Network

    每条额外的边加入到图中,会导致树上一条路径成环,假设没有其余边,那么要将新图分成两部分,如果想删一条成环路径上的边,那么必须把这条额外边也删除. 因此每条额外边加入时,只需将环上的边+1.最后看看每条 ...

  10. webdriver入门

    webdriver是web自动化测试中的重要工具,通过webdriver可以灵活的操纵browser完成相关的测试,目前的webdriver对主流的浏览器均有支持, 如firefox ,chrome, ...