diff options
Diffstat (limited to 'src/support/command-line.cpp')
-rw-r--r-- | src/support/command-line.cpp | 54 |
1 files changed, 42 insertions, 12 deletions
diff --git a/src/support/command-line.cpp b/src/support/command-line.cpp index c84474259..0147f9044 100644 --- a/src/support/command-line.cpp +++ b/src/support/command-line.cpp @@ -53,26 +53,38 @@ void printWrap(std::ostream& os, int leftPad, const std::string& content) { Options::Options(const std::string& command, const std::string& description) : debug(false), positional(Arguments::Zero) { + std::string GeneralOption = "General options"; + add("--version", "", "Output version information and exit", + GeneralOption, Arguments::Zero, [command](Options*, const std::string&) { - std::cout << command << " version " << PROJECT_VERSION << "\n"; + std::cout << command << " version " << PROJECT_VERSION << '\n'; exit(0); }); add("--help", "-h", "Show this help message and exit", + GeneralOption, Arguments::Zero, [this, command, description](Options* o, const std::string&) { + for (size_t i = 0; i < SCREEN_WIDTH; i++) { + std::cout << '='; + } + std::cout << '\n'; std::cout << command; if (positional != Arguments::Zero) { std::cout << ' ' << positionalName; } std::cout << "\n\n"; printWrap(std::cout, 0, description); - std::cout << "\n\nOptions:\n"; + std::cout << '\n'; + for (size_t i = 0; i < SCREEN_WIDTH; i++) { + std::cout << '='; + } + std::cout << '\n'; size_t optionWidth = 0; for (const auto& o : options) { if (o.hidden) { @@ -81,17 +93,27 @@ Options::Options(const std::string& command, const std::string& description) optionWidth = std::max(optionWidth, o.longName.size() + o.shortName.size()); } - for (const auto& o : options) { - if (o.hidden) { - continue; + for (int i = int(categories.size()) - 1; i >= 0; i--) { + auto& category = categories[i]; + std::cout << "\n\n" << category << ":\n"; + for (size_t i = 0; i < category.size() + 1; i++) { + std::cout << '-'; } std::cout << '\n'; - bool long_n_short = o.longName.size() != 0 && o.shortName.size() != 0; - size_t pad = 1 + optionWidth - o.longName.size() - o.shortName.size(); - std::cout << " " << o.longName << (long_n_short ? ',' : ' ') - << o.shortName << std::string(pad, ' '); - printWrap(std::cout, optionWidth + 4, o.description); - std::cout << '\n'; + for (const auto& o : options) { + if (o.hidden || o.category != category) { + continue; + } + std::cout << '\n'; + bool long_n_short = + o.longName.size() != 0 && o.shortName.size() != 0; + size_t pad = + 1 + optionWidth - o.longName.size() - o.shortName.size(); + std::cout << " " << o.longName << (long_n_short ? ',' : ' ') + << o.shortName << std::string(pad, ' '); + printWrap(std::cout, optionWidth + 4, o.description); + std::cout << '\n'; + } } std::cout << '\n'; exit(EXIT_SUCCESS); @@ -99,6 +121,7 @@ Options::Options(const std::string& command, const std::string& description) add("--debug", "-d", "Print debug information to stderr", + GeneralOption, Arguments::Optional, [&](Options* o, const std::string& arguments) { debug = true; @@ -111,11 +134,18 @@ Options::~Options() {} Options& Options::add(const std::string& longName, const std::string& shortName, const std::string& description, + const std::string& category, Arguments arguments, const Action& action, bool hidden) { options.push_back( - {longName, shortName, description, arguments, action, hidden, 0}); + {longName, shortName, description, category, arguments, action, hidden, 0}); + + if (std::find(categories.begin(), categories.end(), category) == + categories.end()) { + categories.push_back(category); + } + return *this; } |