From 2e7bf3b8c7648920d533a6c7673d546aa573e0c8 Mon Sep 17 00:00:00 2001 From: Cameron Reed Date: Sat, 16 Mar 2024 10:50:00 -0600 Subject: [PATCH] Fix some weird choices I made two years ago This includes a potential two byte memory leak --- Inc/argparser.h | 5 +++-- Src/argparser.cpp | 54 +++++++++++++++++++++++++++++------------------ 2 files changed, 37 insertions(+), 22 deletions(-) diff --git a/Inc/argparser.h b/Inc/argparser.h index 2f88d72..499dc2d 100644 --- a/Inc/argparser.h +++ b/Inc/argparser.h @@ -8,8 +8,7 @@ namespace Arguments { enum ERROR { - IMPOSSIBLE = -1, - NO_ERROR, + NO_ERROR = 0, ERROR_UNKNOWN_OPTION, ERROR_INCORRECT_TYPE, ERROR_MISSING_ARGUMENT, @@ -70,10 +69,12 @@ private: ERROR handle_short_option(const char option, const char* next_value); ERROR handle_positional_argument(const char* arg); ERROR get_option_data(Option* opt, const char* option_name, const char* data_str); + ERROR get_option_data(Option* opt, const char option_name, const char* data_str); ERROR unknown_option(const char* option); ERROR unknown_option(const char option); ERROR incorrect_type(const char* option, const char* got); ERROR missing_argument(const char* option); + ERROR missing_argument(const char option); ERROR missing_positional_argument(const char* arg); private: const char* m_program_name; diff --git a/Src/argparser.cpp b/Src/argparser.cpp index d3cfc30..e7d99e8 100644 --- a/Src/argparser.cpp +++ b/Src/argparser.cpp @@ -129,16 +129,11 @@ ERROR Parser::handle_long_option(const char* option, const char* next_value) return unknown_option(option); - } else { + } - matched_option->m_found = true; - return get_option_data(matched_option, matched_option->m_name, next_value); - } - - std::cout << "Congratulations! You have reached an impossible state" << std::endl << std::endl; - std::cout << "Reality is broken :)" << std::endl; - return IMPOSSIBLE; + matched_option->m_found = true; + return get_option_data(matched_option, option, next_value); } ERROR Parser::handle_short_option(const char option, const char* next_value) @@ -159,20 +154,11 @@ ERROR Parser::handle_short_option(const char option, const char* next_value) return unknown_option(option); - } else { - - matched_option->m_found = true; - char* name = new char[2]; - name[0] = matched_option->m_short_name; - name[1] = 0; - return get_option_data(matched_option, name, next_value); - delete[] name; - } - std::cout << "Congratulations! You have reached impossible state #2" << std::endl << std::endl; - std::cout << "Reality is broken :)" << std::endl; - return IMPOSSIBLE; + + matched_option->m_found = true; + return get_option_data(matched_option, option, next_value); } ERROR Parser::handle_positional_argument(const char* arg) @@ -218,6 +204,27 @@ ERROR Parser::get_option_data(Option* opt, const char* option_name, const char* return NO_ERROR; } +ERROR Parser::get_option_data(Option* opt, const char option_name, const char* data_str) +{ + if (opt->m_type == FLAG) + return NO_ERROR; + + if (data_str == nullptr || data_str[0] == '-') + return missing_argument(option_name); + + if (opt->m_type == INT) { + int* val = new int; + if ((*val = stringToInt(data_str)) == -1) + return incorrect_type(opt->m_name, data_str); + opt->data = val; + } else { + opt->data = (void*) data_str; + } + + m_opt_index++; + return NO_ERROR; +} + ERROR Parser::unknown_option(const char* option) { std::cout << m_program_name << ": invalid option '" << option << '\'' << std::endl << std::endl; @@ -248,6 +255,13 @@ ERROR Parser::missing_argument(const char* option) return ERROR_MISSING_ARGUMENT; } +ERROR Parser::missing_argument(const char option) +{ + std::cout << m_program_name << ": Missing argument for option '" << option << '\'' << std::endl; + + return ERROR_MISSING_ARGUMENT; +} + ERROR Parser::missing_positional_argument(const char* arg) { std::cout << m_program_name << ": Missing required positional argument '" << arg << "'" << std::endl << std::endl;