我想让程序有两个选项,起始地址和结束地址,以便程序选项如下:
--start_address 0xc0000000 --end_address 0xffffffff
Run Code Online (Sandbox Code Playgroud)
是否可以options_description采用这种十六进制输入?我是否必须将输入视为字符串并将其转换为十六进制值.我现在有这个:
po::options_description desc("Allowed options");
desc.add_options()
("help,h", "display this help message")
("path,p", po::value<std::string>(), "Executable file path")
("start_address,s", po::value<std::string>(), "Start address")
("end_address,e", po::value<std::string>(), "End address")
;
Run Code Online (Sandbox Code Playgroud)
可以boost::lexical_cast做这样的转换吗?
So I'm working off one of the examples for Boost program_options library, and I wanted to try setting a default value for one of the multiple-values/ vector-values, but it doesn't seem to work. As I think is suggested here to work.
What I've modified is on about line 40:
po::options_description config("Configuration");
config.add_options()
("optimization", po::value<int>(&opt)->default_value(10),
"optimization level")
("include-path,I", o::value< vector<string> >()->default_value(vector<string>(),"SOMETHING")->composing(), "include path")
;
Run Code Online (Sandbox Code Playgroud)
When I compile this small change, I expect that when no -I option is passed that …
在如下配置中; 有没有办法处理各个部分.
我正在寻找一种方法来以可靠的方式验证下面的各个"服务器"部分.
[basic]
number_of_servers=3
[server]
ip=10.20.30.40
password=sdfslkhf
[server]
ip=10.20.30.41
password=sdfslkhf
[server]
ip=10.20.30.42
password=sdfslkhf
[server]
password=sdfslkhf
[server]
ip=10.20.30.42
Run Code Online (Sandbox Code Playgroud) 目前我正在使用boost::program_options解析BeagleBoard(基于ARM的处理器)上的配置文件.我的程序是多线程的,并与boost 1.45 multithreaded库相关联.
我的程序似乎在解析配置文件时会挂起
namespace po = boost::program_options;
po::options_description desc("Options");
uint32_t option1=0;
std::vector<std::string> optionsString;
std::cout<<"Before adding options"<<std::endl;
desc.add_options()
("option1",
po::value<uint32_t>(&option1), "...")
("finaloption",
po::value<std::vector<std::string> >(&optionsString)->multitoken(), "string of options");
//Never gets here
std::cout<<"After adding options"<<std::endl;
po::variables_map vm;
std::cout<<"Starting program"<<std::endl;
Run Code Online (Sandbox Code Playgroud)
程序在打印出"添加选项后"之前挂起.如果我通过gdb运行程序停止并执行后退跟踪,它只是显示它在"永远不会到达此处"注释之前就行了.回溯的顶部就是它
#0 ??
#1 __lll_lock_wait lowlevellock.c:47
#2 __pthread_mutex_lock pthread_mutex_lock.c:61
#3 in boost::shared_ptr<boost::program_options::option_description>* std::__uninitialized_move_a<boost::shared_ptr<boost::program_options::option_description>*, boost::shared_ptr<boost::program_options::option_description>*, std::allocator<boost::shared_ptr<boost::program_option::option_description> > >(boost::shared_ptr<boost::program_optionns::option_description>*, boost::shared_ptr<boost::program_options::option_description>*, std::allocator<boost::shared_ptr<boost::program_options::option_description> >&) () from /usr/local/lib/libboost_program_options-mt.so.1.45.0
#4 in std::vector<boost::shared_ptr<boost::program_options::option_description>, std::allocator<boost::shared_ptr<boost::program_options::option_description> > >::_M_insert_aux(__gnu_cxx::__normal_iterator<boost::shared_ptr<boost::program_options::option_description>, std::vector<boost::shared_ptr<boost::program_options::option_description>, std::allocator<boost::shared_ptr<boost::program_options::option_description> const&)() from /usr/local/lib/libboost_program_options-mt.so.1.45.0
#5 in boost::program_options::options_description::add(boost::shared_ptr<boost::program_options::option_description>) () from …Run Code Online (Sandbox Code Playgroud) 我正在使用boost program_options 1.50.0
我想为我的程序foobar提供以下内容
foobar --debug 2 --debug 3
从boost program_options代码中,有一个示例regex.cpp,它显示了创建新类型并为该类型创建验证器.
我试过了,但它确实有效,但现在我不能使用其他一些add_options()typed_value选项,比如default_value,composing等.
这是我到目前为止尝试的内容:
#include <boost/program_options.hpp>
using namespace boost;
using namespace boost::program_options;
#include <iostream>
using namespace std;
struct lastmultioccurrenceint {
public:
lastmultioccurrenceint(int n) : n(n) {}
int n;
};
void validate(boost::any& v,
const std::vector< std::string >& xs,
//const std::vector< std::basic_string<charT> >& xs,
lastmultioccurrenceint* , int)
{
using namespace boost::program_options;
cerr << "IN VALIDATE" << endl;
//validators::check_first_occurrence(v);
string s = validators::get_single_string(xs);
if (!v.empty()) {
cerr << "\tPRINTTING MULTIOCCURENCE WARNING, allowing v to …Run Code Online (Sandbox Code Playgroud) 是否可以使用boost :: program_options这样的参数?
program -p1 123 -p2 234 -p3 345 -p12 678
Run Code Online (Sandbox Code Playgroud)
即,是否可以-p动态地用第一个标记(例如)后跟数字来指定参数名称?
我想避免这个:
program -p 1 123 -p 2 234 -p 3 345 -p 12 678
Run Code Online (Sandbox Code Playgroud) 我正在使用boost :: program_options这样:
namespace po = boost::program_options;
po::options_description desc("Options");
desc.add_options()
("help,?", "Show Options")
("capture-file,I", po::value<string>(), "Capture File")
("capture-format,F", po::value<string>()->default_value("pcap"), "Capture File Format")
("output-file,O", po::value<string>()->default_value("CONOUT$"), "Output File");
po::variables_map vm;
po::store(po::command_line_parser(ac, av).options(desc)./*positional(pd).*/run(), vm);
Run Code Online (Sandbox Code Playgroud)
如果我通过命令行参数-I hithere它的工作原理,但我通过/I hithere升压抛出了boost::bad_any_cast一个what()"使用boost :: any_cast失败的转换"的.
是否可以使用program_options来解析/-delimitted或--delimitted选项?奖金的问题,可以将其进行配置,以便/和-设置相同的选项,但在彼此的二元对立?例如,/verbose可能意味着详细记录,而-verbose可能意味着更简洁的记录.
我想使用boost :: program_options创建一个可执行文件,可以调用如下:
./example --nmax=0,10 # nmax is chosen randomly between 0 and 10
./example --nmax=9 # nmax is set to 9
./example # nmax is set to the default value of 10
Run Code Online (Sandbox Code Playgroud)
以最小的代码以类型安全的方式实现这一目标的最佳方法是什么?
我正在使用boost :: program_options来指定我的C++应用程序的参数.有没有办法指定一组备选方案中需要一个参数?
<application> [--one int-value1 | --two string-value2 | --three]
Run Code Online (Sandbox Code Playgroud)
另外,在上述中,用户必须通过替代的正好一个:--one,--two,或--three.
我可以手动执行此操作,但希望有一个内置机制而不是这个:
#include <boost/program_options.hpp>
namespace po = boost::program_options;
int main(int argc, char *argv[]) {
po::options_description options;
int band;
std::string titles_path;
options.add_options()
("one", po::value<int>(&band)->default_value(1))
("two", po::value<std::string>(&titles_path))
("three");
po::variables_map vm;
po::store(po::parse_command_line(argc, argv, options), vm);
if (1 != (vm.count("one") + vm.count("two") + vm.count("three"))) {
std::cerr << options << std::endl;
return -11;
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
使用提升选项有更好的方法吗?
我有一个位置选项(文件名),我希望它是最后一个选项.基本上,用户可以在命令行上传递一堆东西,并且还使用-F作为文件名.但是,我希望用户也能够将文件名放在最后.
例如
./program --var 3 /path/to/file
Run Code Online (Sandbox Code Playgroud)
我当前实现的代码允许调用者将文件名放在命令行的任何位置.无论如何强迫位置参数总是在"常规"之后?
以下是我设置位置参数的方法:
pos_opts_desc.add("filename", -1);
Run Code Online (Sandbox Code Playgroud)
并解析命令行:
store(
command_line_parser(argc, argv).options(opts_desc).postional(pos_opts_desc).run(),
opts_var_map);
Run Code Online (Sandbox Code Playgroud)
在此先感谢您的帮助.
编辑添加:
我完全可以在命令行的任何地方指定-F.但是,如果设置是通过位置选项完成的,我想确保位置选项位于最后.