diff --git a/src/arg_parse.cpp b/src/arg_parse.cpp index b428043..eaf1ca1 100644 --- a/src/arg_parse.cpp +++ b/src/arg_parse.cpp @@ -17,13 +17,11 @@ */ #include "arg_parse.hpp" -#include - -enum cmd_id parse_args(const char *cmd) { - for(int i = 0; i < (int)ARRAY_LEN(commands); ++i) { - for(int j = 0; j < (int)ARRAY_LEN(commands[i].str); ++j) { - if(commands[i].str[j] && strcmp(commands[i].str[j], cmd) == 0) - return commands[i].id; +enum cmd_id parse_args(const std::string &cmd) { + for(const auto &command : commands) { + for(const auto &alias : command.second) { + if(cmd == alias) + return command.first; } } diff --git a/src/arg_parse.hpp b/src/arg_parse.hpp index 1e8dd97..cfa0826 100644 --- a/src/arg_parse.hpp +++ b/src/arg_parse.hpp @@ -17,7 +17,10 @@ */ #pragma once -#include +#include +#include +#include +#include enum cmd_id { CMD_UNKNOWN = 0, @@ -26,35 +29,29 @@ enum cmd_id { CMD_VERSION, }; -struct cmd { - enum cmd_id id; - const char *str[3]; -}; - -static const struct cmd commands[] = { +static const std::map> commands = { { CMD_ADD, {"add", "new"} }, { CMD_HELP, {"help", "-h", "--help"} }, { CMD_VERSION, {"version", "-v", "--version"} }, }; static inline void print_version(void) { - printf("menu-helper v%s\n\n", VERSION); + std::cout << "menu-helper v" << VERSION << "\n" << std::endl; } static inline void print_usage(void) { - printf("USAGE: menu-helper [options]\n\n"); + std::cout << "USAGE: menu-helper [options]\n" << std::endl; } static inline void print_help(void) { print_version(); print_usage(); - printf("COMMANDS:\n" + std::cout << "COMMANDS:\n" "\tadd, new Add a new recipe to the database\n" "\thelp, -h, --help Show this help information.\n" "\tversion, -v, --version Show version information.\n" - "\n"); + << std::endl; } - -enum cmd_id parse_args(const char *cmd); +enum cmd_id parse_args(const std::string &cmd);