From cb7b5ddba70ecf43c5d09a4a470d6348c154b801 Mon Sep 17 00:00:00 2001 From: Shwoomple <> Date: Fri, 12 Aug 2022 13:35:56 +0530 Subject: [PATCH] lib/cli: Add usage generator. --- src/lib/cli.cpp | 51 +++++++++++++++++++++++++++++++- src/test/cli_argument_parser.cpp | 20 +++++++++++++ src/tools/hexnet.cpp | 2 +- 3 files changed, 71 insertions(+), 2 deletions(-) diff --git a/src/lib/cli.cpp b/src/lib/cli.cpp index a9ca296..a8a3fbb 100644 --- a/src/lib/cli.cpp +++ b/src/lib/cli.cpp @@ -258,5 +258,54 @@ namespace CLI { // argument is not present, but this is not an error -> false, NOT_PRESENT, "" else return ErrorOr(false, ErrorCodes::NOT_PRESENT, std::string("")); } - //std::string ArgumentsParser::getUsage(); + + std::string ArgumentsParser::getUsage(){ + std::string usageString = "Help: " + this->programName + "\n\n\t" + this->description + "\n\n" + "Usage: " + this->programName; + + if(!this->flagsByShortName.empty()){ + usageString += " [-"; + } + + for(const auto& [key, value]: this->flagsByShortName){ + usageString.push_back(key); + } + + if(!this->flagsByShortName.empty()){ + usageString += "] "; + } + + for(const auto& [key, value]: this->optionsByShortName){ + usageString += "[-"; + usageString.push_back(key); + usageString += " " + value->placeholder + "] "; + } + + for(const auto& argument: this->arguments){ + usageString += argument.placeholder + " "; + } + + usageString += "\n\nFlags:\n"; + + for(const auto& [key, value]: this->flagsByShortName){ + usageString += "\t-"; + usageString.push_back(key); + usageString += ", --" + value->longName + "\n\t\t" + value->description + "\n"; + } + + usageString += "\nOptions:\n"; + + for(const auto& [key, value]: this->optionsByShortName){ + usageString += "\t-"; + usageString.push_back(key); + usageString += " " + value->placeholder + ", --" + value->longName + "=" + value->placeholder + "\n\t\t" + value->description + "\n"; + } + + usageString += "\nArguments:\n"; + + for(const auto& argument: this->arguments){ + usageString += "\t" + argument.placeholder + "\n\t\t" + argument.description + "\n"; + } + + return usageString; + } } diff --git a/src/test/cli_argument_parser.cpp b/src/test/cli_argument_parser.cpp index c58ae32..5c502e3 100644 --- a/src/test/cli_argument_parser.cpp +++ b/src/test/cli_argument_parser.cpp @@ -636,5 +636,25 @@ int main() { delete[] tooManyArgumentsTestParameterList; std::cout << "Passed too many arguments test." << std::endl; + std::vector helpTestFlags; + helpTestFlags.push_back(CLI::Flag('a', "apple", "throws an apple on Newton's head.")); + helpTestFlags.push_back(CLI::Flag('y', "yapple", "throws an yapple on Newton's head.")); + + std::vector helpTestOptions; + helpTestOptions.push_back(CLI::Option('b', "banana", "BANANA", "smack someone with a ripe banana.")); + helpTestOptions.push_back(CLI::Option('c', "corn", "CORNHUBBBBB", "visit cornhub.")); + + std::vector helpTestArguments; + helpTestArguments.push_back(CLI::Argument("WASH", "Number of times to wash my shark.")); + helpTestArguments.push_back(CLI::Argument("SHAKE", "Number of times to shake fist at cloud.")); + + const char** helpTestCommand = new const char*[1]; + helpTestCommand[0] = "universecreator"; + CLI::ArgumentsParser helpTestParser = CLI::ArgumentsParser(1, helpTestCommand, helpTestFlags, helpTestOptions, helpTestArguments, "Create a universe with a banana and an apple."); + + ASSERT(helpTestParser.getUsage() == "Help: universecreator\n\n\tCreate a universe with a banana and an apple.\n\nUsage: universecreator [-ay] [-b BANANA] [-c CORNHUBBBBB] WASH SHAKE \n\nFlags:\n\t-a, --apple\n\t\tthrows an apple on Newton's head.\n\t-y, --yapple\n\t\tthrows an yapple on Newton's head.\n\nOptions:\n\t-b BANANA, --banana=BANANA\n\t\tsmack someone with a ripe banana.\n\t-c CORNHUBBBBB, --corn=CORNHUBBBBB\n\t\tvisit cornhub.\n\nArguments:\n\tWASH\n\t\tNumber of times to wash my shark.\n\tSHAKE\n\t\tNumber of times to shake fist at cloud.\n"); + std::cout << "Passed Argument Parser usage test." << std::endl; + + return 0; } diff --git a/src/tools/hexnet.cpp b/src/tools/hexnet.cpp index 856f813..4878ebc 100644 --- a/src/tools/hexnet.cpp +++ b/src/tools/hexnet.cpp @@ -165,7 +165,7 @@ int main(int argc, char* argv[]){ std::vector arguments; arguments.push_back(CLI::Argument("PORT", "the port to use")); - CLI::ArgumentsParser cliParser = CLI::ArgumentsParser(argc, argv, flags, options, arguments); + CLI::ArgumentsParser cliParser = CLI::ArgumentsParser(argc, argv, flags, options, arguments, "Arbitrary tcp/udp connections in hex format."); if (cliParser.wrongUsage) { //TODO: spit out usage information generated by the parser