diff --git a/src/rifiuti-vista.c b/src/rifiuti-vista.c index 9bd9d8b..74ad633 100644 --- a/src/rifiuti-vista.c +++ b/src/rifiuti-vista.c @@ -280,7 +280,7 @@ main (int argc, "Parse index files in C:\\$Recycle.bin style folder " "and dump recycle bin data. Can also dump a single index file.")); rifiuti_setup_opt_ctx (&context, RECYCLE_BIN_TYPE_DIR); - exit_status = rifiuti_parse_opt_ctx (&context, &argc, &argv); + exit_status = rifiuti_parse_opt_ctx (&context, &argv, &error); if (exit_status != R2_OK) goto cleanup; diff --git a/src/rifiuti.c b/src/rifiuti.c index 4f27730..64037ec 100644 --- a/src/rifiuti.c +++ b/src/rifiuti.c @@ -348,7 +348,7 @@ main (int argc, g_option_context_set_summary (context, N_( "Parse INFO2 file and dump recycle bin data.")); rifiuti_setup_opt_ctx (&context, RECYCLE_BIN_TYPE_FILE); - exit_status = rifiuti_parse_opt_ctx (&context, &argc, &argv); + exit_status = rifiuti_parse_opt_ctx (&context, &argv, &error); if (exit_status != R2_OK) goto cleanup; diff --git a/src/utils.c b/src/utils.c index d44cd63..f1df806 100644 --- a/src/utils.c +++ b/src/utils.c @@ -780,50 +780,53 @@ rifiuti_setup_opt_ctx (GOptionContext **context, g_option_context_set_help_enabled (*context, TRUE); } +/** + * @brief Process command line arguments + * @param context option context pointer + * @param argv reference to command line `argv` + * @param error reference of `GError` pointer to store errors + * @return `R2_OK` upon parse success, `R2_ERR_ARG` on failure. + * `error` is set upon error as well. + */ r2status rifiuti_parse_opt_ctx (GOptionContext **context, - int *argc, - char ***argv) + char ***argv, + GError **error) { - GError *err = NULL; - gboolean parse_ok; + int argc; + char **args; + GArray *arg_array; // TODO GStrvBuilder since 2.68 + + argc = g_strv_length (*argv); - /* Must be done before parsing, since argc might be modified later */ - if (*argc <= 1) { #ifdef G_OS_WIN32 - g_set_print_handler (gui_message); + args = g_win32_get_command_line (); +#else + args = g_strdupv (*argv); #endif - char *help_msg = g_option_context_get_help ( - *context, FALSE, NULL); - g_print ("%s", help_msg); - g_free (help_msg); - g_option_context_free (*context); - return (R2_ERR_GUI_HELP); + arg_array = g_array_new (TRUE, TRUE, sizeof(gpointer)); + g_array_append_vals (arg_array, args, argc); + if (argc == 1) { + char *help_opt = g_strdup ("--help-all"); + g_array_append_val (arg_array, help_opt); +#ifdef G_OS_WIN32 + g_set_print_handler (gui_message); +#endif } { -#ifdef G_OS_WIN32 - char **args = g_win32_get_command_line (); -#else - char **args = g_strdupv (*argv); -#endif - char *args_str = g_strjoinv("|", args); + char *args_str = g_strjoinv("|", (char **) arg_array->data); g_debug("Calling args: %s", args_str); g_free(args_str); - - parse_ok = g_option_context_parse_strv (*context, &args, &err); - g_option_context_free (*context); - g_strfreev (args); } - if (parse_ok) - return R2_OK; + g_option_context_parse_strv (*context, + (char ***) &(arg_array->data), error); + g_option_context_free (*context); + g_array_unref (arg_array); - g_printerr (_("Error parsing options: %s"), err->message); - g_printerr ("\n"); - g_clear_error (&err); - return R2_ERR_ARG; + return (*error != NULL) ? R2_ERR_ARG : R2_OK; } diff --git a/src/utils.h b/src/utils.h index 37aa8fb..78de4d5 100644 --- a/src/utils.h +++ b/src/utils.h @@ -161,8 +161,8 @@ void rifiuti_setup_opt_ctx (GOptionContext **context, rbin_type type); r2status rifiuti_parse_opt_ctx (GOptionContext **context, - int *argc, - char ***argv); + char ***argv, + GError **error); GDateTime * win_filetime_to_gdatetime (int64_t win_filetime);