diff options
author | gramanas <anastasis.gramm2@gmail.com> | 2018-10-17 20:00:57 +0300 |
---|---|---|
committer | gramanas <anastasis.gramm2@gmail.com> | 2018-10-17 20:00:57 +0300 |
commit | 4c6d49f7d8ee1e6e6b849f16dc821e83d8967af8 (patch) | |
tree | 6fb28baabbbaa2718ba597d6fefe7bb7cd87903b /src/actionparser.c | |
parent | 37623394d364b55aa8f949fd8aa51edf65d9da10 (diff) | |
download | ck-4c6d49f7d8ee1e6e6b849f16dc821e83d8967af8.tar.gz ck-4c6d49f7d8ee1e6e6b849f16dc821e83d8967af8.tar.bz2 ck-4c6d49f7d8ee1e6e6b849f16dc821e83d8967af8.zip |
[v0.7] Add help subcommand
Diffstat (limited to 'src/actionparser.c')
-rw-r--r-- | src/actionparser.c | 61 |
1 files changed, 36 insertions, 25 deletions
diff --git a/src/actionparser.c b/src/actionparser.c index c8c70db..aee9732 100644 --- a/src/actionparser.c +++ b/src/actionparser.c @@ -148,7 +148,15 @@ int parse_SEARCH(UserOpt *opt) { } int parse_HELP(UserOpt *opt) { - return -1; + /* Help expects a maximum of 1 argument */ + if (optNum <= pos || optNum > pos + 1) { + opt->err = PERR_HELP_WRONG; + return -1; + } + + int arg_num = optNum - pos; + fill_args_list(arg_num, opt); + return 0; } @@ -164,26 +172,34 @@ int parse_vals(UserOpt *opt) { } } -void determine_action(UserOpt *opt) { - /* get action */ - if (next_token() == -1) { - opt->action = CK_WRONG_ACTION; - return; - } - +CkAction parser_get_action(const char *name, char *actionName) { int i; #define X(ACTION) \ for (i = 1; i < atoi(str##ACTION[0]) + 1; i++) { \ - if (strcmp(token, str##ACTION[i]) == 0) { \ - opt->action = CKA_##ACTION; \ - LOG("Action to perform: %s", str##ACTION[1]); \ - return; \ + if (strcmp(name, str##ACTION[i]) == 0) { \ + if (actionName) { \ + strcpy(actionName, str##ACTION[1]); \ + } \ + return CKA_##ACTION; \ } \ } CK_ACTIONS; #undef X + return CK_WRONG_ACTION; +} + +void determine_action(UserOpt *opt) { + /* get action */ + if (next_token() == -1) { + opt->action = CK_WRONG_ACTION; + return; + } - opt->action = CK_WRONG_ACTION; + char actionName[STR_S]; + opt->action = parser_get_action(token, actionName); + if (opt->action != CK_WRONG_ACTION) { + LOG("Action to perform: %s", actionName); + } return; } @@ -284,7 +300,7 @@ void verbose() { } void get_possible_action_strings(char *dest, CkAction ckAction) { - char buf[STR_S]; + char buf[STR_M]; switch (ckAction) { #define X(ACTION) \ case CKA_##ACTION: \ @@ -308,15 +324,15 @@ void get_possible_action_strings(char *dest, CkAction ckAction) { void print_parser_error(UserOpt *opt) { char errStr[STR_L]; - char names[STR_S]; + char names[STR_M]; get_possible_action_strings(names, opt->action); switch (opt->err) { case PERR_NOERR: return; case PERR_UNKONW_ACTION: - sprintf(errStr, "Unknown action: %s", token); - break; + ERR("Unknown action: %s", token); + return; case PERR_INIT_WRONG: sprintf(errStr, "Initialize database\nUsage: %s version_control_dir secret_dir", names); break; @@ -336,14 +352,13 @@ void print_parser_error(UserOpt *opt) { sprintf(errStr, "Search through the configs with grep\nUsage: %s search-term", names); break; case PERR_HELP_WRONG: - sprintf(errStr, "Usage: ........"); - break; + sprintf(errStr, "Get help for a ck action.\nUsage: %s action", names); } HELP("%s", errStr); } void print_parser_help() { - char names[STR_S]; + char names[STR_M]; ckhelp("ck - the config keeper"); ckhelp("Usage:"); get_possible_action_strings(names, CKA_INIT); @@ -359,7 +374,7 @@ void print_parser_help() { get_possible_action_strings(names, CKA_SEARCH); ckhelp("Search: \t%s", names); get_possible_action_strings(names, CKA_HELP); - ckhelp("Print this: \t%s", names); + ckhelp("Actions Help: \t%s", names); report_help(); } @@ -396,10 +411,6 @@ int parse_action(int argc, char* argv[], UserOpt *opt) { print_parser_error(opt); return -1; } - if (opt->action == CKA_HELP) { - print_parser_help(); - return -1; - } // parse values if (parse_vals(opt)) { |