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

C Getopt-传递参数的字符串参数,c,getopt,C,Getopt,我有一个接受多个命令行参数的程序,所以我使用getopt。我的一个参数接受字符串作为参数。是否仍然需要通过getopt函数获取该字符串,还是必须通过argv[]数组获取该字符串?还可以使用getopt读取参数,如-file?到目前为止,我看到的所有参数只有一个字符,如-a 编辑 根据下面的答案,我编写了一个使用getopt_long()的程序,但是switch语句仅在我使用character参数而不是long参数时识别参数。我不知道为什么会这样。在传递参数-mf-file sample时,我没有

我有一个接受多个命令行参数的程序,所以我使用getopt。我的一个参数接受字符串作为参数。是否仍然需要通过getopt函数获取该字符串,还是必须通过argv[]数组获取该字符串?还可以使用getopt读取参数,如
-file
?到目前为止,我看到的所有参数只有一个字符,如
-a

编辑

根据下面的答案,我编写了一个使用getopt_long()的程序,但是switch语句仅在我使用character参数而不是long参数时识别参数。我不知道为什么会这样。在传递参数
-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;  
    }
}
static struct option long\u options[]=
{
{“mf”,必选参数,NULL,'a'},
{“md”,必需的_参数,NULL,'b'},
{“mn”,必需的_参数,NULL,'c'},
{“mw”,必需的_参数,NULL,'d'},
{“lf”,必需的_参数,NULL,'e'},
{“ld”,必选参数,NULL,'f'},
{“ln”,必选参数,NULL,'g'},
{“lw”,必需的_参数,NULL,'h'},
{“rf”,必选参数,NULL,'i'},
{“rd”,必需的_参数,NULL,'j'},
{“rn”,必需的_参数,NULL,'k'},
{“rw”,必需的_参数,NULL,'l'},
{“df”,必需的_参数,NULL,'m'},
{“dd”,必需的_参数,NULL,'n'},
{“dn”,必需的_参数,NULL,'o'},
{“dw”,必需的_参数,NULL,'p'},
{“file”,必需的参数,NULL,'q'},
{NULL,0,NULL,0}
};
int ch=0;
while((ch=getopt_long(argc,argv,“abcdefghijklmnopq:,long_options,NULL))!=-1)
{
//检查是否通过了单个字符或长选项
开关(ch){
案例“a”:

无法阅读
mangetopt

optstring是包含合法选项字符的字符串。如果 这样的字符后跟冒号,该选项需要 参数,因此getopt()在 中相同的argv元素或以下argv元素的文本 optarg。两个冒号表示一个选项使用可选参数;如果有 当前argv元素中的文本(即,与选项使用相同的单词 名称本身,例如,“-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;
}
#包括
#包括
int main(int argc,char*argv[])
{
int-opt;
while((opt=getopt(argc,argv,“i:o:”)!=-1)
{
开关(opt)
{
案例“i”:
printf(“输入文件:\%s\”\n,optarg);
打破
案例“o”:
printf(“输出文件:\%s\”\n,optarg);
打破
}
}
返回0;
}
这里的
optstring
是“i:o:”字符串中每个字符后面的冒号“:”表示这些选项需要参数。您可以在
optarg
全局变量中找到参数作为字符串。有关详细信息和更多示例,请参阅手册

有关多个字符的选项开关,请参阅长选项
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);
}
#包括
#包括
intmain(intargc,char*argv[]);
int main(int argc,char*argv[])
{
INTC;
常量字符*short_opt=“hf:”;
结构选项long_opt[]=
{
{“help”,无_参数,NULL,'h'},
{“file”,必需的参数,NULL,'f'},
{NULL,0,NULL,0}
};
while((c=getopt_long(argc,argv,short_opt,long_opt,NULL))!=-1)
{
开关(c)
{
案例-1:/*没有更多参数*/
案例0:/*长选项切换*/
打破
案例“f”:
printf(“您输入了\%s\”\n,optarg);
打破
案例“h”:
printf(“用法:%s[选项]\n”,argv[0]);
printf(“-f文件\n”);
printf(“-h,--help打印此帮助并退出\n”);
printf(“\n”);
返回(0);
案例“:”:
案例“?”:
fprintf(stderr,“请尝试“%s--help”以获取详细信息。\n”,argv[0]);
回报率(-2);
违约:
fprintf(stderr,“%s:无效选项--%c\n”,argv[0],c);
fprintf(stderr,“请尝试“%s--help”以获取详细信息。\n”,argv[0]);
回报率(-2);
};
};
返回(0);
}

我可以用于getopt_long()的不同参数的数量有限制吗?我有17个不同的参数,它只在我输入参数的字符版本时识别参数,而不是长参数。我相信您只受内存限制。您是否正在更新长选项的最后一个值?在我的示例中,每个长选项都与短选项配对。因此“-help”和“-h”是相同的选项。如何通过将“h”更改为“p”并将“p”添加到短的\u opt字符串中,您可以使“--help”与“-p”选项相同。我只是看到您在编辑。您必须使用“--option”而不是“-option”。getopt()和getopt\u long()将'-option'解释为'-o-p-t-i-o-n'。如果您想将长选项与一个破折号一起使用,则必须编写