diff options
author | gramanas <anastasis.gramm2@gmail.com> | 2018-11-14 20:39:19 +0200 |
---|---|---|
committer | gramanas <anastasis.gramm2@gmail.com> | 2018-11-14 20:39:19 +0200 |
commit | 11bcc43a2c9e2787c861debda6b94e0550065402 (patch) | |
tree | e26a64040dfc86afa1d69f051e899e14c461e771 | |
parent | 7b3eb51ec3ea3f96dc3d794e799f0d61c3e1d7be (diff) | |
download | ck-11bcc43a2c9e2787c861debda6b94e0550065402.tar.gz ck-11bcc43a2c9e2787c861debda6b94e0550065402.tar.bz2 ck-11bcc43a2c9e2787c861debda6b94e0550065402.zip |
Simplify cli parser
-rw-r--r-- | src/actionparser.c | 73 | ||||
-rw-r--r-- | src/actionparser.h | 2 | ||||
-rw-r--r-- | src/actions.c | 9 |
3 files changed, 38 insertions, 46 deletions
diff --git a/src/actionparser.c b/src/actionparser.c index 9ca2867..398ba5e 100644 --- a/src/actionparser.c +++ b/src/actionparser.c @@ -175,7 +175,29 @@ int parse_RESTORE(UserOpt *opt) { return 0; } +void determine_action(UserOpt *opt) { + /* get action */ + if (!next_token()) { + opt->action = CK_WRONG_ACTION; + return; + } + + char actionName[STR_S] = ""; + opt->action = parser_get_action(token, actionName); + if (opt->action != CK_WRONG_ACTION) { + LOG("Action to perform: %s", actionName); + } + return; +} + int parse_vals(UserOpt *opt) { + /* find the action */ + determine_action(opt); + if (opt->action == CK_WRONG_ACTION) { + opt->err = PERR_UNKNOWN_ACTION; + return -1; + } + switch (opt->action) { #define X(ACTION) \ case CKA_##ACTION: \ @@ -203,21 +225,6 @@ CkAction parser_get_action(const char *name, char *actionName) { return CK_WRONG_ACTION; } -void determine_action(UserOpt *opt) { - /* get action */ - if (!next_token()) { - opt->action = CK_WRONG_ACTION; - return; - } - - char actionName[STR_S] = ""; - opt->action = parser_get_action(token, actionName); - if (opt->action != CK_WRONG_ACTION) { - LOG("Action to perform: %s", actionName); - } - return; -} - UserOpt make_empty_user_opt() { UserOpt opt; opt.action = CK_WRONG_ACTION; @@ -309,7 +316,7 @@ void verbose() { } } -void get_possible_action_strings(char *dest, CkAction ckAction) { +char * get_possible_action_strings(char *dest, CkAction ckAction) { char buf[STR_M] = ""; switch (ckAction) { #define X(ACTION) \ @@ -326,10 +333,11 @@ void get_possible_action_strings(char *dest, CkAction ckAction) { #undef X default: dest = NULL; - return; + return NULL; } strcpy(dest, buf); + return dest; } void print_parser_error(UserOpt *opt) { @@ -374,22 +382,14 @@ void print_parser_help() { char names[STR_M] = ""; ckhelp("ck - the config keeper"); ckhelp("Usage:"); - get_possible_action_strings(names, CKA_INIT); - ckhelp("Initialize: \t%s", names); - get_possible_action_strings(names, CKA_ADD); - ckhelp("Add config: \t%s", names); - get_possible_action_strings(names, CKA_DEL); - ckhelp("Delete config: \t%s", names); - get_possible_action_strings(names, CKA_EDIT); - ckhelp("Edit config: \t%s", names); - get_possible_action_strings(names, CKA_LIST); - ckhelp("List configs: \t%s", names); - get_possible_action_strings(names, CKA_SEARCH); - ckhelp("Search configs: \t%s", names); - get_possible_action_strings(names, CKA_RESTORE); - ckhelp("Restore links: \t%s", names); - get_possible_action_strings(names, CKA_HELP); - ckhelp("Help: \t%s", names); + ckhelp("Init\t%s", get_possible_action_strings(names, CKA_INIT)); + ckhelp("Add\t%s", get_possible_action_strings(names, CKA_ADD)); + ckhelp("Delete\t%s", get_possible_action_strings(names, CKA_DEL)); + ckhelp("Edit\t%s", get_possible_action_strings(names, CKA_EDIT)); + ckhelp("List\t%s", get_possible_action_strings(names, CKA_LIST)); + ckhelp("Search\t%s", get_possible_action_strings(names, CKA_SEARCH)); + ckhelp("Restore\t%s", get_possible_action_strings(names, CKA_RESTORE)); + ckhelp("Help\t%s", get_possible_action_strings(names, CKA_HELP)); report_help(); } @@ -416,13 +416,6 @@ int parse_action(int argc, const char **argv, UserOpt *opt) { print_parser_help(); return -1; } - /* find the action */ - determine_action(opt); - if (opt->action == CK_WRONG_ACTION) { - opt->err = PERR_UNKNOWN_ACTION; - print_parser_error(opt); - return -1; - } /* parse values */ if (parse_vals(opt)) { print_parser_error(opt); diff --git a/src/actionparser.h b/src/actionparser.h index 0b8d2da..24ad763 100644 --- a/src/actionparser.h +++ b/src/actionparser.h @@ -70,7 +70,7 @@ struct UserOptions { * and return the result enum */ int parse_action(int argc, const char **argv, UserOpt *opt); CkAction parser_get_action(const char *name, char *actionName); -void get_possible_action_strings(char *dest, CkAction ckAction); +char * get_possible_action_strings(char *dest, CkAction ckAction); void free_user_opt(UserOpt *opt); #endif // ACTIONPARSER_H diff --git a/src/actions.c b/src/actions.c index dde18ef..4620c0d 100644 --- a/src/actions.c +++ b/src/actions.c @@ -305,11 +305,10 @@ int run_HELP(UserOpt *opt, Conf *conf) { } switch(parser_get_action(list_get(opt->args), NULL)) { -#define X(ACTION) \ - case CKA_##ACTION: \ - get_possible_action_strings(tmp, CKA_##ACTION); \ - HELP("%s", tmp); \ - print_##ACTION##_help(); \ +#define X(ACTION) \ + case CKA_##ACTION: \ + HELP("%s", get_possible_action_strings(tmp, CKA_##ACTION)); \ + print_##ACTION##_help(); \ return 0; CK_ACTIONS #undef X |