在 C 中使用 getopt 和非选项参数

问题描述 投票:0回答:6

我正在用 C 语言编写一个小程序来处理大量命令行参数,因此我决定使用 getopt 来对它们进行排序。

但是,我希望两个非选项参数(源文件和目标文件)是强制性的,因此在调用程序时必须将它们作为参数,即使没有标志或其他参数也是如此。

这是我必须使用标志处理参数的简化版本:

while ((c = getopt(argc, argv, "i:d:btw:h:s:")) != -1) {
    switch (c) {
        case 'i': {
            i = (int)atol(optarg);
        }
        case 'd': {
            d = (int)atol(optarg);
        }
        case 'b':
            buf = 1;
            break;
        case 't':
            time = 1;
            break;
        case 'w':
            w = (int)atol(optarg);
            break;
        case 'h':
            h = (int)atol(optarg);
            break;
        case 's':
            s = (int)atol(optarg);
            break;
        default:
            break;
    }
}

如何编辑它以便也处理非选项参数?

我还希望能够在选项之前之后有非选项,那么如何处理?

c arguments argv getopt getopt-long
6个回答
38
投票

getopt
设置
optind
变量以指示下一个参数的位置。

在选项循环之后添加与此类似的代码:

if (argv[optind] == NULL || argv[optind + 1] == NULL) {
  printf("Mandatory argument(s) missing\n");
  exit(1);
}

编辑:

如果您想在常规参数之后允许选项,您可以执行类似的操作:

while (optind < argc) {
  if ((c = getopt(argc, argv, "i:d:btw:h:s:")) != -1) {
    // Option argument
    switch (c) {
    case 'i':
      i = (int)atol(optarg);
      break;
    case 'd':
      d = (int)atol(optarg);
      break;
    case 'b':
      buf = 1;
      break;
    case 't':
      time = 1;
      break;
    case 'w':
      w = (int)atol(optarg);
      break;
    case 'h':
      h = (int)atol(optarg);
      break;
    case 's':
      s = (int)atol(optarg);
      break;
    default:
      // bad or unknown option
      show_help();
      exit(1);
      break;
    }
  } else {
      // Regular argument
      <code to handle the argument>
      optind++;  // Skip to the next argument
  }
}

17
投票

可以在这里找到非常好的示例:GNU Libc代码:

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

int
main (int argc, char **argv)
{
int aflag = 0;
int bflag = 0;
char *cvalue = NULL;
int index;
int c;

opterr = 0;

while ((c = getopt (argc, argv, "abc:")) != -1)
switch (c)
{
case 'a':
    aflag = 1;
    break;
case 'b':
    bflag = 1;
    break;
case 'c':
    cvalue = optarg;
    break;
case '?':
    if (optopt == 'c')
    fprintf (stderr, "Option -%c requires an argument.\n", optopt);
    else if (isprint (optopt))
    fprintf (stderr, "Unknown option `-%c'.\n", optopt);
    else
    fprintf (stderr,
        "Unknown option character `\\x%x'.\n",
        optopt);
    return 1;
default:
    abort ();
}

printf ("aflag = %d, bflag = %d, cvalue = %s\n",
    aflag, bflag, cvalue);

for (index = optind; index < argc; index++)
printf ("Non-option argument %s\n", argv[index]);
return 0;
}

它允许在参数之前和之后有选项。我编译并运行了测试示例:

$ ./a.out aa ff bb -a -ctestparam hello
aflag = 1, bflag = 0, cvalue = testparam
Non-option argument aa
Non-option argument ff
Non-option argument bb
Non-option argument hello

3
投票

根据https://www.man7.org/linux/man-pages/man3/getopt.3.html

默认情况下,getopt() 在扫描时会排列 argv 的内容, 这样最终所有的非选项都会结束。另外两个 还实现了扫描模式。如果第一个字符为 optstring 是 '+' 或环境变量 POSIXLY_CORRECT 是 设置,然后一旦出现非选项参数,选项处理就会停止 遇到。如果 optstring 的第一个字符是 '-',那么 每个非选项 argv 元素都被当作参数来处理 字符代码为 1 的选项。(由程序使用 编写的目的是期望选项和其他 argv 元素 任何顺序并且关心两者的顺序。) 特殊参数“--”强制结束选项扫描,无论如何 扫描模式。


1
投票
int main(int argc, char** argv) {
    
    
  char* inputfile;
  char* outputfile;
  char* output_file_type;
  char* color_red;
  char* color_blue;
  char* color_green;
  
  
  int opt;
  
  if (argv[optind] == NULL || argv[optind + 1] == NULL) {
  printf("Mandatory argument(s) missing\n");
  exit(1);
    }
 
  
  while((opt = getopt(argc, argv, ":i:o:r:g:b:t:")) != -1){
    switch(opt){
      case 'i':
        inputfile = optarg;
        printf("Input file : %s\n",inputfile);
        break;  
      case 'o':
        outputfile = optarg;
        printf("Output File: %s\n",outputfile);
        break;
      case 't':
        output_file_type = optarg;
        printf("Output File type: %s\n", output_file_type);
        break;
      case 'r':
        color_red = optarg;
        printf("Color Red: %s\n",color_red);
        break;
      case 'g':
        color_green = optarg;
        printf("Color Green: %s\n",color_green);
        break;
      case 'b':
        color_blue = optarg;
        printf("Color Blue: %s\n",color_blue);
        break;
      case ':':
        printf("option needs a value\n");
        break;
      case '?':
        printf("unknown option: %c\n", optopt);
        break;
    }
  
  }
  
  for (; optind < argc; optind++){
      
      
       printf("Given extra arguments: %s\n", argv[optind]);
     
   
  }  


    return (EXIT_SUCCESS);
}

运行命令:

gcc main.c -o image
./image -i ./resource/input_file.bmp -o ./resource/output_file.bmp -t BPM -r 10 -g 24 -b 40

输出:

Input file : ./resource/input_file.bmp
Output File: ./resource/output_file.bmp
Output File type: BPM
Color Red: 10
Color Green: 24

0
投票

Mead's Guide to getopt 的作者指出

如果您想让 getopt 解析并在 while 循环中返回非选项参数(按指定的顺序),您必须通过在 optstring 前面放置一个减号 (-) 来指示它这样做。

提供的示例是“-:a:b:X”,其中减号 (-)“禁止 getopt 将所有非选项参数移动到命令行末尾”,冒号 (:)“禁止 getopt 显示错误消息”。

如果找到非选项参数,则 getopt 将返回整数值 1。


0
投票

我要继续输入我的答案。我已经测试过它,它符合

getopt
,如 here on Linux 所描述。请注意,此答案仅允许首先使用 选项,然后 之后不允许使用非选项。但这对于许多 CLI 工具来说是正常的。

我现在添加这个答案,因为我发现 Klas 答案中的编辑不起作用。


#include <stdio.h>
#include <stdlib.h>
#include <getopt.h>
#include <ctype.h>


int main (int argc, char * const argv[])
{
    int c;
    
    // Process option arguments
    while (-1 != (c = getopt(argc, argv, "abc:"))) {
         switch (c) {
     case 'a': printf("option: a\n"); break;
     case 'b': printf("option: b\n"); break;
     case 'c': printf("option: c with arg: \"%s\"\n", optarg); break;
     default:
         printf("unknown arg: %2X\n", optopt);
     }
    }
    
    // Process remaining arguments
    for (int i = optind; i < argc; ++i) {
        printf("non-option: %s\n", argv[i]);
    }
    
    return EXIT_SUCCESS;
}
© www.soinside.com 2019 - 2024. All rights reserved.