boost::program_options 可以使用“-”以外的分隔符吗?

发布于 2024-09-03 06:34:35 字数 946 浏览 2 评论 0原文

我使用 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);

如果我传递命令行参数 -I hihere 它可以工作,但如果我传递 /I hihere boost 会抛出一个 boost::bad_any_castwhat() 为“使用 boost::any_cast 转换失败”。

是否可以使用program_options来解析/分隔或-分隔选项?额外的问题,是否可以配置为使 /- 设置相同的选项,但彼此是二进制相反的?例如,/verbose 可能意味着详细日志记录,而 -verbose 可能意味着不太详细的日志记录。

I am using boost::program_options like this:

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);

If I pass the command line parameter -I hithere it works, but it I pass /I hithere boost throws a boost::bad_any_cast with a what() of " failed conversion using boost::any_cast".

Is it possible to use program_options to parse either /-delimitted or --delimitted options? Bonus question, can it be configured so that / and - set the same option, but are binary opposites of each other? For example, /verbose might mean verbose logging while -verbose might mean less verbose logging.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我纯我任性 2024-09-10 06:34:35

要使用 /-,请使用 command_line_parserstyle() 方法以及 style_t 标志。例如:(

po::store(po::command_line_parser(ac, av)
    .options(desc)
    .style(po::command_line_style::default_style
        | po::command_line_style::case_insensitive
        | po::command_line_style::allow_slash_for_short
        | po::command_line_style::allow_long_disguise)
    /*.positional(pd)*/
    .run(), vm);

allow_long_disguise/ 开始一个长选项。)

您可以通过以下方式使 /- 相反添加您自己的其他解析器;然而,这将是非常不标准的,因此可能会让最终用户感到困惑,所以我不确定这是一个好主意。

To use / and -, use command_line_parser's style() method with the appropriate combination of style_t flags. For example:

po::store(po::command_line_parser(ac, av)
    .options(desc)
    .style(po::command_line_style::default_style
        | po::command_line_style::case_insensitive
        | po::command_line_style::allow_slash_for_short
        | po::command_line_style::allow_long_disguise)
    /*.positional(pd)*/
    .run(), vm);

(allow_long_disguise lets / start a long option.)

You could probably make / and - opposites by adding your own additional parser; however, this would be very nonstandard and therefore potentially confusing to end users, so I'm not sure it's a good idea.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文