Getopt-传递参数的字符串参数

我有一个程序,它接受多个命令行参数,所以我使用getopt。 我的一个参数将字符串作为参数。 无论如何通过getopt函数获取该字符串还是我必须通过argv []数组获取它? 也可以像-file一样读取args吗? 我到目前为止看到的所有论据只有一个字符,如-a

编辑

从下面的答案中我编写了一个使用getopt_long()的程序,但是当我使用character参数而不是long参数时,switch语句只识别参数。 我不确定为什么会这样。 在传递参数-mf -file sample我没有看到print语句。

编辑

我尝试输入命令参数作为--file然后它工作。 仅使用-file是不可能的?

 static struct option long_options[] = { {"mf", required_argument, NULL, 'a'}, {"md", required_argument, NULL, 'b'}, {"mn", required_argument, NULL, 'c'}, {"mw", required_argument, NULL, 'd'}, {"lf", required_argument, NULL, 'e'}, {"ld", required_argument, NULL, 'f'}, {"ln", required_argument, NULL, 'g'}, {"lw", required_argument, NULL, 'h'}, {"rf", required_argument, NULL, 'i'}, {"rd", required_argument, NULL, 'j'}, {"rn", required_argument, NULL, 'k'}, {"rw", required_argument, NULL, 'l'}, {"df", required_argument, NULL, 'm'}, {"dd", required_argument, NULL, 'n'}, {"dn", required_argument, NULL, 'o'}, {"dw", required_argument, NULL, 'p'}, {"file", required_argument, NULL, 'q'}, {NULL, 0, NULL, 0} }; int ch=0; while ((ch = getopt_long(argc, argv, "abcdefghijklmnopq:", long_options, NULL)) != -1) { // check to see if a single character or long option came through switch (ch){ case 'a': cout<<"title"; break; case 'b': break; case 'c': break; case 'd': break; case 'e': break; case 'f': break; case 'g': break; case 'h': break; case 'i': break; case 'j': break; case 'k': break; case 'l': break; case 'm': break; case 'n': break; case 'o': break; case 'p': break; case 'q': cout<<"file"; break; case '?': cout<<"wrong message" break; } } 

阅读man getopt http://linux.die.net/man/3/getopt

optstring是一个包含合法选项字符的字符串。 如果这样的字符后跟冒号,则该选项需要一个参数,因此getopt()在optarg中的同一argv元素或以下argv元素的文本中放置一个指向以下文本的指针。 两个冒号意味着一个选项需要一个可选的arg; 如果当前argv-element中有文本(即,与选项名称本身相同的单词,例如“-oarg”),则在optarg中返回,否则optarg设置为零。

示例代码:

 #include  #include  int main (int argc, char *argv[]) { int opt; while ((opt = getopt (argc, argv, "i:o:")) != -1) { switch (opt) { case 'i': printf ("Input file: \"%s\"\n", optarg); break; case 'o': printf ("Output file: \"%s\"\n", optarg); break; } } return 0; } 

这里的optstring是“i:o:”字符串中每个字符后面的冒号’:’字符表示这些选项需要一个参数。 您可以在optarg global var中找到参数作为字符串。 请参阅手册了解详细信息和更多示例

对于多个字符选项开关,请参阅long选项getopt_long 。 查看手册以获取示例。

编辑以响应单个’ – ‘长选项:

从手册页

getopt_long_only()类似于getopt_long(),但是“ – ”和“ – ”可以表示长选项。 如果以“ – ”(非“ – ”)开头的选项与长选项不匹配,但与短选项匹配,则会将其解析为短选项。

查看手册并试一试。

要指定标志需要参数,请在short_opt变量中的标志后面添加一个“:”。 要使用长参数,请使用getopt_long()。

这是一个快速示例程序:

 #include  #include  int main(int argc, char * argv[]); int main(int argc, char * argv[]) { int c; const char * short_opt = "hf:"; struct option long_opt[] = { {"help", no_argument, NULL, 'h'}, {"file", required_argument, NULL, 'f'}, {NULL, 0, NULL, 0 } }; while((c = getopt_long(argc, argv, short_opt, long_opt, NULL)) != -1) { switch(c) { case -1: /* no more arguments */ case 0: /* long options toggles */ break; case 'f': printf("you entered \"%s\"\n", optarg); break; case 'h': printf("Usage: %s [OPTIONS]\n", argv[0]); printf(" -f file file\n"); printf(" -h, --help print this help and exit\n"); printf("\n"); return(0); case ':': case '?': fprintf(stderr, "Try `%s --help' for more information.\n", argv[0]); return(-2); default: fprintf(stderr, "%s: invalid option -- %c\n", argv[0], c); fprintf(stderr, "Try `%s --help' for more information.\n", argv[0]); return(-2); }; }; return(0); }