我正在尝试使用Boost 1.58.0解析命令行。我的代码非常简单,可以从教程中复制\粘贴。看起来像这样:

 try {
        po::options_description desc;
        desc.add_options()
                ("version,v", "Display version of application.");

        po::positional_options_description p;
        p.add("input-file", -1);

        try
        {
            po::store(po::command_line_parser(argc, argv).
                      options(desc).positional(p).run(), vm);

            if ( vm.count("version")  )
            {
                std::cout << "Program version: " << SHUF_T_VERSION << std::endl << "Boost library version: " << BOOST_LIB_VERSION << std::endl;
                return false;
            }

            po::notify(vm); // throws on error, so do after help in case
            // there are any problems
        }
        catch(po::error& e)
        {
            std::cerr << "ERROR: " << e.what() << std::endl << std::endl;
            std::cerr << desc << std::endl;
            return false;
        }

    }
    catch(std::exception& e)
    {
        std::cerr << "Unhandled Exception: "
                  << e.what() << ", application will now exit" << std::endl;
        return false;

    }

  return true;

整个代码是here
该代码似乎是正确的。 app -v已正确处理。但是,如果我包含任何位置争议,例如app myfilepo::store()就会抛出unrecognised option 'myfile'。有什么想法为什么会发生?

最佳答案

您需要添加“输入文件”作为选项:

po::options_description desc;
desc.add_options()
        ("version,v", "Display version of application.")
        ("input-file", po::value<std::vector<std::string> >(), "input file");

the tutorial:

10-07 13:35
查看更多