Ken Yourek / ucmd

Dependents:   nucleo_ucmd_helloworld

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers ucSwitchOpt.c Source File

ucSwitchOpt.c

00001 #include <string.h>
00002 #include "ucmd_internal.h"
00003 
00004 ucMemoryManager_INIT(ucSwitchOpt, ucSwitchOpt_COUNT);
00005 
00006 static ucSwitchOpt *create_switch_opt(void) {
00007     return ucMemoryManager_create();
00008 }
00009 
00010 ucArgOpt *ucSwitchOpt_get_arg_opt(ucSwitchOpt *p) {
00011     return ucArgOptOwner_get_arg_opt((ucArgOptOwner*)p);
00012 }
00013 
00014 ucSwitchOpt *ucSwitchOpt_get_next(ucSwitchOpt *p) {
00015     if (NULL == p) return NULL;
00016     return p->next;
00017 }
00018 
00019 ucSwitchOpt *ucSwitchOpt_find(ucSwitchOpt *p, const char *name) {
00020     while (NULL != p) {
00021         if (0 == strcmp(ucOpt_get_name((ucOpt*)p), name)) {
00022             return p;
00023         }
00024         p = ucSwitchOpt_get_next(p);
00025     }
00026     return NULL;
00027 }
00028 
00029 ucSwitchOpt *ucSwitchOpt_init(ucSwitchOpt *p, const char *name, const char *desc, ucBool is_required, ucArgOpt *arg_opt, ucSwitchOpt *next) {
00030     if (NULL == p) return NULL;
00031     if (NULL == ucArgOptOwner_init((ucArgOptOwner*)p, name, desc, is_required, arg_opt)) return NULL;
00032 
00033     p->next = next;
00034     return p;
00035 }
00036 
00037 ucSwitchOpt *ucSwitchOpt_create(const char *name, const char *desc, ucArgOpt *arg_opt, ucSwitchOpt *next) {
00038     return ucSwitchOpt_init(create_switch_opt(), name, desc, ucBool_FALSE, arg_opt, next);
00039 }
00040 
00041 ucSwitchOpt *ucSwitchOpt_create_required(const char *name, const char *desc, ucArgOpt *arg_opt, ucSwitchOpt *next) {
00042     return ucSwitchOpt_init(create_switch_opt(), name, desc, ucBool_TRUE, arg_opt, next);
00043 }
00044 
00045 void ucSwitchOpt_destroy(ucSwitchOpt *p) {
00046     ucMemoryManager_destroy(p);
00047 }
00048 
00049 void ucSwitchOpt_destroy_chain(ucSwitchOpt *p) {
00050     ucSwitchOpt *next = p;
00051     while (NULL != next) {
00052         p = next;
00053         next = ucSwitchOpt_get_next(p);
00054         ucArgOpt_destroy_chain(ucSwitchOpt_get_arg_opt(p));
00055         ucSwitchOpt_destroy(p);
00056     }
00057 }
00058 
00059 const char *ucSwitchOpt_format_validation_err(ucSwitchOpt *p, ucCmdLine *cmd, ucSwitchTok *switch_tok) {
00060 
00061     /* check if the switch option is required */
00062     if (ucOpt_is_required((ucOpt*)p)) {
00063 
00064         /* check if the switch token is missing */
00065         if (NULL == switch_tok) {
00066 
00067             /* the switch is required, but it is not
00068                present, so send the error */
00069             return ucCmdLine_format_response(cmd, "%sthe switch \"%s\" is required.", ucOpt_validation_err_invalid_switch_prefix, ucOpt_get_name((ucOpt*)p));
00070         }
00071     }
00072 
00073     /* return the result of the argument validation */
00074     return ucArgOptOwner_format_validation_err(
00075         (ucArgOptOwner*)p, 
00076         cmd, 
00077         ucSwitchTok_get_arg(switch_tok), 
00078         ucOpt_get_name((ucOpt*)p)
00079     );
00080 }
00081