2013-04-07 2 views
5

Ich verwende Boost.program_options, um Befehlszeilen für meine Implementierung von POSIX-Dienstprogrammen zu analysieren. Nehmen Sie als einfaches Beispiel cmp.So zeigen Sie die Befehlszeilenoperandenbeschreibung in --help output an

Jetzt möchte ich ein zusätzliches Argument --help haben, das eine Beschreibung aller Argumente zeigt, was in diesem Fall wichtig ist. Ich habe:

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

, die die file1 und file2 Optionen Beschreibung zu zeigen, nicht in der Lage ist. Ich kann sie natürlich zu options hinzufügen, aber das würde mindestens zwei unerwünschte Argumente hinzufügen [-]-file{1,2}, die ich wirklich nicht will. Ich will nur diese Ausgabe für --help (ohne es offensichtlich zu hartzucodieren):

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. 

Gibt es eine Möglichkeit, dies in der Bibliothek ohne Hacking zu erreichen? Ich würde denken, das ist ziemlich einfach, aber ich kann es in keiner der tutorials finden.

AKTUALISIEREN Für alle, ich habe eine feature request dafür eingereicht, in einer hoffentlich rückwärtskompatiblen Weise.

Antwort

1

Es ist nicht ideal, aber wie wäre es mit einem "Dummy" Satz von Programmoptionen, formatieren Sie den boost :: program_options Formatierer formatieren seinen Hilfetext für Sie, und entfernen Sie dann die Bindestriche mit einer schnellen Ersetzung?

Dann geben Sie diesen Hilfstext zusätzlich zum Hilfstext options aus.

Etwas wie folgt aus:

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; 

Die Ausgabe sieht wie folgt aus:

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. 

Es ist nicht ideal, weil unter anderem die der Abstand zwischen den Spalten nicht die Hilfe Ausgabe entsprechen von Ihrem andere options Ausgang. Aber für etwas schnell und schmutzig, das funktioniert im Grunde, könnte es in Ordnung sein.

Es ist sowieso ein Gedanke.

(Ich sehe nichts in der Boost.Program_Options API, mit der Sie dies auch "richtig" machen können. https://stackoverflow.com/a/3621947/368896 gibt einen Hinweis, dass so etwas nicht unterstützt wird, aber das ist jetzt 3 Jahre alt .)

+0

Schlechte Nachrichten, leider ': (' – rubenvb