Wenn Sie den Befehlszeilenparser verwenden, können Sie verschiedene Stile festlegen. Die Lösung wäre also, nur lange Optionen zu verwenden und allow_long_disguise style zu aktivieren, wodurch lange Optionen mit einem Strich (d. H. "-long_option") angegeben werden können. Hier ein Beispiel:
#include <iostream>
#include <boost/program_options.hpp>
namespace options = boost::program_options;
using namespace std;
int
main (int argc, char *argv[])
{
options::options_description desc (string (argv[0]).append(" options"));
desc.add_options()
("h", "Display this message")
;
options::variables_map args;
options::store (options::command_line_parser (argc, argv).options (desc)
.style (options::command_line_style::default_style |
options::command_line_style::allow_long_disguise)
.run(), args);
options::notify (args);
if (args.count ("h"))
{
cout << desc << endl;
return 0;
}
}
Es wird allerdings ein kleines Problem mit der Beschreibung ausgegeben:
$ ./test --h
./test options:
--h Display this message
Und das ist schwer zu beheben, weil das ist, was verwendet wird, um diese Ausgabe zu bilden:
Die einzige Lösung, die in den Sinn kommt, ist das Ersetzen von "-" durch "-" in der resultierenden Zeichenkette. Zum Beispiel:
#include <iostream>
#include <sstream>
#include <boost/program_options.hpp>
#include <boost/algorithm/string/replace.hpp>
namespace options = boost::program_options;
using namespace std;
int
main (int argc, char *argv[])
{
options::options_description desc (string (argv[0]).append(" options"));
desc.add_options()
("h", "Display this message")
;
options::variables_map args;
options::store (options::command_line_parser (argc, argv).options (desc)
.style (options::command_line_style::default_style |
options::command_line_style::allow_long_disguise)
.run(), args);
options::notify (args);
if (args.count ("h"))
{
std::stringstream stream;
stream << desc;
string helpMsg = stream.str();
boost::algorithm::replace_all (helpMsg, "--", "-");
cout << helpMsg << endl;
return 0;
}
}
Das Beste, was Sie tun können, ist, den Code zu beheben, wo es leer lange Option Beschreibung druckt und einen Patch an den Autor der Bibliothek senden.
Sie möchten, dass der Hilfetext nur kurze Optionen deaktiviert? –
Nein Ich möchte nur Optionen wie -v anstelle von --verbose oder beides definieren. Das Angeben von ", v" anstelle von "v" macht den Trick, aber egifiziert die Druckauflistung der Beschreibung. – Novikov