现在的位置: 首页 > 综合 > 正文

c++中getopt和getopt_long的使用方法

2013年10月23日 ⁄ 综合 ⁄ 共 4271字 ⁄ 字号 评论关闭


  getopt(分析命令行参数)
  相关函数
  表头文件 #include<unistd.h>
  定义函数 int getopt(int argc,char * const argv[ ],const char * optstring);
  extern char *optarg;
  extern int optind, opterr, optopt;
  函数说明 getopt()用来分析命令行参数。参数argc和argv是由main()传递的参数个数和内容。参数 optstring为选项字符串, 告知 getopt()可以处理哪个选项以及哪个选项需要参数,如果选项字符串里的字母后接着冒号“:”,则表示还有相关的参数,全域变量optarg 即会指向此额外参数。如果在处理期间遇到了不符合optstring指定的其他选项getopt()将显示一个错误消息,并将全域变量optarg设为“?”字符,如果不希望getopt()印出错信息,则只要将全域变量opterr设为0即可。
  getopt() 所设置的全局变量包括:
  optarg——指向当前选项参数(如果有)的指针。
    optind——再次调用 getopt() 时的下一个 argv 指针的索引。
    optopt——最后一个已知选项。
    opterr : 是否打印出错信息,如果不希望getopt()印出错信息,则只要将全域变量opterr设为0即可。

  补充说明下optstring中的指定的内容的意义(例如getopt(argc, argv, "ab:c:de::");
  1.单个字符,表示选项,(如上例中的abcde各为一个选项)
  2.单个字符后接一个冒号:表示该选项后必须跟一个参数。参数紧跟在选项后或者以空格隔开。该参数的指针赋给optarg。(如上例中的b:c:)
  3 单个字符后跟两个冒号,表示该选项后必须跟一个参数。参数必须紧跟在选项后不能以空格隔开。该参数的指针赋给optarg。(如上例中的e::)

  范例 #include<stdio.h>
  #include<unistd.h>
  int main(int argc,char **argv)
  {
  int ch;
  opterr = 0;
  while((ch = getopt(argc,argv,”a:bcde”))!= -1)
  switch(ch)
  {
  case ‘a’: printf(“option a:’%s’\n”,optarg); break;
  case ‘b’: printf(“option b :b\n”); break;
  default: printf(“other option :%c\n”,ch);
  }
  printf(“optopt +%c\n”,optopt);
  }
  执行 $./getopt –b
  option b:b
  执行 $./getopt –c
  other option:c
  执行 $./getopt –a
  other option :?
  执行 $./getopt –a12345
  option a:’12345’

与Shell一样,GNU C也提供一组解析命令参数的函数:getopts 与 getopt_long。前者处理类似 -o 这样的参数(单-),后者处理长参数,类似于--help (双-)。

在shell中,man getopt_long,会给出这组函数的详细说明。这里作简要说明。

一、getopts
用法:
#include <unistd.h>

int getopt(int argc, char * const argv[], const char *optstring);

extern char *optarg;
extern int optind, opterr, optopt;

getopts解析命令行传给main函数的参数argc和argv[][]。以'-'开头的参数都视作选项(option)。 getopts连续调用,就可以逐次获得所有选项。每次调用时, optind 保存下个参数在argv中的索引(index)。如果找到一个选项,getopt会返回找到的选项字符,更新optind。如果选项有参数,将参数存到optarg,否则optarg为0。

optstring是一个字符串,控制选项的读取。如,"abc:d",表示读取选项-a,-b,-c,-d, 并且-c 需要给参数。如果getopts碰到optstring无法识别的选项,函数返回'?',并且将选项存到optopt。如果将opterr赋为0,将不会显示错误信息。

getopts会缺省将argv排列,使得选项在前,而非选项的参数放在最后。这样当getopts读取完所有的选项以后,optind会指向非选项的参数。

下面是man提供的例子:

#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>

int
main(int argc, char *argv[])
{
int flags, opt;
int nsecs, tfnd;

nsecs = 0;
tfnd = 0;
flags = 0;
while ((opt = getopt(argc, argv, "nt:")) != -1) {
switch (opt) {
case 'n':
flags = 1;
break;
case 't':
nsecs = atoi(optarg);
tfnd = 1;
break;
default: /* '?' */
fprintf(stderr, "Usage: %s [-t nsecs] [-n] name\n",
argv[0]);
exit(EXIT_FAILURE);
}
}

printf("flags=%d; tfnd=%d; optind=%d\n", flags, tfnd, optind);

if (optind >= argc) {
fprintf(stderr, "Expected argument after options\n");
exit(EXIT_FAILURE);
}

printf("name argument = %s\n", argv[optind]);

/* Other code omitted */

exit(EXIT_SUCCESS);
}

二、getopt_long
用法:
#include <getopt.h>

int getopt_long(int argc, char * const argv[], const char *optstring,  const struct option *longopts, int *longindex);

int getopt_long_only(int argc, char * const argv[], const char *optstring,  const struct option *longopts, int *longindex);

getopt_long的用法基本与getopt一样。为了解析长选项,需要显式定义长选项结构。
getopt_long也可以识别长选项的简写,如果简写唯一的话。例如,--help选项,写成--h, --he, --hel也可以识别,只要简写唯一。

长选项结构的定义如下
struct option {
const char *name;
int         has_arg;
int        *flag;
int         val;
};

其中name是长选项的名字,has_arg表示长选项有无参数,有三种定义:no_argument, required_argument,optional_argument。flag代表结果如何返回,若flag是NULL,getopt_long返回val,否则返回0,并将val存到flag指向的地址。

结构的最后一组元素必须赋为{0, 0, 0, 0}。

longindex指向长选项在结构longopts中的索引。

下面是man提供的例子:

#include <stdio.h>     /* for printf */
#include <stdlib.h>    /* for exit */
#include <getopt.h>

int
main(int argc, char **argv)
{
int c;
int digit_optind = 0;

while (1) {
int this_option_optind = optind ? optind : 1;
int option_index = 0;
static struct option long_options[] = {
{"add", 1, 0, 0},
{"append", 0, 0, 0},
{"delete", 1, 0, 0},
{"verbose", 0, 0, 0},
{"create", 1, 0, 'c'},
{"file", 1, 0, 0},
{0, 0, 0, 0}
};

c = getopt_long(argc, argv, "abc:d:012",
long_options, &option_index);
if (c == -1)
break;

switch (c) {
case 0:
printf("option %s", long_options[option_index].name);
if (optarg)
printf(" with arg %s", optarg);
printf("\n");
break;

case '0':
case '1':
case '2':
if (digit_optind != 0 && digit_optind != this_option_optind)
printf("digits occur in two different argv-elements.\n");
digit_optind = this_option_optind;
printf("option %c\n", c);
break;
case 'a':
printf("option a\n");
break;

case 'b':
printf("option b\n");
break;

case 'c':
printf("option c with value '%s'\n", optarg);
break;

case 'd':
printf("option d with value '%s'\n", optarg);
break;

case '?':
break;

default:
printf("?? getopt returned character code 0%o ??\n", c);
}
}

if (optind < argc) {
printf("non-option ARGV-elements: ");
while (optind < argc)
printf("%s ", argv[optind++]);
printf("\n");
}

exit(EXIT_SUCCESS);
}

抱歉!评论已关闭.