c++ - C/C++ getopt optstring 语法

标签 c++ c unix command-line-arguments getopt

使用 C++ 中 unistd.h 中包含的 getopt 函数,有没有一种方法可以构造 optstring,使得...

[-a] [-f "reg_expr"] out_file1 [[-f "reg_expr"] out_file2 ...] 可能吗?

这是一项家庭作业,但重点不在这个特定的子任务上。

在我的脑海中,我想指定以下逻辑:

(一个参数),(无限多个 f 参数和 2 个必需的(子)参数),...(无限多个通用参数)

也许我对 getopt 函数的理解存在根本性的缺陷。我还看到了 getopt_long。也许这就是我所缺少的。

我最初起草了这个,它起作用了,但我遇到了 getopt 函数并认为它可能做得更好。

int outFileFlags;
int outFileMode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
int i = 1;
while (i < argc){
    if (i == 1 && strcmp( argv[i], "-a") == 0){
        cout << "append flag set" << endl;
        outFileFlags = O_RDWR | O_APPEND;
        i++;
        continue;
    }
    else {
        outFileFlags = O_TRUNC | O_RDWR | O_CREAT;
    }
    if (strcmp( argv[i], "-f") == 0 && i+2 <= argc){
        cout << "   regx = " << argv[i+1] << endl;
        cout << "   fn = " << argv[i+2] << endl;
        i = i+3;
        continue;
    }
    else {
        cout << "   regx = none" << endl;
        cout << "   fn = " << argv[i] << endl;
        i++;
        continue;
    }
}

注意:假设这是为 unix 环境编写的。我不认为我可以使用标准库中的任何东西。我只包括 std::cout 用于测试目的。

我很乐意详细说明作业的任何细节。然而,主要问题围绕着 optstring 的语法。我目前只知道 : 意思是必需的,::意思是可选的,有没有办法指定像正则表达式通配符一样重复的参数 *?

编辑:

我敢肯定这是草率的,因为我不认为 getopt 旨在处理每个选项的多个参数,但它确实起到了作用......

int main(int argc, char *argv[]){
    char c;
    int iterations = 0;
    while (*argv) {
        optind = 1;
        if (iterations == 0){
            opterr = 0;
            c = getopt(argc, argv, "a");
            if(c == 'a'){
                //~ APPEND SET
            }
            else if(c=='?'){
                optind--;
            }
        }
        while ((c = getopt(argc, argv, "f:")) != -1) {
            if (c == 'f'){
                //~ REGEX = optarg
                if (optind < argc && strcmp(argv[optind], "-f") != 0) {
                    //~ FILENAME = argv[optind]
                    optind++;
                }
                else {
                    errno = 22;
                    perror("Error");
                    exit(errno);
                }
            }
            else {
                errno = 22;
                perror("Error");
                exit(errno);
            }
        }
        argc -= optind;
        argv += optind;
        iterations++;
        //~ REMAINING FILES = *argv
    }
}

最佳答案

您需要为每组选项和输出文件名执行单独的 getopt 循环。

group_index = 0;
while (*argv) {
  optreset = 1;
  optind = 1;
  while ((ch = getopt(argc, argv, "af:")) != -1) {
    switch (ch) {
      /* process options */
    }
  }
  argc -= optind;
  argv += optind;
  outfile[group_index++] = *argv;
  argc--;
  argv++;
}

关于c++ - C/C++ getopt optstring 语法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13447918/

相关文章:

bash - 我们如何根据 unix 中的给定日期获取工作日

c++ - 调整大小的 WTL 布局

c++ - 如何以更好的性能传递和共享 shared_ptr 所有权?

c++ - 当我编译我的程序时,这个内存映射意味着什么?

c - 为什么我的 C 代码无法运行或运行速度太慢?

c - C语言中的大整数?

C++ 不同的子类需要不同的参数

c - 我不断收到此错误 : pointer value used where a floating point value was expected

c - 如何离开(中断)waitpid()函数?

regex - 在 sed 模式中指定 "-"