问题描述
我已经实现了路由,以通过遍历 po :: variable_map 来打印出
boost :: program_options
对象中可用的所有选项以及它们的当前值
I have implemented a routing to print out all of the options that are available in my boost::program_options
objects along with their current values by iterating over the po::variable_map
.
这样,我可以直接打印选项的长名称及其值.但是,我不知道如何从长选项名中检索短选项名(即--help中的-h).有什么建议怎么做吗?
With this I can directly print the long name of the option along with its value. However, I do not know, how to retrieve the short option name from the long option name (i.e. -h from --help). Any suggestions how to do this?
推荐答案
我记得在最近的答案中描述了为什么 variable_map
太迟到"以至于无法了解选项描述:
I remember describing in a recent answer why variable_map
is "too late" to know about the option descriptions:
- Boost. Handy variables_map. Taking advantage of having specified the type in the options_desctription
简而言之,variable_map位于存储组件,因此与选项描述"(位于选项说明组件).
In short, the variable_map sits in the Storage Component of the library and as such is isolated from the Option Descriptions (which lives in the Options Description Component).
您可以使用中间结果,即解析器组件:
You could work with the intermediate results, the raw output from the Parsers Component:
因此,让我们尝试在此处算出一个示例:
So, let's try to work out a sample here:
po::parsed_options const intermediate = po::parse_command_line(ac, av, desc);
//////////////////////////////////
// print the extended usage info as per the question
//
for (auto& entry : intermediate.options)
{
po::option_description const& opt = desc.find(entry.string_key, false, false, false);
std::cout << "\nActual tokens involved: ";
for (auto& tok : entry.original_tokens)
std::cout << "'" << tok << "' ";
for (std::string const& v : entry.value)
std::cout << "\nAssociated value: " << v;
std::cout << "\n-----------------------------------------------------------\n";
std::cout << "opt.format_name() : " << opt.format_name() << "\n";
std::cout << "opt.long_name() : " << opt.long_name() << "\n";
std::cout << "opt.canonical_display_name('-'): " << opt.canonical_display_name(cls::allow_dash_for_short) << "\n";
std::cout << "opt.canonical_display_name('/'): " << opt.canonical_display_name(cls::allow_slash_for_short) << "\n";
std::cout << "opt.canonical_display_name('--'): " << opt.canonical_display_name(cls::allow_long) << "\n";
}
打印例如
Actual tokens involved: '-C' '42'
Associated value: 42
-----------------------------------------------------------
opt.format_name() : -C [ --compression ]
opt.long_name() : compression
opt.canonical_display_name('-'): -C
opt.canonical_display_name('/'): /C
opt.canonical_display_name('--'): --compression
Actual tokens involved: '--name' 'santa'
Associated value: santa
-----------------------------------------------------------
opt.format_name() : -n [ --name ]
opt.long_name() : name
opt.canonical_display_name('-'): -n
opt.canonical_display_name('/'): /n
opt.canonical_display_name('--'): --name
Compression level was set to 42.
#include <boost/program_options.hpp>
#include <boost/program_options/detail/parsers.hpp>
#include <iostream>
namespace po = boost::program_options;
namespace cls = po::command_line_style;
int main(int ac, char** av) {
// Declare the supported options.
po::options_description desc("Allowed options");
desc.add_options()
("help", "produce help message")
("name,n", po::value<std::string>(), "specify the name")
("compression,C", po::value<int>(), "set compression level")
;
po::variables_map vm;
po::parsed_options const intermediate = po::parse_command_line(ac, av, desc);
//////////////////////////////////
// print the extended usage info as per the question
//
for (auto& entry : intermediate.options)
{
po::option_description const& opt = desc.find(entry.string_key, false, false, false);
std::cout << "\nActual tokens involved: ";
for (auto& tok : entry.original_tokens)
std::cout << "'" << tok << "' ";
for (std::string const& v : entry.value)
std::cout << "\nAssociated value: " << v;
std::cout << "\n-----------------------------------------------------------\n";
std::cout << "opt.format_name() : " << opt.format_name() << "\n";
std::cout << "opt.long_name() : " << opt.long_name() << "\n";
std::cout << "opt.canonical_display_name('-'): " << opt.canonical_display_name(cls::allow_dash_for_short) << "\n";
std::cout << "opt.canonical_display_name('/'): " << opt.canonical_display_name(cls::allow_slash_for_short) << "\n";
std::cout << "opt.canonical_display_name('--'): " << opt.canonical_display_name(cls::allow_long) << "\n";
}
//////////////////////////////////
po::store(intermediate, vm);
po::notify(vm);
if (vm.count("help")) {
std::cout << desc << "\n";
return 1;
}
if (vm.count("compression")) {
std::cout << "Compression level was set to "
<< vm["compression"].as<int>() << ".\n";
} else {
std::cout << "Compression level was not set.\n";
}
}
这篇关于boost程序选项短/长参数名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!