diff options
author | Jean Guyomarc'h <jean@guyomarch.bzh> | 2016-11-11 23:53:08 +0100 |
---|---|---|
committer | Jean Guyomarc'h <jean@guyomarch.bzh> | 2016-11-11 23:53:08 +0100 |
commit | 168f370c6de47aa5469322b54b3ad792025e25d0 (patch) | |
tree | f665e951cab71eea9d4a1fa56c84b7d76e35ee79 | |
parent | 45e1ccf312225efad21a616602aedb3f9f0188bf (diff) | |
download | efl-devs/jayji/ecore-getopt.tar.gz |
ecore_getopt: ensure optional parameters are always setdevs/jayji/ecore-getopt
Right now, optional parameters (STORE_DEF_*) must be handled by the developer
and not by Ecore_Getopt (unless the option is specified without any
parameters). Now, we make sure that all optional parameters are assigned to
their default values. Always. Unless they get overriden by the proper
command-line parameter.
This improves the usability of Ecore_Getopt and reduces the possible
mistakes a developer can do when using Ecore_Getopt.
-rw-r--r-- | src/lib/ecore/ecore_getopt.c | 103 |
1 files changed, 64 insertions, 39 deletions
diff --git a/src/lib/ecore/ecore_getopt.c b/src/lib/ecore/ecore_getopt.c index c85a71a15d..68495a2799 100644 --- a/src/lib/ecore/ecore_getopt.c +++ b/src/lib/ecore/ecore_getopt.c @@ -1096,6 +1096,50 @@ _ecore_getopt_parse_double(const char *str, return endptr > str; } +static void +_ecore_getopt_use_optional_value(const Ecore_Getopt_Desc_Store *store, + Ecore_Getopt_Value *value) +{ + switch (store->type) + { + case ECORE_GETOPT_TYPE_STR: + *value->strp = (char *)store->def.strv; + break; + + case ECORE_GETOPT_TYPE_BOOL: + *value->boolp = store->def.boolv; + break; + + case ECORE_GETOPT_TYPE_SHORT: + *value->shortp = store->def.shortv; + break; + + case ECORE_GETOPT_TYPE_INT: + *value->intp = store->def.intv; + break; + + case ECORE_GETOPT_TYPE_LONG: + *value->longp = store->def.longv; + break; + + case ECORE_GETOPT_TYPE_USHORT: + *value->ushortp = store->def.ushortv; + break; + + case ECORE_GETOPT_TYPE_UINT: + *value->uintp = store->def.uintv; + break; + + case ECORE_GETOPT_TYPE_ULONG: + *value->ulongp = store->def.ulongv; + break; + + case ECORE_GETOPT_TYPE_DOUBLE: + *value->doublep = store->def.doublev; + break; + } +} + static Eina_Bool _ecore_getopt_parse_store(const Ecore_Getopt *parser EINA_UNUSED, const Ecore_Getopt_Desc *desc, @@ -1196,45 +1240,7 @@ error: return EINA_FALSE; use_optional: - switch (store->type) - { - case ECORE_GETOPT_TYPE_STR: - *value->strp = (char *)store->def.strv; - break; - - case ECORE_GETOPT_TYPE_BOOL: - *value->boolp = store->def.boolv; - break; - - case ECORE_GETOPT_TYPE_SHORT: - *value->shortp = store->def.shortv; - break; - - case ECORE_GETOPT_TYPE_INT: - *value->intp = store->def.intv; - break; - - case ECORE_GETOPT_TYPE_LONG: - *value->longp = store->def.longv; - break; - - case ECORE_GETOPT_TYPE_USHORT: - *value->ushortp = store->def.ushortv; - break; - - case ECORE_GETOPT_TYPE_UINT: - *value->uintp = store->def.uintv; - break; - - case ECORE_GETOPT_TYPE_ULONG: - *value->ulongp = store->def.ulongv; - break; - - case ECORE_GETOPT_TYPE_DOUBLE: - *value->doublep = store->def.doublev; - break; - } - + _ecore_getopt_use_optional_value(store, value); return EINA_TRUE; } @@ -2032,6 +2038,23 @@ _ecore_getopt_find_help(const Ecore_Getopt *parser) return NULL; } +static void +_ecore_getopt_set_defaults(const Ecore_Getopt *parser, + Ecore_Getopt_Value *values) +{ + const Ecore_Getopt_Desc *desc = parser->descs; + + for (; !_ecore_getopt_desc_is_sentinel(desc); desc++, values++) + { + if (desc->action == ECORE_GETOPT_ACTION_STORE) + { + const Ecore_Getopt_Desc_Store *const s = &desc->action_param.store; + if (s->arg_req == ECORE_GETOPT_DESC_ARG_REQUIREMENT_OPTIONAL) + _ecore_getopt_use_optional_value(s, values); + } + } +} + EAPI int ecore_getopt_parse(const Ecore_Getopt *parser, Ecore_Getopt_Value *values, @@ -2065,6 +2088,8 @@ ecore_getopt_parse(const Ecore_Getopt *parser, else prog = parser->prog; + _ecore_getopt_set_defaults(parser, values); + nonargs = _ecore_getopt_parse_find_nonargs_base(parser, argc, argv); if (nonargs < 0) goto error; |