2013-04-07 18 views
5

Używam Boost.program_options do analizy poleceń dla mojej implementacji narzędzi POSIX. Jako prosty przykład weź cmp.Jak wyświetlić opis operandu wiersza polecenia w --help output

Teraz chciałbym mieć dodatkowy argument --help, który pokazuje opis wszystkich argumentów, co jest w tym przypadku ważne. Mam:

po::options_description options("Options"); 
options.add_options()("help", "Show this help output.") 
        (",l", "(Lowercase ell.) Write the byte number (decimal) and the differing bytes (octal) for each difference.") 
        (",s", "Write nothing for differing files; return exit status only.") 

po::positional_options_description operands; 
operands.add("file1", 1);//, "A pathname of the first file to be compared. If file1 is '-', the standard input shall be used.") 
operands.add("file2", 1);//, "A pathname of the second file to be compared. If file2 is '-', the standard input shall be used."); 

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

if(vm.count("help")) 
{ 
    std::cout << "cmp: compare two files\nUsage: cmp [ -l | -s ] file1 file2\n" << options; 
    return 0; 
} 

który jest w stanie wykazać, opis file1 i file2 Opcje. Mogę oczywiście dodać je do options, ale doda to co najmniej dwa niechciane argumenty: [-]-file{1,2}, których naprawdę nie chcę. Chcę tylko to wyjście dla --help (oczywiście bez hardcoding go):

cmp: compare two files 
Usage: cmp [ -l | -s ] file1 file2 
Options: 
    --help    Show this help output. 
    -l     (Lowercase ell.) Write the byte number (decimal) and the differing bytes (octal) for each difference. 
    -s     Write nothing for differing files; return exit status only. 
Operands: 
    file1     A pathname of the first file to be compared. If file1 is '-', the standard input shall be used. 
    file2     A pathname of the second file to be compared. If file2 is '-', the standard input shall be used. 

Czy istnieje jakiś sposób, aby osiągnąć ten cel bez hacking wokół biblioteki? Sądzę, że to dość podstawowe rzeczy, ale nie mogę ich znaleźć w żadnym z tutorials.

AKTUALIZACJA Dla dobra wszystkich osób przesłałem do tego feature request w sposób zgodny z wcześniejszymi wersjami.

Odpowiedz

1

To nie jest idealne, ale co z tworzeniem "obojętnego" zestawu opcji programu, pozwól programowi :: format_options formatować tekst pomocy dla ciebie, a następnie po prostu usuń myślniki z szybką wymianą?

Następnie dane wyjściowe, które pomagają w tekście oprócz tekstu pomocy options.

coś takiego:

po::options_description dummy_options("Operands"); 
dummy_options.add_options() 
    ("file1", po::value<std::string>(), "A pathname of the first file to be compared. If file1 is '-', the standard input shall be used.") 
    ("file2", po::value<std::string>(), "A pathname of the second file to be compared. If file2 is '-', the standard input shall be used.") 
    ; 

std::stringstream s; 
s << dummy_options; 
std::string dummy_help_text = s.str(); 
boost::replace_all(dummy_help_text, "--", ""); 
boost::replace_all(dummy_help_text, "arg", "  "); 

std::cout << dummy_help_text << std::endl; 

Wyjście wygląda następująco:

Operands: 
    file1     A pathname of the first file to be compared. If file1 
         is '-', the standard input shall be used. 
    file2     A pathname of the second file to be compared. If file2 
         is '-', the standard input shall be used. 

To nie jest idealny, ponieważ, między innymi, odstęp między kolumnami nie będzie pasował do wyjścia pomocy ze strony inne wyjście options. Ale jeśli chodzi o coś szybkiego i brudnego, to zasadniczo działa, może być OK.

To jest myśl, w każdym razie.

(Nie widzę niczego w interfejsie API Boost.Program_Options, który pozwala to zrobić w odpowiedni sposób, https://stackoverflow.com/a/3621947/368896 podaje wskazówkę, że tego typu rzeczy nie są obsługiwane, ale to już 3 lata .)

+0

Złe wieści, niestety ': (' – rubenvb