getopt_long 函數中“:”的意義是什麼?

科瓦茨

這裡getopt_long例子中,為什麼短選項用冒號 at 分隔,為什麼像這樣組合在一起?abc:d:f:abc

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

/* Flag set by ‘--verbose’. */
static int verbose_flag;

int
main (int argc, char **argv)
{
  int c;

  while (1)
    {
      static struct option long_options[] =
        {
          /* These options set a flag. */
          {"verbose", no_argument,       &verbose_flag, 1},
          {"brief",   no_argument,       &verbose_flag, 0},
          /* These options don’t set a flag.
             We distinguish them by their indices. */
          {"add",     no_argument,       0, 'a'},
          {"append",  no_argument,       0, 'b'},
          {"delete",  required_argument, 0, 'd'},
          {"create",  required_argument, 0, 'c'},
          {"file",    required_argument, 0, 'f'},
          {0, 0, 0, 0}
        };
      /* getopt_long stores the option index here. */
      int option_index = 0;

      c = getopt_long (argc, argv, "abc:d:f:",
                       long_options, &option_index);

      /* Detect the end of the options. */
      if (c == -1)
        break;

      switch (c)
        {
        case 0:
          /* If this option set a flag, do nothing else now. */
          if (long_options[option_index].flag != 0)
            break;
          printf ("option %s", long_options[option_index].name);
          if (optarg)
            printf (" with arg %s", optarg);
          printf ("\n");
          break;

        case 'a':
          puts ("option -a\n");
          break;

        case 'b':
          puts ("option -b\n");
          break;

        case 'c':
          printf ("option -c with value `%s'\n", optarg);
          break;

        case 'd':
          printf ("option -d with value `%s'\n", optarg);
          break;

        case 'f':
          printf ("option -f with value `%s'\n", optarg);
          break;

        case '?':
          /* getopt_long already printed an error message. */
          break;

        default:
          abort ();
        }
    }
信噪比

在 GNU 的頁面上,據說

此字符串中的選項字符後可以跟一個冒號 ( :) 以指示它需要一個必需的參數。

例如,xy:zq接受選項x, y, z, q; y需要一個額外的參數。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

異步函數的 neverthrow 有什麼意義?

使用“隱式解包可選”作為函數參數類型有什麼意義?

那麼,為什麼我必須在基類中定義虛函數?

僅在類型構造函數上多態的實例有什麼意義?

getRangeAt() 函數中索引參數的目的是什麼?

未定義 LVGL 函數 - 為什麼?

getopt_long() 的段错误

為什麼我不能在函數中設置我的構造函數?

如果中間件函數沒有在同一個文件中定義,為什麼我的中間件函數不起作用?

在某些 C++ 標準庫中定義類而不是函數有什麼好處?

使用getopt_long处理用户错误

getopt_long不传递参数

為什麼此參數包中的函數調用會向後求值?

在 Solidity 合約中,括號內的函數參數本身有什麼作用?

“”有什麼用?和“:”在具有指針作為參數的函數中?

R中`subset`函數的邏輯參數究竟是什麼?

“glfwSetErrorCallback”函數返回什麼?

PHP 中匿名函數和靜態匿名函數到底有什麼區別?

Visual Studio NugetPackageManager 界面中“Version”列的意義是什麼?(與“已安裝”列不同)

C++20 中兩種類型的模塊文件(接口和實現)有什麼意義?

為什麼這個自定義反向函數的行為很奇怪?

numpy中`2097184`的含義是什麼?

不太明白我在代碼中的 forEach 函數中的 i 代表什麼

為什麼在 Python 中的函數中修改了列表?

我什麼時候需要 Spring 實體中的構造函數?

為什麼 $Null 驗證在 powershell 函數中失敗?

為什麼作者在LazyColumn 中為items 函數添加key(task)?

為什麼多態函數不能接受 Scala 中的通配符(存在)類型?

回調函數中的詞法環境是什麼?