c++ - 如何使 boost 程序选项忽略要由另一个解释器处理的某些输入?

标签 c++ boost corba boost-program-options

我想在我正在编写的程序中使用 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 (not parse_command_line) and call the allow_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/

相关文章:

corba - CORBA 是一种语言吗?

c++ - 有没有什么方法可以提取 Boost Regex,以便我可以将它打包到我的项目中?

java - 什么是 IOR 文件,它有什么作用,它是如何工作的?

c++ - 如何将标准头链接到源代码?

c++ - 为什么在头文件中声明并在文件中定义会出现多重定义错误?

c++ - 根据 boost::filesystem,为什么这个文件不是常规文件?

C++:使用 boost::regex 将管道分隔的字符串与通配符匹配

java - 用户定义的 CORBA 异常在编译后给我错误

c++ - 写入后linux c刷新串行缓冲区

带有运算符的 std::array 的 C++11 类型别名