Port arg_parse.{cpp,hpp} to C++.
This commit is contained in:
parent
836daeb69b
commit
e86ca506c1
@ -17,13 +17,11 @@
|
||||
*/
|
||||
#include "arg_parse.hpp"
|
||||
|
||||
#include <string.h>
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -17,7 +17,10 @@
|
||||
*/
|
||||
#pragma once
|
||||
|
||||
#include <stdio.h>
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <map>
|
||||
#include <vector>
|
||||
|
||||
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<enum cmd_id, std::vector<std::string>> 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 <cmd> [options]\n\n");
|
||||
std::cout << "USAGE: menu-helper <cmd> [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);
|
||||
|
Loading…
Reference in New Issue
Block a user