【问题标题】:Required and Optional Arguments Using Boost Library Program Options使用 Boost 库程序选项的必需和可选参数
【发布时间】:2011-07-20 17:04:33
【问题描述】:

我正在使用 Boost 程序选项库来解析命令行参数。

我有以下要求:

  1. 一旦提供“帮助”,所有其他选项都是可选的;
  2. 一旦未提供“帮助”,则需要所有其他选项。

我该如何处理?这是我处理这个的代码,我发现它非常多余,我认为一定有一个容易做到的,对吧?

#include <boost/program_options.hpp>
#include <iostream>
#include <sstream>
namespace po = boost::program_options;

bool process_command_line(int argc, char** argv,
                          std::string& host,
                          std::string& port,
                          std::string& configDir)
{
    int iport;

    try
    {
        po::options_description desc("Program Usage", 1024, 512);
        desc.add_options()
          ("help",     "produce help message")
          ("host,h",   po::value<std::string>(&host),      "set the host server")
          ("port,p",   po::value<int>(&iport),             "set the server port")
          ("config,c", po::value<std::string>(&configDir), "set the config path")
        ;

        po::variables_map vm;
        po::store(po::parse_command_line(argc, argv, desc), vm);
        po::notify(vm);

        if (vm.count("help"))
        {
            std::cout << desc << "\n";
            return false;
        }

        // There must be an easy way to handle the relationship between the
        // option "help" and "host"-"port"-"config"
        if (vm.count("host"))
        {
            std::cout << "host:   " << vm["host"].as<std::string>() << "\n";
        }
        else
        {
            std::cout << "\"host\" is required!" << "\n";
            return false;
        }

        if (vm.count("port"))
        {
            std::cout << "port:   " << vm["port"].as<int>() << "\n";
        }
        else
        {
            std::cout << "\"port\" is required!" << "\n";
            return false;
        }

        if (vm.count("config"))
        {
            std::cout << "config: " << vm["config"].as<std::string>() << "\n";
        }
        else
        {
            std::cout << "\"config\" is required!" << "\n";
            return false;
        }
    }
    catch(std::exception& e)
    {
        std::cerr << "Error: " << e.what() << "\n";
        return false;
    }
    catch(...)
    {
        std::cerr << "Unknown error!" << "\n";
        return false;
    }

    std::stringstream ss;
    ss << iport;
    port = ss.str();

    return true;
}

int main(int argc, char** argv)
{
  std::string host;
  std::string port;
  std::string configDir;

  bool result = process_command_line(argc, argv, host, port, configDir);
  if (!result)
      return 1;

  // Do the main routine here
}

【问题讨论】:

    标签: c++ boost boost-program-options required optional


    【解决方案1】:

    我自己也遇到过这个问题。解决方案的关键是函数po::store 填充variables_map,而po::notify 引发遇到的任何错误,因此可以在发送任何通知之前使用vm

    因此,根据Tim,根据需要将每个选项设置为必需,但在处理完帮助选项后运行po::notify(vm)。这样它将退出而不会抛出任何异常。现在,将选项设置为 required,缺少选项将导致抛出 required_option 异常,使用其 get_option_name 方法,您可以将错误代码减少为相对简单的 catch 块。

    作为附加说明,您的选项变量是通过po::value&lt; -type- &gt;( &amp;var_name ) 机制直接设置的,因此您不必通过vm["opt_name"].as&lt; -type- &gt;() 访问它们。

    Peters answer中提供了一个代码示例

    【讨论】:

    • 感谢您的回复。我认为它按预期工作。我还在下面为需要一个好例子的人发布了完整的程序。
    • 优秀的解决方案!官方文档应该用例子说明清楚。
    • @rcollyer 你能提供一个完整的工作示例吗?
    • @JonasStein 我可以,但Peter's 似乎还不错。如果这还不够,请告诉我。
    • @rcollyer sx 网站没有在视觉上连接两个答案,所以我错过了。我添加了一个注释。如果您不满意,请恢复。
    【解决方案2】:

    这里是根据 rcollyer 和 Tim 的完整程序,学分归属于:

    #include <boost/program_options.hpp>
    #include <iostream>
    #include <sstream>
    namespace po = boost::program_options;
    
    bool process_command_line(int argc, char** argv,
                              std::string& host,
                              std::string& port,
                              std::string& configDir)
    {
        int iport;
    
        try
        {
            po::options_description desc("Program Usage", 1024, 512);
            desc.add_options()
              ("help",     "produce help message")
              ("host,h",   po::value<std::string>(&host)->required(),      "set the host server")
              ("port,p",   po::value<int>(&iport)->required(),             "set the server port")
              ("config,c", po::value<std::string>(&configDir)->required(), "set the config path")
            ;
    
            po::variables_map vm;
            po::store(po::parse_command_line(argc, argv, desc), vm);
    
            if (vm.count("help"))
            {
                std::cout << desc << "\n";
                return false;
            }
    
            // There must be an easy way to handle the relationship between the
            // option "help" and "host"-"port"-"config"
            // Yes, the magic is putting the po::notify after "help" option check
            po::notify(vm);
        }
        catch(std::exception& e)
        {
            std::cerr << "Error: " << e.what() << "\n";
            return false;
        }
        catch(...)
        {
            std::cerr << "Unknown error!" << "\n";
            return false;
        }
    
        std::stringstream ss;
        ss << iport;
        port = ss.str();
    
        return true;
    }
    
    int main(int argc, char** argv)
    {
      std::string host;
      std::string port;
      std::string configDir;
    
      bool result = process_command_line(argc, argv, host, port, configDir);
      if (!result)
          return 1;
    
      // else
      std::cout << "host:\t"   << host      << "\n";
      std::cout << "port:\t"   << port      << "\n";
      std::cout << "config:\t" << configDir << "\n";
    
      // Do the main routine here
    }
    
    /* Sample output:
    
    C:\Debug>boost.exe --help
    Program Usage:
      --help                produce help message
      -h [ --host ] arg     set the host server
      -p [ --port ] arg     set the server port
      -c [ --config ] arg   set the config path
    
    
    C:\Debug>boost.exe
    Error: missing required option config
    
    C:\Debug>boost.exe --host localhost
    Error: missing required option config
    
    C:\Debug>boost.exe --config .
    Error: missing required option host
    
    C:\Debug>boost.exe --config . --help
    Program Usage:
      --help                produce help message
      -h [ --host ] arg     set the host server
      -p [ --port ] arg     set the server port
      -c [ --config ] arg   set the config path
    
    
    C:\Debug>boost.exe --host 127.0.0.1 --port 31528 --config .
    host:   127.0.0.1
    port:   31528
    config: .
    
    C:\Debug>boost.exe -h 127.0.0.1 -p 31528 -c .
    host:   127.0.0.1
    port:   31528
    config: .
    */
    

    【讨论】:

    • 您应该捕获boost::program_options::required_option,这样您就可以直接处理缺少必需选项的问题,而不是让std::exception 捕获它。
    • 端口的类型应该是无符号的。
    • 你应该只捕获 boost::program_options::error 这个。
    • 我同意 rcollyer。您还可以在特定的 catch 块中打印缺少的选项以及使用信息。当命令行不正确时,应用程序打印使用信息是标准的。我希望 boost 会在使用中输出一些需要或不需要的东西,但它看起来不像。因此,看起来我们必须自己在使用字符串中添加类似 [required] 的内容。这使得使用 required() 方法对我的吸引力降低。另一种选择可能是使用 boost::optional 然后使用这些变量编写一个验证子例程。
    【解决方案3】:

    您可以很容易地指定一个选项是必需的 [1],例如:

    ..., value<string>()->required(), ...
    

    但据我所知,没有办法表示 program_options 库的不同选项之间的关系。

    一种可能性是使用不同的选项集多次解析命令行,然后如果您已经检查了“帮助”,则可以根据需要设置其他三个选项再次解析。不过,我不确定我是否会认为这是对你现有的改进。

    【讨论】:

    • 是的,我可以输入-&gt;required(),但是用户无法通过--help 获取帮助信息(不提供所有其他必需选项),因为需要其他选项。跨度>
    • @Peter 您第一次会寻找 only 帮助,其他选项甚至不会出现在列表中。然后,如果他们没有传入帮助选项,那么只有这样你才会再次运行解析,这次传入其他三个选项,设置为必需,并且 not 帮助。这种方法可能需要第三组选项,将所有选项组合起来,用于打印使用信息。我很确定它会起作用,但 rcollyer 的方法更干净。
    【解决方案4】:
        std::string conn_mngr_id;
        std::string conn_mngr_channel;
        int32_t priority;
        int32_t timeout;
    
        boost::program_options::options_description p_opts_desc("Program options");
        boost::program_options::variables_map p_opts_vm;
    
        try {
    
            p_opts_desc.add_options()
                ("help,h", "produce help message")
                ("id,i", boost::program_options::value<std::string>(&conn_mngr_id)->required(), "Id used to connect to ConnectionManager")
                ("channel,c", boost::program_options::value<std::string>(&conn_mngr_channel)->required(), "Channel to attach with ConnectionManager")
                ("priority,p", boost::program_options::value<int>(&priority)->default_value(1), "Channel to attach with ConnectionManager")
                ("timeout,t", boost::program_options::value<int>(&timeout)->default_value(15000), "Channel to attach with ConnectionManager")
            ;
    
            boost::program_options::store(boost::program_options::parse_command_line(argc, argv, p_opts_desc), p_opts_vm);
    
            boost::program_options::notify(p_opts_vm);
    
            if (p_opts_vm.count("help")) {
                std::cout << p_opts_desc << std::endl;
                return 1;
            }
    
        } catch (const boost::program_options::required_option & e) {
            if (p_opts_vm.count("help")) {
                std::cout << p_opts_desc << std::endl;
                return 1;
            } else {
                throw e;
            }
        }
    

    【讨论】:

    • 这当然是一个有趣的选择。但是,它会迫使您重复帮助处理代码,虽然很小,但我倾向于避免它。
    猜你喜欢
    • 1970-01-01
    • 2017-06-14
    • 1970-01-01
    • 2011-12-31
    • 2017-01-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-05
    相关资源
    最近更新 更多