找回密码
 用户注册

QQ登录

只需一步,快速开始

查看: 4373|回复: 0

解析命令行参数-ACE_Get_Opt类的用法

[复制链接]
发表于 2012-2-29 00:23:30 | 显示全部楼层 |阅读模式
通常我们开发后台服务程序时经常要提供命令行解析和搜集配置信息的功能。重复这样的
劳动不仅耗时而且对操作系统平台也有依赖性,ACE框架提供了对命令行参数的解析及配置
文件的解析的API接口使我们可以方面的对我们的程序进行方面的配置。
解析命令行参数-ACE_Get_Opt类的用法
ARGV_Example.cpp
此示例演示了ACE_Get_Optint类的基本用法,用来解析类似 program -c -g -a param 这样的程序参数源码解释
  1. /**
  2. * ARGV_Example.cpp,v 1.3
  3. 2004/01/03 13:04:35 jwillemsen Exp
  4. *
  5. * ACE_ARGV examples not in a larger
  6. program. Sample code from The ACE
  7. * Programmer's Guide, Copyright 2003
  8. Addison-Wesley. All Rights Reserved.
  9. */
  10. #include
  11. "ace/os_include/os_netdb.h"
  12. #include "ace/OS_NS_string.h"
  13. #include
  14. "ace/Log_Msg.h"
  15. // Listing 1 code/ch04
  16. #include
  17. "ace/ARGV.h"
  18. #include
  19. "ace/Get_Opt.h"
  20. //此示例演示了ACE_Get_Optint类的基本用法,用来解析类似 program -c -g -a
  21. param  //这样的程序参数
  22. ACE_TMAIN (int argc, ACE_TCHAR *argv[])
  23. {
  24.    
  25. //'(":f:h:")'类似一个参数解析的模版 字母紧跟着一个冒号':'那就意味着该“参数开关”    //后期望一个参数
  26.     //"f:"
  27. 和"h:"说明参数列表必须是 -f parm1 -h parm2的形式
  28.     //options[] = "ab:" 则说明参数列表应该是 -a -b
  29. parm1类似的形式
  30.     static const ACE_TCHAR options[] = ACE_TEXT (":f:h:");
  31.    
  32. //ACE_Get_Opt会根据指定的参数和模版生产我们需要的参数列表
  33.    ACE_Get_Opt cmd_opts (argc, argv,
  34. options);
  35. // Listing 1
  36.   int option;
  37.   ACE_TCHAR
  38. config_file[MAXPATHLEN];
  39.   ACE_TCHAR hostname[MAXHOSTNAMELEN];
  40.   
  41. ACE_OS_String::strcpy (config_file, ACE_TEXT ("HAStatus.conf"));
  42.   
  43. ACE_OS_String::strcpy (hostname, ACE_TEXT ("not set"));
  44.   
  45. //遍历参数列表并进行读取、解析
  46.   while ((option = cmd_opts ()) != EOF)
  47.     switch
  48. (option) {
  49.     case 'f':
  50.       ACE_OS_String::strncpy
  51. (config_file,
  52.                               cmd_opts.opt_arg
  53. (),
  54.                               MAXPATHLEN);
  55.       break;
  56.    
  57. case 'h':
  58.       ACE_OS_String::strncpy
  59. (hostname,
  60.                               cmd_opts.opt_arg
  61. (),
  62.                               MAXHOSTNAMELEN);
  63.       break;
  64.      
  65. //':'放在"参数模版"的最前面,如果根据模版指示如没有找到期待的参数
  66.      //重载的'='操作符会返回':'否则会返回'?'
  67.      
  68. case ':':
  69.       ACE_ERROR_RETURN
  70.         ((LM_ERROR, ACE_TEXT ("-%c
  71. requires an argument\n"),
  72.           cmd_opts.opt_opt ()), -1);
  73.    
  74. default:
  75.       ACE_ERROR_RETURN
  76.         ((LM_ERROR, ACE_TEXT ("Parse
  77. error.\n")), -1);
  78.     }
  79.   ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("Config
  80. file: %s\n"), config_file));
  81.   ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("Hostname:
  82. %s\n"), hostname));
  83.   return 0;
  84. }执行结果演示
  85. //正常的参数解析
  86. D:\project\ACE_wrappers\examples\APG\Config>ARGV_Example -f
  87. my.conf -h chengkai
  88. Config file: my.conf
  89. Hostname:
  90. chengkai
  91. //确实一个必要的参数
  92. D:\project\ACE_wrappers\examples\APG\Config>ARGV_Example
  93. -f my.conf -h
  94. -h requires an
  95. argument
  96. //指定了一个无效的参数
  97. D:\project\ACE_wrappers\examples\APG\Config>ARGV_Example
  98. -f my.conf -a
  99. Parse error.
  100. Get_Opt_Long.cpp
  101. 此示例演示长参数开关的使用,你可以用'program
  102. -config my.conf' 或用'program --config my.conf'
  103. 这样形式的用法和 'program -f
  104. my.conf'是等价的。源码解释
  105. /**
  106. * Get_Opt_Long.cpp,v 1.2 2004/01/03 13:04:35 jwillemsen Exp
  107. *
  108. *
  109. ACE_Get_Opt long_only examples. Sample code from The ACE
  110. * Programmer's
  111. Guide, Copyright 2003 Addison-Wesley. All Rights Reserved.
  112. */
  113. #include
  114. "ace/OS_NS_string.h"
  115. #include "ace/Get_Opt.h"
  116. #include
  117. "ace/Log_Msg.h"
  118. //此示例演示长参数开关的使用,你可以用'program -config my.conf'//或用'program
  119. --config my.conf'
  120. //这样形式的用法和 'program -f my.conf'是等价的
  121. int
  122. ACE_TMAIN
  123. (int argc, ACE_TCHAR *argv[])
  124. {
  125.   static const ACE_TCHAR options[] =
  126. ACE_TEXT (":f:");
  127.   ACE_Get_Opt cmd_opts
  128.     (argc, argv, options, 1, 0,
  129. ACE_Get_Opt::PERMUTE_ARGS, 1);
  130.   //指定短选项开关对应的长选项开关
  131.   if
  132. (cmd_opts.long_option
  133.       (ACE_TEXT ("config"), 'f',
  134. ACE_Get_Opt::ARG_REQUIRED) == -1)
  135.     return -1;
  136.   int option;
  137.   
  138. ACE_TCHAR config_file[MAXPATHLEN];
  139.   ACE_OS_String::strcpy (config_file,
  140. ACE_TEXT ("HAStatus.conf"));
  141.   while ((option = cmd_opts ()) != EOF)
  142.    
  143. switch (option) {
  144.     case 'f':
  145.       ACE_OS_String::strncpy
  146. (config_file,
  147.                               cmd_opts.opt_arg
  148. (),
  149.                               MAXPATHLEN);
  150.       break;
  151.    
  152. case ':':
  153.       ACE_ERROR_RETURN
  154.         ((LM_ERROR, ACE_TEXT ("-%c
  155. requires an argument\n"),
  156.           cmd_opts.opt_opt ()), -1);
  157.    
  158. default:
  159.       ACE_ERROR_RETURN
  160.         ((LM_ERROR, ACE_TEXT ("Parse
  161. error.\n")), -1);
  162.     }
  163.   ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("Config file
  164. is %s\n"), config_file));
  165.   return 0;
  166. }执行结果演示
  167. D:\project\ACE_wrappers\examples\APG\Config>Get_Opt_Long -f
  168. my.conf
  169. Config file is my.conf
  170. D:\project\ACE_wrappers\examples\APG\Config>Get_Opt_Long -config
  171. my.conf
  172. Config file is my.conf
  173. D:\project\ACE_wrappers\examples\APG\Config>Get_Opt_Long --config
  174. my.conf
  175. Config file is
  176. my.conf
  177. Get_Opt.cpp
  178. 此示例展示了在使用长选项并且没有指定与之对应的短选项的时候,ACE_Get_Opt可以通过指定一个整数返回值与长选项匹配/**
  179. *
  180. Get_Opt.cpp,v 1.2 2004/01/03 13:04:35 jwillemsen Exp
  181. *
  182. * ACE_Get_Opt
  183. examples not in a larger program. Sample code from The ACE
  184. * Programmer's
  185. Guide, Copyright 2003 Addison-Wesley. All Rights Reserved.
  186. */
  187. #include
  188. "ace/OS_NS_string.h"
  189. #include "ace/Get_Opt.h"
  190. #include
  191. "ace/Log_Msg.h"
  192. //此示例展示了在使用长选项并且没有指定与之对应的短选项的时候,可以通过指定一个//  
  193. 整数返回值与长选项匹配
  194. int
  195. ACE_TMAIN (int argc, ACE_TCHAR *argv[])
  196. {
  197.   //
  198. Example for a long option without a corresponding short option.
  199.   // Just put
  200. some context here so the following compiles and runs.
  201.   static const
  202. ACE_TCHAR options[] = ACE_TEXT (":f:");
  203.   ACE_Get_Opt cmd_opts (argc, argv,
  204. options);
  205.   // 指定长选项--cool_option 并且其缺省返回值0
  206.   cmd_opts.long_option
  207. (ACE_TEXT ("cool_option"));
  208.   cmd_opts.long_option (ACE_TEXT ("the_answer"),
  209. 42);
  210.   // Listing 1
  211.   int option;
  212.   ACE_TCHAR
  213. config_file[MAXPATHLEN];
  214.   ACE_OS_String::strcpy (config_file, ACE_TEXT
  215. ("HAStatus.conf"));
  216.   while ((option = cmd_opts ()) != EOF)
  217.     switch
  218. (option) {
  219.     case 'f':
  220.       ACE_OS_String::strncpy
  221. (config_file,
  222.                               cmd_opts.opt_arg
  223. (),
  224.                               MAXPATHLEN);
  225.       break;
  226.       
  227. // Listing 2 code/ch04
  228.     case 0:
  229.       ACE_DEBUG ((LM_DEBUG, ACE_TEXT
  230. ("Yes, very cool.\n")));
  231.       break;
  232.     case 42:
  233.       ACE_DEBUG
  234. ((LM_DEBUG, ACE_TEXT ("the_answer is 42\n")));
  235.       break;
  236.       //
  237. Listing 2
  238.     case ':':
  239.       ACE_ERROR_RETURN
  240.         ((LM_ERROR,
  241. ACE_TEXT ("-%c requires an argument\n"),
  242.           cmd_opts.opt_opt ()),
  243. -1);
  244.     default:
  245.       ACE_ERROR_RETURN
  246.         ((LM_ERROR,
  247. ACE_TEXT ("Parse error.\n")), -1);
  248.     }
  249.   return 0;
  250. }执行结果演示
  251. Get_Opt --cool_option  --the_answer
  252. Yes, very cool.
  253. the_answer is
  254. 42
  255. 读写配置文件-ACE_Configuration_Heap类的用法
  256. HA_Status.cpp
  257. 此示例演示了ACE对配置文件的读取的功能,ACE可以方便的从配置文件读取字符串类型、整数类型、二进制信息/**
  258. *
  259. HA_Status.cpp,v 1.4 2005/06/29 16:35:46 shuston Exp
  260. *
  261. * Home Automation
  262. Status server. Sample code from The ACE Programmer's Guide,
  263. * Copyright 2003
  264. Addison-Wesley. All Rights Reserved.
  265. */
  266. #include
  267. "ace/OS_NS_string.h"
  268. #include "ace/Configuration.h"
  269. #include
  270. "ace/Configuration_Import_Export.h"
  271. #include "ace/Get_Opt.h"
  272. #include
  273. "ace/Log_Msg.h"
  274. #include "ace/INET_Addr.h"
  275. #include
  276. "ace/Service_Object.h"
  277. class HA_Status : public
  278. ACE_Service_Object
  279. {
  280. public:
  281.   virtual int init (int argc, ACE_TCHAR
  282. *argv[]);
  283. private:
  284.   ACE_INET_Addr
  285. listen_addr_;
  286. };
  287. int
  288. HA_Status::init (int argc, ACE_TCHAR
  289. *argv[])
  290. {
  291.   // Do ACE_Get_Opt and get conf file name, read out the
  292. sections
  293.   // and print the names.
  294.   // Listing 1 code/ch04
  295.   
  296. static const ACE_TCHAR options[] = ACE_TEXT (":f:");
  297.   ACE_Get_Opt cmd_opts
  298. (argc, argv, options);
  299.   if (cmd_opts.long_option
  300.       (ACE_TEXT
  301. ("config"), 'f', ACE_Get_Opt::ARG_REQUIRED) == -1)
  302.     return -1;
  303.   int
  304. option;
  305.   ACE_TCHAR config_file[MAXPATHLEN];
  306.   ACE_OS::strcpy
  307. (config_file, ACE_TEXT ("HAStatus.conf"));
  308.   while ((option = cmd_opts ()) !=
  309. EOF)
  310.     switch (option) {
  311.     case 'f':
  312.       ACE_OS::strncpy
  313. (config_file,
  314.                        cmd_opts.opt_arg
  315. (),
  316.                        MAXPATHLEN);
  317.       break;
  318.     case
  319. ':':
  320.       ACE_ERROR_RETURN
  321.         ((LM_ERROR, ACE_TEXT ("-%c requires an
  322. argument\n"),
  323.           cmd_opts.opt_opt ()), -1);
  324.     default:
  325.       
  326. ACE_ERROR_RETURN
  327.         ((LM_ERROR, ACE_TEXT ("Parse error.\n")),
  328. -1);
  329.     }
  330.   // Listing 1
  331.   // Listing 2 code/ch04
  332.   
  333. ACE_Configuration_Heap config;
  334.   if (config.open () == -1)
  335.    
  336. ACE_ERROR_RETURN
  337.       ((LM_ERROR, ACE_TEXT ("%p\n"), ACE_TEXT ("config")),
  338. -1);
  339.   ACE_Registry_ImpExp config_importer (config);
  340.   if
  341. (config_importer.import_config (config_file) == -1)
  342.    
  343. ACE_ERROR_RETURN
  344.       ((LM_ERROR, ACE_TEXT ("%p\n"), config_file),
  345. -1);
  346.   ACE_Configuration_Section_Key status_section;
  347.   //指定打开HAStatus
  348. 节进行读取
  349.   if (config.open_section (config.root_section
  350. (),
  351.                            ACE_TEXT
  352. ("HAStatus"),
  353.                            0,
  354.                            
  355. status_section) == -1)
  356.     ACE_ERROR_RETURN ((LM_ERROR, ACE_TEXT
  357. ("%p\n"),
  358.                        ACE_TEXT ("Can't open HAStatus
  359. section")),
  360.                       -1);
  361.   
  362. //分别用来保存读取的字符串类型的地址和整形的端口号
  363.   u_int status_port;
  364.   ACE_TString  server_ip
  365. ;
  366.   //读取取字符串类型的地址属性
  367.   if(config.get_string_value(status_section,ACE_TEXT
  368. ("ServerIP"),                                                   server_ip)
  369. ==-1)  
  370.        ACE_ERROR_RETURN
  371.       ((LM_ERROR,
  372.         ACE_TEXT
  373. ("HAStatus ServerIP does not exist\n")),
  374.        -1);
  375.   //读取整形的端口属性
  376.   if
  377. (config.get_integer_value (status_section,
  378.                                 
  379. ACE_TEXT ("ListenPort"),
  380.                                 status_port) ==
  381. -1)
  382.     ACE_ERROR_RETURN
  383.       ((LM_ERROR,
  384.         ACE_TEXT ("HAStatus
  385. ListenPort does not exist\n")),
  386.        -1);
  387.   //this->listen_addr_.set
  388. (static_cast<u_short> (status_port));
  389.   // Listing 2
  390.   ACE_DEBUG
  391. ((LM_DEBUG, ACE_TEXT ("ServerIP =
  392. %s\n"),                                            server_ip.c_str()));
  393.   
  394. ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("ListenPort = %d"),status_port));
  395.   
  396. return 0;
  397. }
  398. int
  399. ACE_TMAIN (int argc, ACE_TCHAR *argv[])
  400. {
  401.   
  402. HA_Status status;
  403.   status.init (argc, argv);
  404.   return
  405. 0;
  406. }
复制代码
配置文件内容格式
•[名称]用来定义一个节。
•可以用'#' 或';'来作为注释
•“attribute”=“value”表示一个字符串类型的属性 。
•attribute=#value 或 表示一个整数类型的属性值。
•“attribute”=dword:value 表示一个64位的双字(十六进制)
•“attribute”=hex:value 以十六进制串表示二进制序列
ACE对配置文件的解析和我们通常用的对配置文件读写的类库不太一样1),感觉ACE对配置文件的
读取对格式要求比较严格和我们普通使用的配置文件格式兼容性稍差一些。
有以下几点点需要我们注意。

1.如果你用来表示一个字符串类型的属性值,你应该用'”'包括你的属性值和属性名称,
2.否则ACE将不能正常解析
3.如果你使用类似”dword:“、”hex:“等前缀表示数字类型的属性值,
4.你需要把属性名称用'”'包括起来 ,属性值能用'”'包括起来
5.表示整数类型的属性时,属性名称和属性值都不要'”'包括起来
测试程序
配置文件内容
#这用来表示一个"节"
[HAStatus]
;字符串类型的属性值
"ServerIP"="127.0.0.1"
;整数类型的属性值
ListenPort=#8080
执行结果
D:\project\ACE_wrappers\examples\APG\Config>HA_Status.exe
ServerIP = 127.0.0.1
ListenPort = 8080
1) 象 attribute=value 这样的定义ACE将不能正常解析,这也是我在读了ACE的实现代码后才得知的,
一开始还以为这是一个BUG  

您需要登录后才可以回帖 登录 | 用户注册

本版积分规则

Archiver|手机版|小黑屋|ACE Developer ( 京ICP备06055248号 )

GMT+8, 2024-4-28 16:53 , Processed in 0.012054 second(s), 6 queries , Redis On.

Powered by Discuz! X3.5

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表