Add help message to display on invalid input

This commit is contained in:
Stephen Seo 2021-08-18 20:57:54 +09:00
parent 8841bc2511
commit 740a405703
2 changed files with 18 additions and 1 deletions

View file

@ -6,6 +6,7 @@
#include <vector> #include <vector>
namespace Helpers { namespace Helpers {
typedef std::unordered_map<std::string, std::string> ParseArgsResultType;
std::unordered_map<std::string, std::string> parseArgs( std::unordered_map<std::string, std::string> parseArgs(
int argc, int argc,
char **argv, char **argv,

View file

@ -1,11 +1,27 @@
#include <iostream> #include <iostream>
#include <string> #include <string>
#include <vector> #include <vector>
#include <stdexcept>
#include "helpers.hpp" #include "helpers.hpp"
void printHelp() {
std::cout << "Usage:\n";
std::cout << "-w <width> - specify the width in integers\n";
std::cout << "--string <string> - input string to print within the "
"specified width";
std::cout << std::endl;
}
int main(int argc, char **argv) { int main(int argc, char **argv) {
auto parsed = Helpers::parseArgs(argc, argv, {}, {"w", "string"}); Helpers::ParseArgsResultType parsed;
try {
parsed = Helpers::parseArgs(argc, argv, {}, {"w", "string"});
} catch(const std::invalid_argument &e) {
std::cout << "ERROR: invalid argument\n";
printHelp();
return 1;
}
// for(auto pair : parsed) { // for(auto pair : parsed) {
// std::cout << pair.first << ", " << pair.second << std::endl; // std::cout << pair.first << ", " << pair.second << std::endl;