diff options
author | René Korthaus <[email protected]> | 2017-08-28 22:45:11 +0200 |
---|---|---|
committer | René Korthaus <[email protected]> | 2018-01-14 17:22:21 +0100 |
commit | fd9c081f7a1fa4c6a0fc8bf0eea2fd7d1a58cd27 (patch) | |
tree | faf1544989346a685669611ef87a706042662c88 /src/cli/utils.cpp | |
parent | bede26dcff12cbb528a014bed3c2da983f04f8e4 (diff) |
Print a command description for cli help
Diffstat (limited to 'src/cli/utils.cpp')
-rw-r--r-- | src/cli/utils.cpp | 202 |
1 files changed, 194 insertions, 8 deletions
diff --git a/src/cli/utils.cpp b/src/cli/utils.cpp index 91638bfea..47c716ed5 100644 --- a/src/cli/utils.cpp +++ b/src/cli/utils.cpp @@ -12,7 +12,11 @@ #include <botan/cpuid.h> #include <botan/hex.h> #include <botan/parsing.h> +#include <botan/internal/stl_util.h> #include <sstream> +#include <iostream> +#include <iterator> +#include <iomanip> #if defined(BOTAN_HAS_HASH) #include <botan/hash.h> @@ -43,19 +47,71 @@ class Print_Help final : public Command std::string help_text() const override { - std::ostringstream oss; + const std::set<std::string> avail_commands = + Botan::map_keys_as_set(Botan_CLI::Command::global_registry()); - 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"; + const std::map<std::string, std::string> groups_description +#if defined(BOTAN_HAS_AES) && defined(BOTAN_HAS_AEAD_MODES) + { { "encryption", "Encryption" }, +#endif +#if defined(BOTAN_HAS_COMPRESSION) + { "compression", "Compression" }, +#endif + { "hash", "Hash Functions" }, +#if defined(BOTAN_HAS_HMAC) + { "hmac", "HMAC" }, +#endif + { "numtheory", "Number Theory" }, +#if defined(BOTAN_HAS_BCRYPT) + { "passhash", "Password Hashing" }, +#endif +#if defined(BOTAN_HAS_PSK_DB) && defined(BOTAN_HAS_SQLITE3) + { "psk", "PSK Database" }, +#endif + { "pubkey", "Public Key Cryptography" }, +#if defined(BOTAN_HAS_TLS) + { "tls", "TLS" }, +#endif +#if defined(BOTAN_HAS_X509_CERTIFICATES) + { "x509", "X.509" }, +#endif + { "misc", "Miscellaneous" } + }; + + const std::set<std::string> groups = + Botan::map_keys_as_set(groups_description); + + std::ostringstream oss; - for(const auto& cmd_name : Command::registered_cmds()) + oss << "Usage: botan <cmd> <cmd-options>\n"; + oss << "All commands support --verbose --help --output= --error-output= --rng-type= --drbg-seed=\n\n"; + oss << "Available commands:\n\n"; + + for(auto& cmd_group : groups) + { + oss << groups_description.at(cmd_group) << ":\n"; + for(auto& cmd_name : avail_commands) { - std::unique_ptr<Command> cmd = Command::get_cmd(cmd_name); - oss << " " << cmd->cmd_spec() << "\n"; + auto cmd = Botan_CLI::Command::get_cmd(cmd_name); + if(cmd->group() == cmd_group) + { + oss << " " << std::setw(16) << std::left << cmd->cmd_name() << " " << cmd->description() << "\n"; + } } + oss << "\n"; + } + + return oss.str(); + } + + std::string group() const override + { + return ""; + } - return oss.str(); + std::string description() const override + { + return "Prints a help string"; } void go() override @@ -81,6 +137,16 @@ class Config_Info final : public Command " libs: Print libraries\n"; } + std::string group() const override + { + return "misc"; + } + + std::string description() const override + { + return "Print the used prefix, cflags, ldflags or libs"; + } + void go() override { const std::string arg = get_arg("info_type"); @@ -117,6 +183,16 @@ class Version_Info final : public Command public: Version_Info() : Command("version --full") {} + std::string group() const override + { + return "misc"; + } + + std::string description() const override + { + return "Print version info"; + } + void go() override { if(flag_set("full")) @@ -137,6 +213,16 @@ class Print_Cpuid final : public Command public: Print_Cpuid() : Command("cpuid") {} + std::string group() const override + { + return "misc"; + } + + std::string description() const override + { + return "List available processor flags (aes_ni, SIMD extensions, ...)"; + } + void go() override { output() << "CPUID flags: " << Botan::CPUID::to_string() << "\n"; @@ -152,6 +238,16 @@ class Hash final : public Command public: Hash() : Command("hash --algo=SHA-256 --buf-size=4096 *files") {} + std::string group() const override + { + return "hash"; + } + + std::string description() const override + { + return "Compute the message digest of given file(s)"; + } + void go() override { const std::string hash_algo = get_arg("algo"); @@ -195,6 +291,16 @@ class RNG final : public Command public: RNG() : Command("rng --system --rdrand --auto --entropy --drbg --drbg-seed= *bytes") {} + std::string group() const override + { + return "misc"; + } + + std::string description() const override + { + return "Sample random bytes from the specified rng"; + } + void go() override { std::string type = get_arg("rng-type"); @@ -230,6 +336,16 @@ class HTTP_Get final : public Command public: HTTP_Get() : Command("http_get --redirects=1 --timeout=3000 url") {} + std::string group() const override + { + return "misc"; + } + + std::string description() const override + { + return "Retrieve resource from the passed http/https url"; + } + void go() override { const std::string url = get_arg("url"); @@ -251,6 +367,16 @@ class Hex_Encode final : public Command public: Hex_Encode() : Command("hex_enc file") {} + std::string group() const override + { + return "misc"; + } + + std::string description() const override + { + return "Hex encode a given file"; + } + void go() override { auto hex_enc_f = [&](const uint8_t b[], size_t l) { output() << Botan::hex_encode(b, l); }; @@ -265,6 +391,16 @@ class Hex_Decode final : public Command public: Hex_Decode() : Command("hex_dec file") {} + std::string group() const override + { + return "misc"; + } + + std::string description() const override + { + return "Hex decode a given file"; + } + void go() override { auto hex_dec_f = [&](const uint8_t b[], size_t l) @@ -288,6 +424,16 @@ class Base64_Encode final : public Command public: Base64_Encode() : Command("base64_enc file") {} + std::string group() const override + { + return "misc"; + } + + std::string description() const override + { + return "Encode given file to Base64"; + } + void go() override { auto onData = [&](const uint8_t b[], size_t l) @@ -305,6 +451,16 @@ class Base64_Decode final : public Command public: Base64_Decode() : Command("base64_dec file") {} + std::string group() const override + { + return "misc"; + } + + std::string description() const override + { + return "Decode Base64 encoded file"; + } + void go() override { auto write_bin = [&](const uint8_t b[], size_t l) @@ -328,6 +484,16 @@ class Generate_Bcrypt final : public Command public: Generate_Bcrypt() : Command("gen_bcrypt --work-factor=12 password") {} + std::string group() const override + { + return "passhash"; + } + + std::string description() const override + { + return "Calculate the bcrypt password digest of a given file"; + } + void go() override { const std::string password = get_arg("password"); @@ -352,6 +518,16 @@ class Check_Bcrypt final : public Command public: Check_Bcrypt() : Command("check_bcrypt password hash") {} + std::string group() const override + { + return "passhash"; + } + + std::string description() const override + { + return "Checks a given bcrypt hash against hash"; + } + void go() override { const std::string password = get_arg("password"); @@ -379,6 +555,16 @@ class HMAC final : public Command public: HMAC() : Command("hmac --hash=SHA-256 --buf-size=4096 key *files") {} + std::string group() const override + { + return "hmac"; + } + + std::string description() const override + { + return "Compute the HMAC tag of given file(s)"; + } + void go() override { const std::string hash_algo = get_arg("hash"); |