账号密码登录
微信安全登录
微信扫描二维码登录

登录后绑定QQ、微信即可实现信息互通

手机验证码登录
找回密码返回
邮箱找回 手机找回
注册账号返回
其他登录方式
分享
  • 收藏
    X
    c++如何使用getopt_long解析长参数啊?
    51
    0

    这是别人写的代码

    #include <getopt.h>
    #include <string.h>
    
    struct globalArgs_t {
        int noIndex;                /* -I option */
        char *langCode;             /* -l option */
        const char *outFileName;    /* -o option */
        FILE *outFile;
        int verbosity;              /* -v option */
        char **inputFiles;          /* input files */
        int numInputFiles;          /* # of input files */
        int randomized;             /* --randomize option */
    } globalArgs;
     
    static const char *optString = "Il:o:vh?";
     
    static const struct option longOpts[] = {
        { "no-index", no_argument, NULL, 'I' },
        { "language", required_argument, NULL, 'l' },
        { "output", required_argument, NULL, 'o' },
        { "verbose", no_argument, NULL, 'v' },
        { "randomize", no_argument, NULL, 0 },
        { "help", no_argument, NULL, 'h' },
        { NULL, no_argument, NULL, 0 }
    };
    
    opt = getopt_long( argc, argv, optString, longOpts, &longIndex );
        while( opt != -1 ) {
            switch( opt ) {
                case 'I':
                    globalArgs.noIndex = 1; /* true */
                    break;
                     
                case 'l':
                    globalArgs.langCode = optarg;
                    break;
                     
                case 'o':
                    globalArgs.outFileName = optarg;
                    break;
                     
                case 'v':
                    globalArgs.verbosity++;
                    break;
                     
                case 'h':   /* fall-through is intentional */
                case '?':
                    display_usage();
                    break;
     
                case 0:     /* long option without a short arg */
                    if( strcmp( "randomize", longOpts[longIndex].name ) == 0 ) {
                        globalArgs.randomized = 1;
                    }
                    break;
                     
                default:
                    /* You won't actually get here. */
                    break;
            }
             
            opt = getopt_long( argc, argv, optString, longOpts, amp;longIndex );
        }

    我一定要给每一个参数指定短选项吗?

    我只想使用长选项

    static const char *optString = "Il:o:vh?";

    这里面的冒号有什么讲究啊

    0
    打赏
    收藏
    点击回答
        全部回答
    • 共 0 条
    • 舊言虐心 普通会员 1楼

      在C++中,你可以使用getopt_long函数来解析长参数。getopt_long函数是一个标准库函数,用于处理长参数的选项。

      以下是一个简单的示例:

      ```cpp

      include

      include

      include

      include

      int main(int argc, char** argv) { // 获取选项字符串 std::string option_string = "long_option";

      // 解析选项字符串
      int option_index = option_string.find_first_not_of(" ");
      if (option_index == std::string::npos) {
          std::cout << "Option not found" << std::endl;
          return 1;
      }
      
      std::vector<std::string> options;
      options.push_back(option_string.substr(option_index));
      
      // 使用选项
      for (size_t i = 0; i < options.size(); i++) {
          if (options[i].find("long_option") != std::string::npos) {
              // 当找到"long_option"时,执行相应的操作
              std::cout << "Long option found" << std::endl;
              break;
          }
      }
      
      return 0;
      

      } ```

      在这个示例中,我们首先获取选项字符串,然后使用find_first_not_of函数找到第一个非空的选项。然后,我们使用这个选项字符串来创建一个选项的向量。

      在循环中,我们使用find函数找到每个选项的第一个非空字符,然后使用substr函数来获取这个字符之后的部分,这就是选项的名称。如果找到"long_option",我们就执行相应的操作。

      注意,这个示例假设你的参数是用空格分隔的。如果你的参数是用制表符分隔的,你可能需要使用不同的方法来解析选项。

    更多回答
    扫一扫访问手机版
    • 回到顶部
    • 回到顶部