繁体   English   中英

将负数传递给 Getopt

[英]Passing Negative Numbers into Getopt

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

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

我不相信有一种方法可以将命令行参数前缀更改为- (或--在使用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我已经设置了我要转换为 integer 和 double 的参数以具有required_argument 这告诉 GetOpt 在您在命令行上声明它之后期待另一个参数。 这意味着 GetOpt 将在您的命令行参数之后读取参数作为该命令行参数的参数 对于我们想要传递单个char参数(即-N-A-W )的情况,这就是将"N:A:W:"传递给 getopt 的重要性。 :对单个 char 参数的作用与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()我可以使用单个-传递参数参数的长格式。

使用--例如yourcommand -- -5可以解决问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM