问题描述
我有一个拍摄多个命令行参数的程序,所以我使用getopt.我的一个参数用一个字符串作为参数.是否有没有通过Getopt函数获取该字符串,或者我必须通过argv []数组来获取它?也可以像-file这样的getopt read args?我看到的所有参数只有一个字符,例如-a
编辑
从下面的答案中我写了一个程序要使用getopt_long(),但是交换机语句只识别我使用字符参数时识别参数,而不是长参数.我不确定为什么发生这种情况.通过参数-mf -file sample我没有看到打印语句.
编辑
我尝试输入命令参数作为--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; } }
推荐答案
read man getopt http://linux.die.net/man/3/getopt
optstring是包含合法选项字符的字符串.如果 这种角色之后是冒号,该选项需要一个 参数,所以getopt()将指针置于以下文本中的指针 相同的argv-component,或以下argv-component的文本 Optarg.两个冒号意味着一个选项采取可选arg;如果有 当前argv-component中的文本(即,与选项相同的单词 名称本身,例如,"-oarg"),然后它在optarg返回, 否则,optarg设置为零.
示例代码:
#include <stdio.h> #include <unistd.h> 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中找到参数作为字符串.有关详细信息和更多示例,请参阅手册.
对于多个字符选项交换机,请参阅长选项getopt_long.检查手册.
编辑响应单个" - "长选项:
从男人页
getopt_long_only()就像getopt_long(),但' - '以及" - "可以指示很长的选项.如果一个以' - '开头的选项 (不是" - ")与长选项不匹配,但与短选项相匹配, 它被解析为简短的选择.
检查手册并尝试.
其他推荐答案
要指定标志需要参数,请在short_opt变量中的标志之后添加":".要使用长参数,请使用getopt_long().
这是一个快速示例程序:
#include <getopt.h> #include <stdio.h> 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); }