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

91

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

我的要求如下:

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

我该如何处理这个问题?以下是我处理此问题的代码,但我发现它非常冗长,我认为一定有更简单的方法,对吗?

#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
}
4个回答

113
我自己也遇到了这个问题。解决方案的关键在于函数po::store填充了variables_map,而po::notify则会引发任何错误,因此可以在发送任何通知之前使用vm。所以,根据Tim的建议,将每个选项设置为所需的,但在处理帮助选项后运行po::notify(vm)。这样它将退出而不会抛出任何异常。现在,由于选项被设置为必需,缺少选项将导致抛出required_option异常,并且使用其get_option_name方法,您可以将错误代码减少到一个相对简单的catch块。
另外需要注意的是,您的选项变量是通过po::value< -type- >( &var_name )机制直接设置的,因此您不必通过vm["opt_name"].as< -type- >()访问它们。 Peters answer提供了一个代码示例

谢谢你的回复。我认为它的工作符合预期。我也在下面发布了完整的程序,以便需要一个好的示例的人。 - Peter Lee
6
好的方案!官方文档应该通过示例来阐明。 - russoue
@rcollyer,你能提供一个完整的可工作的例子吗? - Jonas Stein
@JonasStein 我可以这样做,但是Peter的看起来已经很好了。如果那不够,请告诉我。 - rcollyer
1
@rcollyer,sx网站在视觉上没有将这两个答案连接起来,所以我错过了它。我已经添加了一条注释。如果您不舒服,请恢复原样。 - Jonas Stein

55

这是完整的程序,按照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: .
*/

6
你应该捕获boost::program_options::required_option,这样你就可以直接处理缺少必需选项的情况,而不是让它被std::exception捕获。 - rcollyer
端口应该是无符号类型。 - user67416
3
你应该只捕获 boost::program_options::error 异常。 - CreativeMind
我同意rcollyer的观点。在特定的catch块中,您还可以打印缺少的选项以及使用信息。当命令行不正确时,应用程序通常会打印使用信息。我希望boost能够在使用信息中输出所需或不需要的内容,但看起来它似乎没有这样做。因此,我们似乎必须自己将[required]之类的内容添加到使用字符串中。这使得对required()方法的使用对我来说不太有吸引力。另一个选择可能是使用boost::optional<T>,然后编写一个验证子例程来使用这些变量。 - shawn1874

13

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

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

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

一种可能性是使用不同的选项集多次解析命令行,然后如果你已经检查了“help”,你可以再次解析,其中另外三个选项都设置为必需。不过我不确定这是否比您现有的方案更好。


2
是的,你说得对,我可以放置 ->required(),但是用户就无法通过 --help(不提供所有其他必需选项)获得帮助信息,因为其他选项是必需的。 - Peter Lee
@Peter,你应该只寻求第一次的帮助,其他选项甚至不会在列表中显示。然后,如果他们没有传递帮助选项,那么你才会再次运行解析,这次传递其他三个选项,设置为必需,而不是帮助。这种方法可能需要第三组选项,将它们合并起来用于打印使用信息。我很确定它可以工作,但rcollyer的方法更简洁。 - Tim Sylvester

1
    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;
        }
    }

这确实是一个有趣的选择。但是,它会强制你重复帮助处理代码,虽然很小,但我倾向于避免它。 - rcollyer

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接