【问题标题】:Passing Negative Numbers into Getopt将负数传递给 Getopt
【发布时间】:2016-02-01 20:29:37
【问题描述】:

我需要将负数传递给 getopt,我想知道是否有一种简单的方法可以将 getopt 使用的前缀(即在 case 语句中标记的“-”字符)更改为不同的字符,如“--”或“+”。

是否需要使用 Getopt::Long 来更改前缀?

【问题讨论】:

    标签: c++ getopt negative-number


    【解决方案1】:

    我不相信有办法将命令行参数前缀更改为 - 以外的任何内容(或使用 getopt_long() 时的 --)。但是,如果您需要传递一个负数,您应该将参数定义为“required_argument”。例如,下面是一个简短的 GetOpt 方法,它使用 getopt_long_only 方法来获取命令行参数:

    // personaldetails.cpp
    
    // compile with:
    // g++ -std=c++11 personaldetails.cpp -o personaldetails
    #include <iostream>
    #include <string>
    #include <vector>
    #include <getopt.h>
    
    int main (int argc, char** argv)
    {
        // Define some variables
        std::string name = "" ;
        int age = 0 ;
        double weight = 0.0 ;
    
        // Setup the GetOpt long options.
        std::vector<struct option> longopts ;
        longopts.push_back({"Name",  required_argument, 0, 'N'}) ; 
        longopts.push_back({"Age",   required_argument, 0, 'A'}) ; // <- IMPORTANT
        longopts.push_back({"Weight",required_argument, 0, 'W'}) ; // <- IMPORTANT
        longopts.push_back({0,0,0,0}) ;
    
        // Now parse the options
        while (1)
        {
            int c(0) ;
            int option_index = -1;
    
            c = getopt_long_only (argc, argv, "A:N:W:",
                                  &longopts[0], &option_index);
    
            /* Detect the end of the options. */
            if (c == -1) break;
    
            // Now loop through all of the options to fill them based on their values
            switch (c)
            {
                case 0:
                    /* If this option set a flag, do nothing else now. */
                    break ;
                case '?':
                    // getopt_long_omly already printed an error message.
                    // This will most typically happen when then an unrecognized
                    // option has been passed.
                    return 0 ;
                case 'N':
                    name = std::string(optarg) ;
                    break ;
                case 'A':
                    age = std::stoi(optarg) ;
                    break ;
                case 'W':
                    weight = std::stod(optarg) ;
                    break ;
                default:
                    // Here's where we handle the long form arguments
                    std::string opt_name( longopts[option_index].name ) ;
                    if (opt_name.compare("Name")==0) {
                        name = std::string(optarg) ;
                    } else if (opt_name.compare("Age")==0) {
                        age = std::stoi(optarg) ;
                    } else if (opt_name.compare("Weight")==0) {
                        weight = std::stod(optarg) ;
                    }
                    break ;
            }
        }
    
        // Print the persons details
        std::cout << "Name  : " << name << std::endl;
        std::cout << "Age   : " << age << std::endl;
        std::cout << "Weight: " << weight << std::endl;
    
        return 0 ;
    }
    

    这里的关键部分是,在longopts 中,我设置了要转换为整数和双精度的参数以得到required_argument。这告诉 GetOpt 在您在命令行上声明后期望另一个参数。这意味着 GetOpt 将在您的命令行参数之后读取参数作为该命令行参数的参数。对于我们想要传递单个char 参数(即-N-A-W)的情况,这就是将"N:A:W:" 传递给getopt 很重要的地方。 : 对单个字符参数的作用与required_argument 对长格式的作用基本相同。

    运行我能做的脚本:

    $ ./personaldetails -Name Sally -Age -45 -Weight 34.5
    Name  : Sally
    Age   : -45
    Weight: 34.5
    
    $./personaldetails -N Sally -A -45 -W -34.5
    Name  : Sally
    Age   : -45
    Weight: -34.5
    

    请注意,由于脚本使用getopt_long_only(),我可以使用单个- 传递参数参数的长格式。

    【讨论】:

      【解决方案2】:

      使用-- 例如yourcommand -- -5 会成功的。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-11-10
        • 2021-11-09
        • 1970-01-01
        • 1970-01-01
        • 2019-07-07
        • 2023-03-25
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多