我使用的代码包含一组模块,编译到各个库.反过来,库以不同的组合链接以构建不同的二进制文件.
所以,这是非常有序的.
不同的模块使用不同的命令行参数,我想使用Boost.Program_options进行解析.
由于命令行参数集取决于哪些库链接在一起,我事先并不知道所有参数,因此无法将它们添加到program_options :: options_description.
如何让每个模块添加命令行参数并稍后读取它们?
谢谢
Boost :: Program_Options的默认语法是"--DEVICE iphone".我如何支持语法"-DEVICE:iphone"或"-DEVICE = iphone"?
我boost::program_options用来为我的应用程序提供命令行解析接口.我想配置它来解析选项,
using namespace boost::program_options;
options_description desc("Allowed options");
desc.add_options()
("help,h", "produce help message")
("version,v", "print the version number")
("include-path,I", value< vector<string> >(), "include path")
("input-file,i", value<string>(), "input file");
positional_options_description p;
p.add("input-file", 1);
variables_map vm;
parsed_options parsed = command_line_parser(ac, av).
options(desc).positional(p).run();
store(parsed, vm);
notify(vm);
Run Code Online (Sandbox Code Playgroud)
我想配置它,以便最后一次切换后的每个标记以向量的形式返回.我已尝试使用collect_unrecognizedBoost文档中给出的示例,但我遇到了一些问题,因为我也使用输入文件的位置参数.
基本上我想这样做.如果我有:
./program -i "inputfile.abc" argument1 argument2 argument3
Run Code Online (Sandbox Code Playgroud)
我想它来存储inputfile.abc的input-file值,并得出vector<string>的argument1,argument2并argument3为未经请求的参数.
然而,我也希望能够有一个位置论证,所以
./program "inputfile.abc" argument1 argument2 argument3
Run Code Online (Sandbox Code Playgroud)
会给我相同的结果.
我很抱歉,如果已经提出这个问题,谢谢你的帮助.
我有一个程序,使用boost :: program_options从配置文件中读取大量变量.配置文件正在工作并读取值,但由于文件中有很多选项,我想将它们记录下来.例如,我希望配置文件看起来像:
# Here is a description of flag1
# flag1 = true means blah blah blah
# flag1 = false means ...
flag1=true
# Here is a description of flag 2
.
.
.
Run Code Online (Sandbox Code Playgroud)
问题是我找不到任何描述方法的文档.我相当肯定我可以使用诸如a=我的注释分隔符之类的东西,并简单地将所有注释分配给a std::vector<string>在解析后丢弃,但是我想知道是否有更合适的方法来处理注释行配置文件.
在你说OVERKILL之前,我不在乎.
如何使Boost.program_options处理所需的cat选项-?
我有
// visible
po::options_description options("Options");
options.add_options()("-u", po::value<bool>(), "Write bytes from the input file to the standard output without delay as each is read.");
po::positional_options_description file_options;
file_options.add("file", -1);
po::variables_map vm;
po::store(po::command_line_parser(argc, argv).options(options).positional(file_options).run(), vm);
po::notify(vm);
bool immediate = false;
if(vm.count("-u"))
immediate = true;
if(vm.count("file"))
support::print(vm["file"].as<vector<string>>());
Run Code Online (Sandbox Code Playgroud)
我运行时抛出异常cat - - -:
无法识别的选项' - '
我希望它-看作一个位置参数,我需要在完整文件列表中以正确的顺序.我怎么能实现这个目标?
UPDATE
我有一半修复.我需要
po::options_description options("Options");
options.add_options()("-u", po::value<bool>(), "Write bytes from the input file to the standard output without delay as …Run Code Online (Sandbox Code Playgroud) 我正在使用Boost :: Program_options来解析我的命令行,并修改了教程中的一些代码,如下所示:
try {
po::options_description desc("Allowed options");
desc.add_options()
("help,h", "output help message")
("width,w", po::value<int>()->required(), " width")
;
po::positional_options_description p;
p.add("width", 1);
po::variables_map vm;
po::store(po::command_line_parser(argc, argv).
options(desc).positional(p).run(), vm);
if (vm.count("help")) {
std::cout << "USAGE: " << av[0] << &p << std::endl;
return 0;
}
po::notify(vm);
if (vm.count("width")) {
std::cout << "width: " << vm["width"].as<int>() << "\n";
}
} catch (std::exception& e) {
std::cout << e.what() << std::endl;
return 1;
} catch (...) {
std::cout << "Exception of unknown type!" << …Run Code Online (Sandbox Code Playgroud) boost选项解析器允许指定一个变量来存储选项值,而不是使用以下so_long["typing"].as<bool>()方式:
bool flag_value;
entries.add_options()
("flag", value<bool>(&flag_value), "a simple flag value");
......
cout<<"flag value is: "<<flag_value<<endl;
Run Code Online (Sandbox Code Playgroud)
但是,上述选项声明不会创建简单的标志选项.它实际上要求你输入一些东西作为值(--flag true | false | on | off | yes | no | 1 | 0),这不是我想要的.
那么,有没有办法将结果存储在一个布尔值中,并仍然将选项保持为一个简单的标志?
po::options_description desc("This are the options that are available");
desc.add_options()("help", "print help")(
"deer", po::value<uint32_t>(), "set how many deer you want")(
"rating", po::value<uint32_t>(), "how good ?")(
"name", po::value<std::string>(), "and your name is ... ?");
po::variables_map vm;
po::store(po::parse_command_line(argc, argv, desc), vm);
po::notify(vm);
Run Code Online (Sandbox Code Playgroud)
在我试图迭代的代码的以下部分 vm
for (const auto& it : vm) {
std::cout << it.first.c_str() << " "
<< it.second.as<it.pair::second_type>() << "\n";
}
Run Code Online (Sandbox Code Playgroud)
这里的要点是vm包含keys相同类型,但是具有不同类型的值,在这个例子中我uint32_t混合了一个std::string.
我如何迭代这种容器?我想避免冗长的方法,所以我试图迭代这个数据结构.
编辑:
我忘了把它写下来,但很明显
namespace po = boost::program_options;
Run Code Online (Sandbox Code Playgroud) 我在Ubuntu 14.04,使用CMake和CLion.我正在尝试使用程序选项,以下代码取自其文档中的示例:
#include <iostream>
#include <boost/program_options.hpp>
int main(int ac, char* av[]) {
namespace po = boost::program_options;
using namespace std;
po::options_description desc("Allowed options");
desc.add_options()
("help", "produce help message")
("compression", po::value<int>(), "set compression level")
;
po::variables_map vm;
po::store(po::parse_command_line(ac, av, desc), vm);
po::notify(vm);
if (vm.count("help")) {
cout << desc << "\n";
return 1;
}
if (vm.count("compression")) {
cout << "Compression level was set to "
<< vm["compression"].as<int>() << ".\n";
} else {
cout << "Compression level was not set.\n";
}
}
Run Code Online (Sandbox Code Playgroud)
当我运行它时,我从终端获得以下输出:
$ …Run Code Online (Sandbox Code Playgroud) 当我a.out -i file0 file1在命令行输入时,我希望选项-i同时接收file0和file1但是,-i只接收file0但不接收file1
但是,我发现我必须输入a.out -i file0 -i file1以-i选择接收file0和file1
可以boost::program_options这样吗?
代码改编自http://www.boost.org/doc/libs/1_62_0/libs/program_options/example/options_description.cpp
#include <boost/program_options.hpp>
using namespace boost;
namespace po = boost::program_options;
#include <iostream>
#include <algorithm>
#include <iterator>
using namespace std;
// A helper function to simplify the main part.
template<class T>
ostream& operator<<(ostream& os, const vector<T>& v)
{
copy(v.begin(), v.end(), ostream_iterator<T>(os, " "));
return os;
}
int main(int …Run Code Online (Sandbox Code Playgroud)