C++ 使用Boost库程序选项的必需参数和可选参数
我正在使用Boost程序选项库解析命令行参数 我有以下要求:C++ 使用Boost库程序选项的必需参数和可选参数,c++,boost,boost-program-options,required,optional,C++,Boost,Boost Program Options,Required,Optional,我正在使用Boost程序选项库解析命令行参数 我有以下要求: 提供“帮助”后,所有其他选项都是可选的 一旦没有提供“帮助”,所有其他选项都是必需的 我该怎么处理?这是我处理这个问题的代码,我发现它非常冗余,我认为一定有一个简单的方法,对吗 #include <boost/program_options.hpp> #include <iostream> #include <sstream> namespace po = boost::program_option
#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
}
#包括
#包括
#包括
名称空间po=boost::program\u选项;
bool进程命令行(int argc,char**argv,
std::字符串和主机,
std::字符串和端口,
std::string和configDir)
{
国际机场;
尝试
{
选项描述描述(“程序使用”,1024512);
说明添加选项()
(“帮助”,“生成帮助消息”)
(“主机,h”,po::值(&host),“设置主机服务器”)
(“端口,p”,po::value(&iport),“设置服务器端口”)
(“config,c”,po::value(&configDir),“设置配置路径”)
;
变量映射虚拟机;
po::store(po::parse_命令行(argc、argv、desc)、vm);
po::通知(vm);
if(vm.count(“帮助”))
{
std::cout您可以很容易地指定需要一个选项[],例如:
…,value()->required()。。。
但据我所知,在程序选项库中无法表示不同选项之间的关系
一种可能是使用不同的选项集多次解析命令行,如果您已经选中了“帮助”你可以再次按照所设定的三个其他选项进行解析。但是我不确定我是否会认为你有什么改进。 < P>我自己已经解决了这个问题。解决方案的关键是函数<代码> P::存储< /代码>填充<代码> ValuabsSypMy/<代码> <代码> P::通知< /代码>引发任何错误。红色,因此可以在发送任何通知之前使用vm
因此,根据,根据需要将每个选项设置为required,但运行po::notify(vm)
处理帮助选项后。这样它将退出,不会抛出任何异常。现在,当选项设置为“必需”时,丢失的选项将导致抛出异常,使用其get\u option\u name
方法,您可以将错误代码减少为相对简单的catch
块
另外,您的选项变量是通过po::value<-type->(&var\u name)
机制直接设置的,因此您不必通过vm[“opt\u name”]”访问它们
中提供了一个代码示例,这是根据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: .
*/
#包括
#包括
#包括
名称空间po=boost::program\u选项;
bool进程命令行(int argc,char**argv,
std::字符串和主机,
std::字符串和端口,
std::string和configDir)
{
国际机场;
尝试
{
选项描述描述(“程序使用”,1024512);
说明添加选项()
(“帮助”,“生成帮助消息”)
(“主机,h”,po::value(&host)->required(),“设置主机服务器”)
(“端口,p”,po::value(&iport)->required(),“设置服务器端口”)
(“config,c”,po::value(&configDir)->required(),“设置配置路径”)
;
变量映射虚拟机;
po::store(po::parse_命令行(argc、argv、desc)、vm);
if(vm.count(“帮助”))
{
std::coutstd::string conn\u mngr\u id;
标准:字符串连接mngr通道;
国际优先权;
int32_t超时;
boost::program_options::options_description p_opts_desc(“program options”);
boost::program\u options::variables\u map p\u opts\u vm;
试一试{
p_opts_desc.add_options()
(“帮助,h”,“生成帮助消息”)
(“id,i”,boost::program_options::value(&conn_mngr_id)->required(),“用于连接到ConnectionManager的id”)
(“通道,c”,boost::program_options::value(&conn_mngr_channel)->required(),“连接到ConnectionManager的通道”)
(“优先级,p”,boost::program_options::value(&priority)->默认值(1),“连接到ConnectionManager的通道”)
(“timeout,t”,boost::program_options::value(&timeout)->默认_值(15000),“连接到ConnectionManager的通道”)
;
boost::program_options::store(boost::program_options::parse_命令行(argc、argv、p_opts_desc)、p_opts_vm);
boost::program\u options::notify(p\u opts\u vm);
if(p_opts_vm.count(“帮助”)){
std::cout yes,我可以将->required()
,但用户无法通过--help
获得帮助信息(不提供所有其他必需选项),因为其他选项是必需的。@Peter如果您第一次只查找“帮助”,其他选项甚至不在列表中。然后,如果它们没有传入“帮助”选项,那么您只会再次运行解析,这次传入其他三个选项,设置为“必需”,而不是“帮助”。这种方法可能需要第三个选项一组选项,将所有选项组合在一起,用于打印使用信息。我很确定它会起作用,但rcollyer的方法更干净。谢谢你的回复。我认为它能如预期的那样起作用。我还为需要一个好例子的人发布了下面的完整程序。非常好的解决方案!官方文档应该请举例说明。@rcollyer你能提供一个完整的工作示例吗?@JonasStein我可以,但看起来很好。如果这还不够,请告诉我。@rcollyer sx网站没有直观地连接这两个答案,所以我错过了。我
#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: .
*/
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;
}
}