diff --git a/src/cli/main.cpp b/src/cli/main.cpp index d9bac0f..e95600d 100644 --- a/src/cli/main.cpp +++ b/src/cli/main.cpp @@ -45,13 +45,9 @@ int App::main(const std::vector &args) { std::locale::global(std::locale("")); // Set locale globally. - if (_version_requested) + if (_exit_requested) { - print_version(); - } - else if (_help_requested) - { - print_help(); + return 0; } else { diff --git a/src/cli/parse_options.cpp b/src/cli/parse_options.cpp index 6c0b032..5157294 100644 --- a/src/cli/parse_options.cpp +++ b/src/cli/parse_options.cpp @@ -20,6 +20,7 @@ #include #include #include +#include using namespace remwharead_cli; using std::cout; @@ -30,8 +31,7 @@ using Poco::Util::OptionCallback; using Poco::Util::HelpFormatter; App::App() - : _help_requested(false) - , _version_requested(false) + : _exit_requested(false) , _argument_error(false) , _format(export_format::undefined) , _timespan({ time_point(), system_clock::now() }) @@ -43,10 +43,11 @@ void App::defineOptions(OptionSet& options) { options.addOption( Option("help", "h", "Show this help message.") - .callback(OptionCallback(this, &App::handle_info))); + .argument("option", false) + .callback(OptionCallback(this, &App::handle_options))); options.addOption( Option("version", "V", "Print version, copyright and license.") - .callback(OptionCallback(this, &App::handle_info))); + .callback(OptionCallback(this, &App::handle_options))); options.addOption( Option("tags", "t", "Add tags to URI, delimited by commas.") .argument("tags") @@ -82,23 +83,21 @@ void App::defineOptions(OptionSet& options) .callback(OptionCallback(this, &App::handle_options))); } -void App::handle_info(const std::string &name, const std::string &) +void App::handle_options(const std::string &name, const std::string &value) { if (name == "help") { - _help_requested = true; + _exit_requested = true; + print_help(value); + stopOptionsProcessing(); } else if (name == "version") { - _version_requested = true; + _exit_requested = true; + print_version(); + stopOptionsProcessing(); } - - stopOptionsProcessing(); -} - -void App::handle_options(const std::string &name, const std::string &value) -{ - if (name == "tags") + else if (name == "tags") { size_t pos_end = 0; size_t pos_start = 0; @@ -193,14 +192,26 @@ void App::handle_options(const std::string &name, const std::string &value) } } -void App::print_help() +void App::print_help(const string &option) { - HelpFormatter helpFormatter(options()); - helpFormatter.setCommand(commandName()); - helpFormatter.setUsage("[-t tags] [-N] URI\n" - "-e format [-f file] [-T start,end] " - "[[-s|-S] expression] [-r]"); - helpFormatter.format(cout); + std::unique_ptr helpFormatter; + OptionSet oneoption; + + if (option.empty()) + { + helpFormatter = std::make_unique(options()); + helpFormatter->setCommand(commandName()); + helpFormatter->setUsage("[-t tags] [-N] URI\n" + "-e format [-f file] [-T start,end] " + "[[-s|-S] expression] [-r]"); + } + else + { + oneoption.addOption(options().getOption(option)); + helpFormatter = std::make_unique(oneoption); + } + + helpFormatter->format(cout); } void App::print_version() diff --git a/src/cli/remwharead_cli.hpp b/src/cli/remwharead_cli.hpp index e0a014b..30acecd 100644 --- a/src/cli/remwharead_cli.hpp +++ b/src/cli/remwharead_cli.hpp @@ -42,15 +42,13 @@ public: protected: void defineOptions(OptionSet& options) override; - void handle_info(const string &name, const string &); void handle_options(const string &name, const string &value); - void print_help(); + void print_help(const string &option); void print_version(); int main(const std::vector &args) override; private: - bool _help_requested; - bool _version_requested; + bool _exit_requested; bool _argument_error; string _uri; vector _tags;