lib/cli: Add usage generator.

BodgeMaster-unfinished
Shwoomple 2022-08-12 13:35:56 +05:30
parent e0648720bb
commit cb7b5ddba7
3 changed files with 71 additions and 2 deletions

View File

@ -258,5 +258,54 @@ namespace CLI {
// argument is not present, but this is not an error -> false, NOT_PRESENT, "" // argument is not present, but this is not an error -> false, NOT_PRESENT, ""
else return ErrorOr<std::string>(false, ErrorCodes::NOT_PRESENT, std::string("")); else return ErrorOr<std::string>(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;
}
} }

View File

@ -636,5 +636,25 @@ int main() {
delete[] tooManyArgumentsTestParameterList; delete[] tooManyArgumentsTestParameterList;
std::cout << "Passed too many arguments test." << std::endl; std::cout << "Passed too many arguments test." << std::endl;
std::vector<CLI::Flag> 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<CLI::Option> 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<CLI::Argument> 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; return 0;
} }

View File

@ -165,7 +165,7 @@ int main(int argc, char* argv[]){
std::vector<CLI::Argument> arguments; std::vector<CLI::Argument> arguments;
arguments.push_back(CLI::Argument("PORT", "the port to use")); 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) { if (cliParser.wrongUsage) {
//TODO: spit out usage information generated by the parser //TODO: spit out usage information generated by the parser