c – 解析位置参数

c – 解析位置参数,第1张

概述考虑从boost程序选项 examples中采用的以下琐碎程序 #include <boost/program_options.hpp>#include <boost/version.hpp>#include <iostream>intmain( int argc, char** argv ){ namespace po = boost::program_options; 考虑从boost程序选项 examples中采用的以下琐碎程序
#include <boost/program_options.hpp>#include <boost/version.hpp>#include <iostream>intmain( int argc,char** argv ){    namespace po = boost::program_options;    po::options_description desc("Options");    unsigned foo;    desc.add_options()        ("help,h","produce help message")        ("foo",po::value(&foo),"set foo")         ;    po::variables_map vm;    try {        po::store(                po::parse_command_line( argc,argv,desc ),vm                );        po::notify( vm );        if ( vm.count("help") ) {            std::cout << desc << "\n";            std::cout << "boost version: " << BOOST_liB_VERSION << std::endl;        }    } catch ( const boost::program_options::error& e ) {        std::cerr << e.what() << std::endl;    }}

以下行为如预期:

samm$./a.out -hOptions:  -h [ --help ]         produce help message  --foo arg             set fooboost version: 1_44samm$./a.out --helpOptions:  -h [ --help ]         produce help message  --foo arg             set fooboost version: 1_44samm$./a.out --foo 1samm$./a.out --asdfunkNown option asdfsamm$

但是,当引入一个位置论证时,我感到非常惊讶,没有被标记为错误

samm$./a.out foo bar bazsamm$

为什么boost :: program_options :: too_many_positional_options_error异常不会抛出?

解决方法 当我明确指出不支持位置选项:
const po::positional_options_description p; // note empty positional options    po::store(            po::command_line_parser( argc,argv).                      options( desc ).                      positional( p ).                      run(),vm                      );

我得到预期的行为:

samm$./a.out asdf foo bartoo many positional optionssamm$
总结

以上是内存溢出为你收集整理的c – 解析位置参数全部内容,希望文章能够帮你解决c – 解析位置参数所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://www.54852.com/langs/1247204.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-06-07
下一篇2022-06-07

发表评论

登录后才能评论

评论列表(0条)

    保存