<bdo id='KDl7l'></bdo><ul id='KDl7l'></ul>

    <tfoot id='KDl7l'></tfoot>

    <small id='KDl7l'></small><noframes id='KDl7l'>

      <i id='KDl7l'><tr id='KDl7l'><dt id='KDl7l'><q id='KDl7l'><span id='KDl7l'><b id='KDl7l'><form id='KDl7l'><ins id='KDl7l'></ins><ul id='KDl7l'></ul><sub id='KDl7l'></sub></form><legend id='KDl7l'></legend><bdo id='KDl7l'><pre id='KDl7l'><center id='KDl7l'></center></pre></bdo></b><th id='KDl7l'></th></span></q></dt></tr></i><div id='KDl7l'><tfoot id='KDl7l'></tfoot><dl id='KDl7l'><fieldset id='KDl7l'></fieldset></dl></div>
    1. <legend id='KDl7l'><style id='KDl7l'><dir id='KDl7l'><q id='KDl7l'></q></dir></style></legend>

      使用 Boost 库程序选项的必需参数和可选参数

      时间:2023-07-19
        <i id='L145Z'><tr id='L145Z'><dt id='L145Z'><q id='L145Z'><span id='L145Z'><b id='L145Z'><form id='L145Z'><ins id='L145Z'></ins><ul id='L145Z'></ul><sub id='L145Z'></sub></form><legend id='L145Z'></legend><bdo id='L145Z'><pre id='L145Z'><center id='L145Z'></center></pre></bdo></b><th id='L145Z'></th></span></q></dt></tr></i><div id='L145Z'><tfoot id='L145Z'></tfoot><dl id='L145Z'><fieldset id='L145Z'></fieldset></dl></div>

          <tbody id='L145Z'></tbody>
      • <small id='L145Z'></small><noframes id='L145Z'>

          <tfoot id='L145Z'></tfoot>

                <bdo id='L145Z'></bdo><ul id='L145Z'></ul>

              • <legend id='L145Z'><style id='L145Z'><dir id='L145Z'><q id='L145Z'></q></dir></style></legend>
                本文介绍了使用 Boost 库程序选项的必需参数和可选参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

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

                I'm using Boost Program Options Library to parse the command line arguments.

                我有以下要求:

                1. 一旦提供了帮助",所有其他选项都是可选的;
                2. 如果未提供帮助",则需要所有其他选项.

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

                How I can deal with this? Here is the my code handling this, and I found it's very redundant, and I think there must be an easy to do, right?

                #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 << "
                ";
                            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>() << "
                ";
                        }
                        else
                        {
                            std::cout << ""host" is required!" << "
                ";
                            return false;
                        }
                
                        if (vm.count("port"))
                        {
                            std::cout << "port:   " << vm["port"].as<int>() << "
                ";
                        }
                        else
                        {
                            std::cout << ""port" is required!" << "
                ";
                            return false;
                        }
                
                        if (vm.count("config"))
                        {
                            std::cout << "config: " << vm["config"].as<std::string>() << "
                ";
                        }
                        else
                        {
                            std::cout << ""config" is required!" << "
                ";
                            return false;
                        }
                    }
                    catch(std::exception& e)
                    {
                        std::cerr << "Error: " << e.what() << "
                ";
                        return false;
                    }
                    catch(...)
                    {
                        std::cerr << "Unknown error!" << "
                ";
                        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
                }
                

                推荐答案

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

                I've run into this issue myself. The key to a solution is that the function po::store populates the variables_map while po::notify raises any errors encountered, so vm can be used prior to any notifications being sent.

                因此,根据 蒂姆,根据需要将每个选项设置为必需,但在处理了帮助选项后运行 po::notify(vm).这样它就会退出而不会抛出任何异常.现在,将选项设置为必需,缺少选项将导致 required_option 异常被抛出并使用它的 get_option_name 方法你可以将你的错误代码减少到一个相对简单的 catch堵塞.

                So, as per Tim, set each option to required, as desired, but run po::notify(vm) after you've dealt with the help option. This way it will exit without any exceptions thrown. Now, with the options set to required, a missing option will cause a required_option exception to be thrown and using its get_option_name method you can reduce your error code to a relatively simple catch block.

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

                As an additional note, your option variables are set directly via the po::value< -type- >( &var_name ) mechanism, so you don't have to access them through vm["opt_name"].as< -type- >().

                Peters 的回答

                这篇关于使用 Boost 库程序选项的必需参数和可选参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:boost::shared_ptr 和 std::shared_ptr 的同居 下一篇:Boost.Test:寻找一个有效的非平凡测试套件示例/教程

                相关文章

              • <tfoot id='p4WPL'></tfoot>
                <i id='p4WPL'><tr id='p4WPL'><dt id='p4WPL'><q id='p4WPL'><span id='p4WPL'><b id='p4WPL'><form id='p4WPL'><ins id='p4WPL'></ins><ul id='p4WPL'></ul><sub id='p4WPL'></sub></form><legend id='p4WPL'></legend><bdo id='p4WPL'><pre id='p4WPL'><center id='p4WPL'></center></pre></bdo></b><th id='p4WPL'></th></span></q></dt></tr></i><div id='p4WPL'><tfoot id='p4WPL'></tfoot><dl id='p4WPL'><fieldset id='p4WPL'></fieldset></dl></div>
                1. <small id='p4WPL'></small><noframes id='p4WPL'>

                    <bdo id='p4WPL'></bdo><ul id='p4WPL'></ul>
                  <legend id='p4WPL'><style id='p4WPL'><dir id='p4WPL'><q id='p4WPL'></q></dir></style></legend>