diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/cli/cli.cpp | 10 | ||||
-rw-r--r-- | src/cli/cli.h | 13 | ||||
-rw-r--r-- | src/cli/main.cpp | 33 | ||||
-rw-r--r-- | src/cli/utils.cpp | 32 |
4 files changed, 53 insertions, 35 deletions
diff --git a/src/cli/cli.cpp b/src/cli/cli.cpp index f23c3574d..e3ff2a75f 100644 --- a/src/cli/cli.cpp +++ b/src/cli/cli.cpp @@ -21,10 +21,14 @@ Command::Command(const std::string& cmd_spec) : m_spec(cmd_spec) Command::~Command() { /* for unique_ptr */ } +std::string Command::cmd_name() const + { + return m_spec.substr(0, m_spec.find(' ')); + } + std::string Command::help_text() const { - return "Usage: " + m_spec + - "\n\nAll commands support --verbose --help --output= --error-output= --rng-type= --drbg-seed="; + return "Usage: " + m_spec; } int Command::run(const std::vector<std::string>& params) @@ -68,7 +72,7 @@ int Command::run(const std::vector<std::string>& params) } this->go(); - return 0; + return m_return_code; } catch(CLI_Usage_Error& e) { diff --git a/src/cli/cli.h b/src/cli/cli.h index 75de33515..d096617a3 100644 --- a/src/cli/cli.h +++ b/src/cli/cli.h @@ -93,18 +93,18 @@ class Command return m_spec; } - std::string cmd_name() const - { - return m_spec.substr(0, m_spec.find(' ')); - } + std::string cmd_name() const; protected: /* - * The actual functionality of the cli command implemented in subclas + * The actual functionality of the cli command implemented in subclas. + * The return value from main will be zero. */ virtual void go() = 0; + void set_return_code(int rc) { m_return_code = rc; } + std::ostream& output(); std::ostream& error_output(); @@ -172,6 +172,9 @@ class Command std::unique_ptr<Botan::RandomNumberGenerator> m_rng; + // possibly set by calling set_return_code() + int m_return_code = 0; + public: // the registry interface: diff --git a/src/cli/main.cpp b/src/cli/main.cpp index 6b0024465..167052c9d 100644 --- a/src/cli/main.cpp +++ b/src/cli/main.cpp @@ -5,41 +5,20 @@ */ #include "cli.h" - #include <botan/version.h> -#include <sstream> #include <iostream> -namespace { - -std::string main_help() - { - std::ostringstream oss; - - oss << "Usage: botan <cmd> <cmd-options>\n"; - oss << "Available commands:\n"; - - for(const auto& cmd_name : Botan_CLI::Command::registered_cmds()) - { - std::unique_ptr<Botan_CLI::Command> cmd = Botan_CLI::Command::get_cmd(cmd_name); - oss << cmd->cmd_spec() << "\n"; - } - - return oss.str(); - } - -} - int main(int argc, char* argv[]) { std::cerr << Botan::runtime_version_check(BOTAN_VERSION_MAJOR, BOTAN_VERSION_MINOR, BOTAN_VERSION_PATCH); - const std::string cmd_name = (argc <= 1) ? "help" : argv[1]; + std::string cmd_name = "help"; - if(cmd_name == "help" || cmd_name == "--help" || cmd_name == "-h") + if(argc >= 2) { - std::cout << main_help(); - return 1; + cmd_name = argv[1]; + if(cmd_name == "--help" || cmd_name == "-h") + cmd_name = "help"; } std::unique_ptr<Botan_CLI::Command> cmd(Botan_CLI::Command::get_cmd(cmd_name)); @@ -50,6 +29,6 @@ int main(int argc, char* argv[]) return 1; } - std::vector<std::string> args(argv + 2, argv + argc); + std::vector<std::string> args(argv + std::min(argc, 2), argv + argc); return cmd->run(args); } diff --git a/src/cli/utils.cpp b/src/cli/utils.cpp index 1e2ddf42e..602035c8c 100644 --- a/src/cli/utils.cpp +++ b/src/cli/utils.cpp @@ -14,6 +14,7 @@ #include <botan/cpuid.h> #include <botan/hex.h> #include <botan/parsing.h> +#include <sstream> #if defined(BOTAN_HAS_BASE64_CODEC) #include <botan/base64.h> @@ -29,6 +30,37 @@ namespace Botan_CLI { +class Print_Help final : public Command + { + public: + Print_Help() : Command("help") {} + + std::string help_text() const override + { + std::ostringstream oss; + + oss << "Usage: botan <cmd> <cmd-options>\n\n"; + oss << "All commands support --verbose --help --output= --error-output= --rng-type= --drbg-seed=\n\n"; + oss << "Available commands:\n"; + + for(const auto& cmd_name : Command::registered_cmds()) + { + std::unique_ptr<Command> cmd = Command::get_cmd(cmd_name); + oss << " " << cmd->cmd_spec() << "\n"; + } + + return oss.str(); + } + + void go() override + { + this->set_return_code(1); + output() << help_text(); + } + }; + +BOTAN_REGISTER_COMMAND("help", Print_Help); + class Config_Info final : public Command { public: |