Ken Yourek / ucmd

Dependents:   nucleo_ucmd_helloworld

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers ucArgOptOwner.c Source File

ucArgOptOwner.c

00001 #include "ucmd_internal.h"
00002 
00003 ucArgOpt *ucArgOptOwner_get_arg_opt(ucArgOptOwner *p) {
00004     if (NULL == p) return NULL;
00005     return p->arg_opt;
00006 }
00007 
00008 ucArgOptOwner *ucArgOptOwner_init(ucArgOptOwner *p, const char *name, const char *desc, ucBool is_required, ucArgOpt *arg_opt) {
00009     if (NULL == p) return NULL;
00010     if (NULL == ucOpt_init((ucOpt*)p, name, desc, is_required)) return NULL;
00011     p->arg_opt = arg_opt;
00012     return p;
00013 }
00014 
00015 const char *ucArgOptOwner_format_validation_err(ucArgOptOwner *p, ucCmdLine *cmd, ucArgTok *arg_tok, const char *switch_name) {
00016     int max_arg_tok_count;
00017 
00018     /* set the prefix for error messages */
00019     const char *validation, *prefix = switch_name == NULL ? ucOpt_validation_err_invalid_argument_prefix : ucOpt_validation_err_invalid_switch_argument_prefix;
00020 
00021     /* get the first argument option */
00022     ucArgOpt *arg_opt = ucArgOptOwner_get_arg_opt(p);
00023 
00024     /* check if an argument option does NOT exist */
00025     if (NULL == arg_opt) {
00026 
00027         /* check if the argumen token DOES exist */
00028         if (NULL != arg_tok) {
00029 
00030             /* the option does NOT exist, but the token DOES, so there's an error */
00031             return NULL == switch_name
00032                 ? ucCmdLine_format_response(cmd, "%sno argument options exist for command \"%s\".", prefix, ucTok_get_value(ucCmdLine_get_cmd_tok(cmd)))
00033                 : ucCmdLine_format_response(cmd, "%sno argument options exist for switch \"%s\".", prefix, switch_name);
00034         }
00035 
00036         /* neither the option nor the token exist, so no error here */
00037         return NULL;
00038     }
00039 
00040     /* loop through all the argument options */
00041     max_arg_tok_count = 0;
00042     while (NULL != arg_opt) {
00043 
00044         /* validate this argument option agains the current token */
00045         validation = ucArgOpt_format_validation_err(arg_opt, cmd, arg_tok, switch_name);
00046         if (NULL != validation) return validation;
00047 
00048         /* get the number of tokens that this option allows */
00049         max_arg_tok_count = ucArgOpt_get_max_tok_count(arg_opt);
00050 
00051         /* move to the next option and the next token */
00052         arg_opt = ucArgOpt_get_next(arg_opt);
00053         arg_tok = ucArgTok_get_next(arg_tok);
00054     }
00055 
00056     /* check if we have any remaining tokens */
00057     if (NULL != arg_tok) {
00058 
00059         /* check if the last argument option does NOT allow multiple tokens */
00060         if (2 > max_arg_tok_count) {
00061 
00062             /* we have remaining tokens but no arguments for them, so there's an error */
00063             return NULL == switch_name
00064                 ? ucCmdLine_format_response(cmd, "%sno option exists for argument \"%s\".", prefix, ucTok_get_value(arg_tok))
00065                 : ucCmdLine_format_response(cmd, "%sno option exists for \"%s\" argument \"%s\".", prefix, switch_name, ucTok_get_value(arg_tok));
00066         }
00067     }
00068 
00069     /* return no error */
00070     return NULL;
00071 }
00072