我想在我正在编写的程序中使用 boost 程序选项。该程序还使用 CORBA,它旨在接受特定于 CORBA 的命令行输入。我想让 boost 程序选项忽略这些与 CORBA 相关的命令行输入,只处理其他输入。我怎样才能做到这一点?
作为示例,给出以下命令行:
./myprogram -ORBInitRef NameService=corbaloc:iiop:localhost:1234/NameService --myBoostOptionFoo 1 --myBoostOptionBar trololo
如何使 boost 程序选项仅处理 myBoostOptionFoo 和 myBoostOptionBar?
所有 CORBA 命令行输入都以“-ORBxxxxxx”开头,因此这应该有助于过滤,但我完全不知道是否有一个简单的方法来完成此操作,因为 boost::PO 会提示它不理解的命令行选项。
最佳答案
直接来自 Allowing Unknown Options 上的文档:
Usually, the library throws an exception on unknown option names. This behavior can be changed. For example, only some part of your application uses Program_options, and you wish to pass unrecognized options to another part of the program, or even to another application.
To allow unregistered options on the command line, you need to use the
basic_command_line_parser
class for parsing (notparse_command_line
) and call theallow_unregistered method
of that class:
parsed_options parsed = command_line_parser(argc, argv).options(desc).allow_unregistered().run();
For each token that looks like an option, but does not have a known name, an instance of basic_option will be added to the result. The string_key and value fields of the instance will contain results of syntactic parsing of the token, the unregistered field will be set to true, and the original_tokens field will contain the token as it appeared on the command line.
If you want to pass the unrecognized options further, the
collect_unrecognized
function can be used. The function will collect original tokens for all unrecognized values, and optionally, all found positional options. Say, if your code handles a few options, but does not handles positional options at all, you can use the function like this:
vector<string> to_pass_further = collect_unrecognized(parsed.options,
include_positional);
关于c++ - 如何使 boost 程序选项忽略要由另一个解释器处理的某些输入?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26489667/