summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJean Guyomarc'h <jean@guyomarch.bzh>2016-11-11 23:53:08 +0100
committerJean Guyomarc'h <jean@guyomarch.bzh>2016-11-11 23:53:08 +0100
commit168f370c6de47aa5469322b54b3ad792025e25d0 (patch)
treef665e951cab71eea9d4a1fa56c84b7d76e35ee79
parent45e1ccf312225efad21a616602aedb3f9f0188bf (diff)
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.c103
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,
1096 return endptr > str; 1096 return endptr > str;
1097} 1097}
1098 1098
1099static void
1100_ecore_getopt_use_optional_value(const Ecore_Getopt_Desc_Store *store,
1101 Ecore_Getopt_Value *value)
1102{
1103 switch (store->type)
1104 {
1105 case ECORE_GETOPT_TYPE_STR:
1106 *value->strp = (char *)store->def.strv;
1107 break;
1108
1109 case ECORE_GETOPT_TYPE_BOOL:
1110 *value->boolp = store->def.boolv;
1111 break;
1112
1113 case ECORE_GETOPT_TYPE_SHORT:
1114 *value->shortp = store->def.shortv;
1115 break;
1116
1117 case ECORE_GETOPT_TYPE_INT:
1118 *value->intp = store->def.intv;
1119 break;
1120
1121 case ECORE_GETOPT_TYPE_LONG:
1122 *value->longp = store->def.longv;
1123 break;
1124
1125 case ECORE_GETOPT_TYPE_USHORT:
1126 *value->ushortp = store->def.ushortv;
1127 break;
1128
1129 case ECORE_GETOPT_TYPE_UINT:
1130 *value->uintp = store->def.uintv;
1131 break;
1132
1133 case ECORE_GETOPT_TYPE_ULONG:
1134 *value->ulongp = store->def.ulongv;
1135 break;
1136
1137 case ECORE_GETOPT_TYPE_DOUBLE:
1138 *value->doublep = store->def.doublev;
1139 break;
1140 }
1141}
1142
1099static Eina_Bool 1143static Eina_Bool
1100_ecore_getopt_parse_store(const Ecore_Getopt *parser EINA_UNUSED, 1144_ecore_getopt_parse_store(const Ecore_Getopt *parser EINA_UNUSED,
1101 const Ecore_Getopt_Desc *desc, 1145 const Ecore_Getopt_Desc *desc,
@@ -1196,45 +1240,7 @@ error:
1196 return EINA_FALSE; 1240 return EINA_FALSE;
1197 1241
1198use_optional: 1242use_optional:
1199 switch (store->type) 1243 _ecore_getopt_use_optional_value(store, value);
1200 {
1201 case ECORE_GETOPT_TYPE_STR:
1202 *value->strp = (char *)store->def.strv;
1203 break;
1204
1205 case ECORE_GETOPT_TYPE_BOOL:
1206 *value->boolp = store->def.boolv;
1207 break;
1208
1209 case ECORE_GETOPT_TYPE_SHORT:
1210 *value->shortp = store->def.shortv;
1211 break;
1212
1213 case ECORE_GETOPT_TYPE_INT:
1214 *value->intp = store->def.intv;
1215 break;
1216
1217 case ECORE_GETOPT_TYPE_LONG:
1218 *value->longp = store->def.longv;
1219 break;
1220
1221 case ECORE_GETOPT_TYPE_USHORT:
1222 *value->ushortp = store->def.ushortv;
1223 break;
1224
1225 case ECORE_GETOPT_TYPE_UINT:
1226 *value->uintp = store->def.uintv;
1227 break;
1228
1229 case ECORE_GETOPT_TYPE_ULONG:
1230 *value->ulongp = store->def.ulongv;
1231 break;
1232
1233 case ECORE_GETOPT_TYPE_DOUBLE:
1234 *value->doublep = store->def.doublev;
1235 break;
1236 }
1237
1238 return EINA_TRUE; 1244 return EINA_TRUE;
1239} 1245}
1240 1246
@@ -2032,6 +2038,23 @@ _ecore_getopt_find_help(const Ecore_Getopt *parser)
2032 return NULL; 2038 return NULL;
2033} 2039}
2034 2040
2041static void
2042_ecore_getopt_set_defaults(const Ecore_Getopt *parser,
2043 Ecore_Getopt_Value *values)
2044{
2045 const Ecore_Getopt_Desc *desc = parser->descs;
2046
2047 for (; !_ecore_getopt_desc_is_sentinel(desc); desc++, values++)
2048 {
2049 if (desc->action == ECORE_GETOPT_ACTION_STORE)
2050 {
2051 const Ecore_Getopt_Desc_Store *const s = &desc->action_param.store;
2052 if (s->arg_req == ECORE_GETOPT_DESC_ARG_REQUIREMENT_OPTIONAL)
2053 _ecore_getopt_use_optional_value(s, values);
2054 }
2055 }
2056}
2057
2035EAPI int 2058EAPI int
2036ecore_getopt_parse(const Ecore_Getopt *parser, 2059ecore_getopt_parse(const Ecore_Getopt *parser,
2037 Ecore_Getopt_Value *values, 2060 Ecore_Getopt_Value *values,
@@ -2065,6 +2088,8 @@ ecore_getopt_parse(const Ecore_Getopt *parser,
2065 else 2088 else
2066 prog = parser->prog; 2089 prog = parser->prog;
2067 2090
2091 _ecore_getopt_set_defaults(parser, values);
2092
2068 nonargs = _ecore_getopt_parse_find_nonargs_base(parser, argc, argv); 2093 nonargs = _ecore_getopt_parse_find_nonargs_base(parser, argc, argv);
2069 if (nonargs < 0) 2094 if (nonargs < 0)
2070 goto error; 2095 goto error;