[cli] Parse untagged option with defined type.
authorMatthias Bolte <matthias.bolte@googlemail.com>
Thu, 23 Apr 2009 13:40:59 +0000 (15:40 +0200)
committerMatthias Bolte <matthias.bolte@googlemail.com>
Thu, 23 Apr 2009 13:40:59 +0000 (15:40 +0200)
Signed-off-by: Matthias Bolte <matthias.bolte@googlemail.com>

cli/option.c

index 3ec6512..771e973 100644 (file)
@@ -99,7 +99,6 @@ _vsc_cli_option_parse (struct VscError *error,
                        struct VscCliOption *option)
 {
        int option_parsed = TRUE;
-       const struct VscCliOptionInfo *option_info;
        const char *tail = NULL;
        char *token = NULL;
        int num_dashes = 0;
@@ -141,10 +140,10 @@ _vsc_cli_option_parse (struct VscError *error,
                        goto failure;
                }
 
-               option_info = _vsc_cli_option_infos_lookup_tagged
-                                (option_infos, token + num_dashes, num_dashes > 1);
+               option->info = _vsc_cli_option_infos_lookup_tagged
+                                 (option_infos, token + num_dashes, num_dashes > 1);
 
-               if (option_info == NULL) {
+               if (option->info == NULL) {
                        if (command_name != NULL) {
                                VSC__ERROR2 (error, VSC__ERROR_CODE__INTERNAL_ERROR,
                                             "Unknown option %s for command '%s'",
@@ -157,9 +156,7 @@ _vsc_cli_option_parse (struct VscError *error,
                        goto failure;
                }
 
-               option->info = option_info;
-
-               switch (option_info->type) {
+               switch (option->info->type) {
                case VSC_CLI__OPTION_TYPE__BOOLEAN:
                        break;
 
@@ -175,7 +172,7 @@ _vsc_cli_option_parse (struct VscError *error,
                        if (token == NULL) {
                                VSC__ERROR2 (error, VSC__ERROR_CODE__INTERNAL_ERROR,
                                             "Expected option syntax: %s",
-                                            _vsc_cli_option_syntax (option_info, FALSE));
+                                            _vsc_cli_option_syntax (option->info, FALSE));
                                goto failure;
                        }
 
@@ -187,7 +184,7 @@ _vsc_cli_option_parse (struct VscError *error,
 
                                VSC__ERROR2 (error, VSC__ERROR_CODE__INTERNAL_ERROR,
                                             "Expected option syntax: %s",
-                                            _vsc_cli_option_syntax (option_info, FALSE));
+                                            _vsc_cli_option_syntax (option->info, FALSE));
                                goto failure;
                        }
 
@@ -205,7 +202,7 @@ _vsc_cli_option_parse (struct VscError *error,
                        if (token == NULL) {
                                VSC__ERROR2 (error, VSC__ERROR_CODE__INTERNAL_ERROR,
                                             "Expected option syntax: %s",
-                                            _vsc_cli_option_syntax (option_info, FALSE));
+                                            _vsc_cli_option_syntax (option->info, FALSE));
                                goto failure;
                        }
 
@@ -224,19 +221,48 @@ _vsc_cli_option_parse (struct VscError *error,
                        goto failure;
                }
        } else if (untagged_option_index >= 0) {
-               option_info = _vsc_cli_option_infos_lookup_untagged
-                                (option_infos, untagged_option_index);
+               option->info = _vsc_cli_option_infos_lookup_untagged
+                                 (option_infos, untagged_option_index);
 
-               if (option_info == NULL) {
+               if (option->info == NULL) {
                        VSC__ERROR2 (error, VSC__ERROR_CODE__INTERNAL_ERROR,
                                     "Too many untagged options for command '%s'",
                                     command_name);
                        goto failure;
                }
 
-               option->info = option_info;
-               option->string = token;
-               token = NULL;
+               switch (option->info->type) {
+               case VSC_CLI__OPTION_TYPE__BOOLEAN:
+                       VSC__ERROR1 (error, VSC__ERROR_CODE__INVALID_ARGUMENT,
+                                    "Boolean options can not untagged");
+
+                       goto failure;
+
+               case VSC_CLI__OPTION_TYPE__NUMBER:
+                       option->number = vsc_strtoi (error, token, NULL, 10);
+
+                       if (error->occured) {
+                               vsc_error_cleanup (error);
+                               vsc_error_init (error);
+
+                               VSC__ERROR2 (error, VSC__ERROR_CODE__INTERNAL_ERROR,
+                                            "Expected option syntax: %s",
+                                            _vsc_cli_option_syntax (option->info, FALSE));
+                               goto failure;
+                       }
+
+                       break;
+
+               case VSC_CLI__OPTION_TYPE__STRING:
+                       option->string = token;
+                       token = NULL;
+                       break;
+
+               default:
+                       VSC__ERROR1 (error, VSC__ERROR_CODE__INTERNAL_ERROR,
+                                    "Internal error");
+                       goto failure;
+               }
 
                if (error->occured) {
                        VSC__APPEND_ERROR0 (error, VSC__ERROR_CODE__TRACE);
@@ -257,6 +283,7 @@ cleanup:
 
 failure:
        vsc_free (&option->string);
+       memset (option, 0, sizeof (struct VscCliOption));
 
        option_parsed = FALSE;