From 740a40570353ec4a46ee97e96675f007e2c8e137 Mon Sep 17 00:00:00 2001 From: Stephen Seo Date: Wed, 18 Aug 2021 20:57:54 +0900 Subject: [PATCH] Add help message to display on invalid input --- cpp_impl/src/helpers.hpp | 1 + cpp_impl/src/main.cpp | 18 +++++++++++++++++- 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/cpp_impl/src/helpers.hpp b/cpp_impl/src/helpers.hpp index c3b5469..c2b61eb 100644 --- a/cpp_impl/src/helpers.hpp +++ b/cpp_impl/src/helpers.hpp @@ -6,6 +6,7 @@ #include namespace Helpers { + typedef std::unordered_map ParseArgsResultType; std::unordered_map parseArgs( int argc, char **argv, diff --git a/cpp_impl/src/main.cpp b/cpp_impl/src/main.cpp index c6fddaa..295db86 100644 --- a/cpp_impl/src/main.cpp +++ b/cpp_impl/src/main.cpp @@ -1,11 +1,27 @@ #include #include #include +#include #include "helpers.hpp" +void printHelp() { + std::cout << "Usage:\n"; + std::cout << "-w - specify the width in integers\n"; + std::cout << "--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;