Provide help for specific arguments (--help=option).
continuous-integration/drone/push Build is passing Details

This commit is contained in:
tastytea 2019-10-10 14:36:59 +02:00
parent 43d9e3947a
commit 2b9768a5a9
Signed by: tastytea
GPG Key ID: CFC39497F1B26E07
3 changed files with 36 additions and 31 deletions

View File

@ -45,13 +45,9 @@ int App::main(const std::vector<std::string> &args)
{ {
std::locale::global(std::locale("")); // Set locale globally. std::locale::global(std::locale("")); // Set locale globally.
if (_version_requested) if (_exit_requested)
{ {
print_version(); return 0;
}
else if (_help_requested)
{
print_help();
} }
else else
{ {

View File

@ -20,6 +20,7 @@
#include <Poco/Util/HelpFormatter.h> #include <Poco/Util/HelpFormatter.h>
#include <Poco/Util/Option.h> #include <Poco/Util/Option.h>
#include <iostream> #include <iostream>
#include <memory>
using namespace remwharead_cli; using namespace remwharead_cli;
using std::cout; using std::cout;
@ -30,8 +31,7 @@ using Poco::Util::OptionCallback;
using Poco::Util::HelpFormatter; using Poco::Util::HelpFormatter;
App::App() App::App()
: _help_requested(false) : _exit_requested(false)
, _version_requested(false)
, _argument_error(false) , _argument_error(false)
, _format(export_format::undefined) , _format(export_format::undefined)
, _timespan({ time_point(), system_clock::now() }) , _timespan({ time_point(), system_clock::now() })
@ -43,10 +43,11 @@ void App::defineOptions(OptionSet& options)
{ {
options.addOption( options.addOption(
Option("help", "h", "Show this help message.") Option("help", "h", "Show this help message.")
.callback(OptionCallback<App>(this, &App::handle_info))); .argument("option", false)
.callback(OptionCallback<App>(this, &App::handle_options)));
options.addOption( options.addOption(
Option("version", "V", "Print version, copyright and license.") Option("version", "V", "Print version, copyright and license.")
.callback(OptionCallback<App>(this, &App::handle_info))); .callback(OptionCallback<App>(this, &App::handle_options)));
options.addOption( options.addOption(
Option("tags", "t", "Add tags to URI, delimited by commas.") Option("tags", "t", "Add tags to URI, delimited by commas.")
.argument("tags") .argument("tags")
@ -82,23 +83,21 @@ void App::defineOptions(OptionSet& options)
.callback(OptionCallback<App>(this, &App::handle_options))); .callback(OptionCallback<App>(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") if (name == "help")
{ {
_help_requested = true; _exit_requested = true;
print_help(value);
stopOptionsProcessing();
} }
else if (name == "version") else if (name == "version")
{ {
_version_requested = true; _exit_requested = true;
print_version();
stopOptionsProcessing();
} }
else if (name == "tags")
stopOptionsProcessing();
}
void App::handle_options(const std::string &name, const std::string &value)
{
if (name == "tags")
{ {
size_t pos_end = 0; size_t pos_end = 0;
size_t pos_start = 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()); std::unique_ptr<HelpFormatter> helpFormatter;
helpFormatter.setCommand(commandName()); OptionSet oneoption;
helpFormatter.setUsage("[-t tags] [-N] URI\n"
"-e format [-f file] [-T start,end] " if (option.empty())
"[[-s|-S] expression] [-r]"); {
helpFormatter.format(cout); helpFormatter = std::make_unique<HelpFormatter>(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<HelpFormatter>(oneoption);
}
helpFormatter->format(cout);
} }
void App::print_version() void App::print_version()

View File

@ -42,15 +42,13 @@ public:
protected: protected:
void defineOptions(OptionSet& options) override; void defineOptions(OptionSet& options) override;
void handle_info(const string &name, const string &);
void handle_options(const string &name, const string &value); void handle_options(const string &name, const string &value);
void print_help(); void print_help(const string &option);
void print_version(); void print_version();
int main(const std::vector<string> &args) override; int main(const std::vector<string> &args) override;
private: private:
bool _help_requested; bool _exit_requested;
bool _version_requested;
bool _argument_error; bool _argument_error;
string _uri; string _uri;
vector<string> _tags; vector<string> _tags;