[cli] Add --verbose-errors to enable verbose error reporting.
authorMatthias Bolte <matthias.bolte@googlemail.com>
Thu, 9 Apr 2009 16:14:57 +0000 (18:14 +0200)
committerMatthias Bolte <matthias.bolte@googlemail.com>
Thu, 9 Apr 2009 16:53:42 +0000 (18:53 +0200)
Signed-off-by: Matthias Bolte <matthias.bolte@googlemail.com>

cli/command.c
cli/core.c
cli/help.c
cli/option.c
cli/token.c
include/libvsccli/core.h
include/libvsccli/option.h
misc/error.c

index 29a8f28..dcee6d8 100644 (file)
@@ -168,7 +168,7 @@ vsc_cli_command_parse (struct VscError *error,
        if (command_name == NULL) {
                if (*input_tail != NULL) {
                        VSC__ERROR2 (error, VSC__ERROR_CODE__INTERNAL_ERROR,
-                                    "expecting command in '%s'", input);
+                                    "Expecting command in '%s'", input);
                }
 
                goto failure;
@@ -178,7 +178,7 @@ vsc_cli_command_parse (struct VscError *error,
 
        if (command_info == NULL) {
                VSC__ERROR2 (error, VSC__ERROR_CODE__INTERNAL_ERROR,
-                            "command '%s' is unknown", command_name);
+                            "Command '%s' is unknown", command_name);
 
                goto failure;
        }
@@ -361,7 +361,7 @@ vsc_cli_command_parse (struct VscError *error,
                if (_vsc_cli_option_list_lookup (command->option_list,
                                                 option_info->long_name) == NULL) {
                        VSC__ERROR2 (error, VSC__ERROR_CODE__INTERNAL_ERROR,
-                                    "command '%s' requires %s option",
+                                    "Command '%s' requires %s option",
                                     command_info->name,
                                     _vsc_cli_option_syntax (option_info, FALSE));
 
index 0bc4fbd..c46b6a2 100644 (file)
@@ -30,6 +30,7 @@
 #include "option.h"
 
 static int _initialized = FALSE;
+static int _verbose_errors = FALSE;
 
 const char *_vsc_cli_name = NULL;
 const char *_vsc_cli_help = NULL;
@@ -51,13 +52,13 @@ vsc_cli_init (struct VscError *error,
 
        if (_initialized) {
                VSC__ERROR1 (error, VSC__ERROR_CODE__INVALID_CALL,
-                            "Already initialized!");
+                            "Already initialized");
                return;
        }
 
        if (_vsc_cli_option_infos_lookup_data (option_infos, 0) != 0) {
                VSC__ERROR1 (error, VSC__ERROR_CODE__INVALID_ARGUMENT,
-                            "Data options can not be used as direct options.");
+                            "Data options can not be used as direct options");
                return;
        }
 
@@ -81,6 +82,24 @@ vsc_cli_cleanup (void)
        _initialized = FALSE;
 }
 
+void
+vsc_cli_error_fprint (struct VscError *error, FILE *stream)
+{
+       if (_verbose_errors) {
+               vsc_error_fprint (error, stream);
+       } else {
+               if (error->message != NULL) {
+                       if (error->code == VSC__ERROR_CODE__REMOTE_ERROR) {
+                               fprintf (stream, "Error: Remote: %s\n", error->message);
+                       } else {
+                               fprintf (stream, "Error: %s\n", error->message);
+                       }
+               } else {
+                       fprintf (stream, "Error: %s\n", vsc_error_string(error->code));
+               }
+       }
+}
+
 struct VscCliOption * /* option_list */
 vsc_cli_parse_option_list (struct VscError *error, const char *input,
                            const char **input_tail)
@@ -107,6 +126,11 @@ vsc_cli_parse_option_list (struct VscError *error, const char *input,
                }
 
                if (num_options_parsed > 0) {
+                       if (strcmp (option.info->long_name,
+                                   VSC_CLI__VERBOSE_ERRORS__OPTION_INFO__LONG_NAME) == 0) {
+                               _verbose_errors = TRUE;
+                       }
+
                        _vsc_cli_option_list_append (error, &option_list, &option);
                        vsc_free (&option.string);
 
index 3a9b105..4b111b5 100644 (file)
@@ -45,7 +45,7 @@ static const struct VscCliOptionInfo _help_option_infos[] = {
 const struct VscCliCommandInfo vsc_cli_help_command_info = {
        "help",
        "print help",
-       "Prints global or command specific help.",
+       "Prints global or command specific help",
        _help,
        _help_option_infos,
 };
@@ -69,7 +69,7 @@ _help (struct VscError *error, const struct VscCliOption *option_list,
 
                if (command_info == NULL) {
                        VSC__ERROR2 (error, VSC__ERROR_CODE__INTERNAL_ERROR,
-                                    "help: command '%s' is unknown", command_name);
+                                    "help: Command '%s' is unknown", command_name);
                        return;
                }
 
index 70cdfd5..3a39c99 100644 (file)
@@ -129,11 +129,11 @@ _vsc_cli_option_parse (struct VscError *error,
                if (option_info == NULL) {
                        if (command_name != NULL) {
                                VSC__ERROR2 (error, VSC__ERROR_CODE__INTERNAL_ERROR,
-                                            "unknown option %s for command '%s'",
+                                            "Unknown option %s for command '%s'",
                                             token, command_name);
                        } else  {
                                VSC__ERROR2 (error, VSC__ERROR_CODE__INTERNAL_ERROR,
-                                            "unknown option %s", token);
+                                            "Unknown option %s", token);
                        }
 
                        goto failure;
@@ -156,7 +156,7 @@ _vsc_cli_option_parse (struct VscError *error,
 
                        if (token == NULL) {
                                VSC__ERROR2 (error, VSC__ERROR_CODE__INTERNAL_ERROR,
-                                            "expected option syntax: %s",
+                                            "Expected option syntax: %s",
                                             _vsc_cli_option_syntax (option_info, FALSE));
                                goto failure;
                        }
@@ -168,7 +168,7 @@ _vsc_cli_option_parse (struct VscError *error,
                                vsc_error_init (error);
 
                                VSC__ERROR2 (error, VSC__ERROR_CODE__INTERNAL_ERROR,
-                                            "expected option syntax: %s",
+                                            "Expected option syntax: %s",
                                             _vsc_cli_option_syntax (option_info, FALSE));
                                goto failure;
                        }
@@ -186,7 +186,7 @@ _vsc_cli_option_parse (struct VscError *error,
 
                        if (token == NULL) {
                                VSC__ERROR2 (error, VSC__ERROR_CODE__INTERNAL_ERROR,
-                                            "expected option syntax: %s",
+                                            "Expected option syntax: %s",
                                             _vsc_cli_option_syntax (option_info, FALSE));
                                goto failure;
                        }
@@ -197,7 +197,7 @@ _vsc_cli_option_parse (struct VscError *error,
 
                default:
                        VSC__ERROR1 (error, VSC__ERROR_CODE__INTERNAL_ERROR,
-                                    "internal error");
+                                    "Internal error");
                        goto failure;
                }
 
@@ -211,7 +211,7 @@ _vsc_cli_option_parse (struct VscError *error,
 
                if (option_info == NULL) {
                        VSC__ERROR2 (error, VSC__ERROR_CODE__INTERNAL_ERROR,
-                                    "too many options for command '%s'",
+                                    "Too many options for command '%s'",
                                     command_name);
                        goto failure;
                }
index 826653d..e0117b1 100644 (file)
@@ -81,7 +81,7 @@ _vsc_cli_token_parse (struct VscError *error, const char *input, char **token,
 
        if (quote) {
                VSC__ERROR1 (error, VSC__ERROR_CODE__INTERNAL_ERROR,
-                            "input is malformed, missing \"");
+                            "Input is malformed, missing \"");
                return;
        }
 
index 66218cf..7c8b130 100644 (file)
@@ -36,6 +36,9 @@ vsc_cli_init (struct VscError *error,
 void
 vsc_cli_cleanup (void);
 
+void
+vsc_cli_error_fprint (struct VscError *error, FILE *stream);
+
 struct VscCliOption * /* option_list */
 vsc_cli_parse_option_list (struct VscError *error, const char *input,
                            const char **input_tail);
index 0bce7a8..5017cda 100644 (file)
 
 #include "types.h"
 
+#define VSC_CLI__VERBOSE_ERRORS__OPTION_INFO__LONG_NAME "verbose-errors"
+
+#define VSC_CLI__VERBOSE_ERRORS__OPTION_INFO \
+       { -1, VSC_CLI__VERBOSE_ERRORS__OPTION_INFO__LONG_NAME, \
+        "enable verbose error reporting", \
+        VSC_CLI__OPTION_TYPE__BOOL, FALSE }
+
 #ifdef __cplusplus
 extern "C" {
 #endif
index 04eaad3..4daebf8 100644 (file)
@@ -38,9 +38,9 @@ static struct VscError _error_default = {
        .occured = TRUE,
        .code = VSC__ERROR_CODE__OUT_OF_MEMORY,
        .message = NULL,
-       .file = "none",
+       .file = "None",
        .line = -1,
-       .function = "none",
+       .function = "None",
 };
 
 static void
@@ -138,7 +138,7 @@ vsc_error_fprint (struct VscError *error, FILE *stream)
 
        localtime_r (&error->timestamp.tv_sec, &time_info);
 
-       fprintf (stream, "%02d:%02d:%02d.%03d %s:%d %s : error [%s]",
+       fprintf (stream, "%02d:%02d:%02d.%03d %s:%d %s : Error [%s]",
                 time_info.tm_hour, time_info.tm_min, time_info.tm_sec,
                 (int) error->timestamp.tv_usec / 1000, error->file,
                 error->line, error->function,
@@ -152,7 +152,7 @@ vsc_error_fprint (struct VscError *error, FILE *stream)
 
        for (error_next = error->next; error_next != NULL;
             error_next = error_next->next) {
-               fprintf (stream, "             %s:%d %s : error [%s]",
+               fprintf (stream, "             %s:%d %s : Error [%s]",
                         error_next->file, error_next->line, error_next->function,
                         vsc_error_string (error_next->code));