需要和使用Boost库程序选项可选参数 [英] Required and Optional Arguments Using Boost Library Program Options
问题描述
我使用升压程序选项库解析命令行参数。
我有以下要求:
- 当帮助提供,所有其他选项都是可选的;
- 未提供一次帮助,所有其他选项都是必需的。
我该如何面对呢?这里是我的code处理这,我发现这是非常多余的,而且我认为必须有一个容易的事,对吧?
的#include<升压/ program_options.hpp>
#包括LT&;&iostream的GT;
#包括LT&;&sstream GT;
命名空间PO =的boost :: program_options;布尔process_command_line(INT ARGC,字符** argv的,
标准::字符串&安培;主办,
标准::字符串&安培;港口,
标准::字符串&安培; configDir)
{
INT I端口; 尝试
{
PO :: options_description递减(程序的使用,1024,512);
desc.add_options()
(帮助,生产帮助信息)
(主机h,PO ::值<标准::字符串>(安培;主机),设置主机服务器)
(端口,P,PO ::值< INT>(安培; I端口),设置服务器端口)
(配置,C,PO ::值<标准::字符串>(安培; configDir)设置的配置路径)
; PO :: variables_map VM;
PO ::店(PO :: parse_command_line(ARGC,ARGV,递减),VM);
PO ::通知(VM); 如果(vm.count(帮助))
{
性病::法院LT&;<说明<< \\ n;
返回false;
} //必须有处理之间的关系的简单方法
//选择帮助和主人 - 端口 - 配置
如果(vm.count(主机))
{
性病::法院LT&;< 主持人:<< VM [主机]为<标准::字符串>()<< \\ n;
}
其他
{
性病::法院LT&;< \\主\\是必需的! << \\ n;
返回false;
} 如果(vm.count(端口))
{
性病::法院LT&;< 端口:<< VM [端口]为<&INT GT;()<< \\ n;
}
其他
{
性病::法院LT&;< \\端口\\是必需的! << \\ n;
返回false;
} 如果(vm.count(配置))
{
性病::法院LT&;< 配置:<< VM [配置]为<标准::字符串>()<< \\ n;
}
其他
{
性病::法院LT&;< \\设置\\是必需的! << \\ n;
返回false;
}
}
赶上(性病::例外急症)
{
的std :: CERR<< 错误:<< e.what()&所述;&下; \\ n;
返回false;
}
抓住(...)
{
的std :: CERR<< 未知错误! << \\ n;
返回false;
} 的std :: stringstream的SS;
SS<< I端口;
端口= ss.str(); 返回true;
}INT主(INT ARGC,字符** argv的)
{
标准::字符串主机;
性病::串口;
标准::字符串configDir; 布尔结果= process_command_line(ARGC,ARGV,主机,端口,configDir);
如果(!结果)
返回1; //执行主程序这里
}
我碰到这个问题我自己。一个解决方案的关键是函数 PO ::店
填充 variables_map
,而 PO ::通知
引发遇到的任何错误,所以 VM
可被发送之前的任何通知。
所以,作为每<一href=\"http://stackoverflow.com/questions/5395503/required-and-optional-arguments-using-boost-library-program-options/5517313#5517313\">Tim,设置每个选项所需,根据需要,但运行 PO ::通知(VM)
您已经处理了帮助选项之后。这样,它就会退出,不抛出任何异常。现在,设置为所需要的场所,缺少选项将导致<一href=\"http://www.boost.org/doc/libs/1_46_1/doc/html/boost/program_options/required_option.html\"><$c$c>required_option$c$c>被抛出的异常,并使用其 get_option_name
方法可以减少你的错误code到一个相对简单的抓
块。
作为一个附加的音符,你的选择变量直接通过 PO ::值&LT设置; -type-&GT;(安培; VAR_NAME)。
机制,这样你就不必通过 VM [opt_name]来访问它们为&lt; -type-&GT;()
。
I'm using Boost Program Options Library to parse the command line arguments.
I have the following requirements:
- Once "help" is provided, all the other options are optional;
- Once "help" is not provided, all the other options are required.
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 << "\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
}
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.
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.
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- >()
.
这篇关于需要和使用Boost库程序选项可选参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!