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.
Schlechte Nachrichten, leider ': (' – rubenvb