2013-04-01 4 views
6

OVERKILL을 말하기 전에, 나는 상관하지 않는다.'-'with boost.program_options

Boost.program_options가 필요한 cat 옵션 -을 처리하도록하려면 어떻게해야합니까? ,

가 나는 그것이 위치 인수로 -를보고 싶어 '-'

알 수없는 옵션 :

나는 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>>()); 

이 전체 파일 목록에서 올바른 순서로 필요합니다. 내가 어떻게 이걸 얻을 수 있니?

UPDATE 나는 반 수정이 있습니다. 나는

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< vector<string> >(), "input file"); po::positional_options_description file_options; file_options.add("file", -1); 

문제는 나는 단지 2를 얻을 것입니다

을 필요로하는 세 가지 - 때 출력 인수 :

지원 :: 인쇄가 잘 벡터 물건을 처리
if(vm.count("file")) 
    support::print(vm["file"].as<vector<string>>()); 

.

+0

Boost.PO는 자체적 인 (제한적인 종류의) 옵션 구문을 가지고 있습니다. Boost.PO를 원하는 구문에 사용할 수 있습니다. – Abyx

+0

@Oli : 완료. 감사. – rubenvb

답변

4

당신은 위치입니다 명명 된 프로그램 옵션을 정의해야합니다, 귀하의 경우는 여기에 file

#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; 
    } 
} 

입니다입니다 coliru demo하고

$ g++ -std=c++11 -O2 -pthread main.cpp -lboost_program_options && ./a.out - - - 
file: - 
file: - 
file: - 
를 클릭하지 않으려면 출력

위치 지정 인수 중 2 개만 표시되는 경우는 argv[0]이 프로그램 이름이므로 약식이므로 인자 파싱을 고려하지 않는다. 이는 basic_command_line_parser 템플릿의 소스 코드에서 볼 수 있습니다.

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  {} 
+0

고마워,하지만 이미 그 부분을 알아 냈어. argv를 사용하는 것이 내 boost.program_options가 예상 한 것이 아니며 argv [0]을 제거했지만 첫 번째 argv를 무시한 것 같습니다. – rubenvb

+0

@rubenvb 당신은 아마도 [tag : windows] 또는 그와 같은 어떤 질문을 당신의'argv'처럼 보이겠습니까? –