- #include <getopt.h> //linux解析命令参数
- #include <limits.h>
- static char *l_opt_arg;
- static int Para_CmdParse(int argc, char *argv[])
- {
- int c;
- int len;
- int value;
- /*遍历输入参数,设置配置参数*/
- while ((c = getopt_long (argc, argv, shortopts, longopts, NULL)) != -1)
- {
- switch (c)
- {
- case 'c': /*CGI根路径*/
- l_opt_arg = optarg;//optarg在getopt.h中定义
- if(l_opt_arg && l_opt_arg[0]!=':'){
- len = strlen(l_opt_arg);
- memcpy(conf_para.CGIRoot, l_opt_arg, len +1);
- }
- break;
- case 'p': /*侦听端口*/
- l_opt_arg = optarg;
- if(l_opt_arg && l_opt_arg[0]!=':'){
- len = strlen(l_opt_arg);
- value = strtol(l_opt_arg, NULL, 10);
- if(value != LONG_MAX && value != LONG_MIN)
- conf_para.ListenPort = value;
- }
- break;
- case '?':/*错误参数*/
- printf("Invalid para\\n");
- case 'h': /*帮助*/
- display_usage();
- break;
- }
- }
- return 0;
- }
- static void display_usage(void)
- {
- printf("appNmae -l number -c path \\n");
- printf("appNmae--ListenPort number\\n");
- printf(" --CGIRoot path \\n");
- }
- //该片段来自于http://www.codesnippet.cn/detail/131120137106.html
来源: http://www.codesnippet.cn/detail/131120137106.html