Add help message to display on invalid input
This commit is contained in:
parent
8841bc2511
commit
740a405703
2 changed files with 18 additions and 1 deletions
|
@ -6,6 +6,7 @@
|
|||
#include <vector>
|
||||
|
||||
namespace Helpers {
|
||||
typedef std::unordered_map<std::string, std::string> ParseArgsResultType;
|
||||
std::unordered_map<std::string, std::string> parseArgs(
|
||||
int argc,
|
||||
char **argv,
|
||||
|
|
|
@ -1,11 +1,27 @@
|
|||
#include <iostream>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <stdexcept>
|
||||
|
||||
#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) {
|
||||
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) {
|
||||
// std::cout << pair.first << ", " << pair.second << std::endl;
|
||||
|
|
Loading…
Reference in a new issue