Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/140.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 处理'-';使用boost.program\u选项_C++_Boost_Cat_Boost Program Options - Fatal编程技术网

C++ 处理'-';使用boost.program\u选项

C++ 处理'-';使用boost.program\u选项,c++,boost,cat,boost-program-options,C++,Boost,Cat,Boost Program Options,在你说过分之前,我不在乎 如何使Boost.program_选项处理所需的cat选项- 我有 // visible po::options_description options("Options"); options.add_options()("-u", po::value<bool>(), "Write bytes from the input file to the standard output without delay as each is read."); po::

在你说过分之前,我不在乎

如何使Boost.program_选项处理所需的
cat
选项
-

我有

// visible
po::options_description options("Options");
options.add_options()("-u", po::value<bool>(), "Write bytes from the input file to the standard output without delay as each is read.");

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

po::variables_map vm;
po::store(po::command_line_parser(argc, argv).options(options).positional(file_options).run(), vm);
po::notify(vm);

bool immediate = false;
if(vm.count("-u"))
  immediate = true;
if(vm.count("file"))
  support::print(vm["file"].as<vector<string>>());
问题是,当我输出参数时,我似乎只得到三个
-
中的两个:

if(vm.count("file"))
  support::print(vm["file"].as<vector<string>>());
if(vm.count(“文件”))
支持::打印(vm[“file”].as());

其中support::print可以很好地处理向量和其他内容。

您需要定义定位的命名程序选项,在您的示例中,它是
文件

#include <boost/foreach.hpp>
#include <boost/program_options.hpp>

#include <iostream>
#include <string>
#include <vector>

namespace po = boost::program_options;

int
main( int argc, char* argv[] )
{
    std::vector<std::string> input;
    po::options_description options("Options");
    options.add_options()
        ("-u", po::value<bool>(), "Write bytes from the input file to the standard output without delay as each is read.")
        ("file", po::value(&input), "input")
        ;

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

    po::variables_map vm;
    po::store(po::command_line_parser(argc, argv).options(options).positional(file_options).run(), vm);
    po::notify(vm);

    bool immediate = false;
    if(vm.count("-u"))
        immediate = true;
    BOOST_FOREACH( const auto& i, input ) {
        std::cout << "file: " << i << std::endl;
    }
}

如果只看到3个位置参数中的2个,很可能是因为按照惯例,
argv[0]
是程序名,因此不考虑用于参数解析。这可以在
basic\u命令行\u解析器
模板的源代码中看到

 37     template<class charT>
 38     basic_command_line_parser<charT>::
 39     basic_command_line_parser(int argc, const charT* const argv[])
 40     : detail::cmdline(
 41         // Explicit template arguments are required by gcc 3.3.1 
 42         // (at least mingw version), and do no harm on other compilers.
 43         to_internal(detail::make_vector<charT, const charT* const*>(argv+1, argv+argc+!argc)))
 44     {}
37模板
38基本命令行解析器::
39基本命令行解析器(int-argc,const-charT*const-argv[])
40:详细信息::cmdline(
41//gcc 3.3.1要求显式模板参数
42//(至少是mingw版本),并且不会对其他编译器造成损害。
43至_内部(详细信息:生成_向量(argv+1,argv+argc+!argc)))
44     {}

Boost.PO有自己的选项语法(有点有限)。也许你可以使用Boost.PO来实现你想要的语法。谢谢,但我已经理解了这一部分。似乎我对argv的干预不是boost.program_选项所期望的,它忽略了第一个argv,尽管我删除了argv[0]。@rubenvb您可能应该将这个问题标记为或类似的问题,那么您的
argv
看起来像什么?
$ g++ -std=c++11 -O2 -pthread main.cpp -lboost_program_options && ./a.out - - -
file: -
file: -
file: -
 37     template<class charT>
 38     basic_command_line_parser<charT>::
 39     basic_command_line_parser(int argc, const charT* const argv[])
 40     : detail::cmdline(
 41         // Explicit template arguments are required by gcc 3.3.1 
 42         // (at least mingw version), and do no harm on other compilers.
 43         to_internal(detail::make_vector<charT, const charT* const*>(argv+1, argv+argc+!argc)))
 44     {}