From 7b13cdf7b39b8695d77a5b14be202edfc79a72d9 Mon Sep 17 00:00:00 2001 From: Alexander Shaduri Date: Sun, 19 Feb 2012 15:04:47 +0000 Subject: [PATCH] GSmartControl now prints an error message and a help text if an invalid command-line options was specified. --- gsmartcontrol/data/gsmartcontrol-root.in | 57 ++++++++++++++++++------ gsmartcontrol/src/gsc_init.cpp | 27 +++++++++-- 2 files changed, 66 insertions(+), 18 deletions(-) diff --git a/gsmartcontrol/data/gsmartcontrol-root.in b/gsmartcontrol/data/gsmartcontrol-root.in index 7d50b29..b1940dd 100644 --- a/gsmartcontrol/data/gsmartcontrol-root.in +++ b/gsmartcontrol/data/gsmartcontrol-root.in @@ -11,18 +11,43 @@ EXEC_BIN="@prefix@/bin/gsmartcontrol"; prog_name="gsmartcontrol" -DESKTOP="$1"; -# This works for --help too -if [ "$DESKTOP" = "" ]; then - DESKTOP="auto"; # default +# Preserve quotes in arguments +final_args_quoted=""; +for i in "$@";do + final_args_quoted="$final_args_quoted \"${i//\"/\\\"}\""; +done; -elif [ "$DESKTOP" != "auto" ] && [ "$DESKTOP" != "kde" ] && \ + +DESKTOP="auto"; + +# Compatibility with old syntax: +# gsmartcontrol-root [ [program_options]] +if [ "$1" == "auto" ] || [ "$1" == "kde" ] || [ "$1" == "gnome" ] || [ "$1" == "other" ]; then + DESKTOP="$1"; + shift; # remove $1 +else + # New syntax: + # gsmartcontrol-root [--desktop=] [program_options] + + for arg in $*; do + case $arg in + --desktop=*) + DESKTOP="${arg#*=}"; + final_args_quoted="${final_args_quoted/\"$arg\"/}"; + ;; + *) + # unknown option + ;; + esac + done +fi + +if [ "$DESKTOP" != "auto" ] && [ "$DESKTOP" != "kde" ] && \ [ "$DESKTOP" != "gnome" ] && [ "$DESKTOP" != "other" ]; then - echo "Usage: $0 [ [<${prog_name}_options>] ]"; + echo "Usage: $0 [--desktop=] [<${prog_name}_options>]"; exit 1; fi -shift; # remove $1 # Auto-detect current desktop if auto was specified. @@ -42,6 +67,9 @@ fi # echo $DESKTOP; + + + # They're basically the same, only the order is different. # sux requires xterm to ask for the password. # xdg-su is basically like this script, except worse :) @@ -102,29 +130,30 @@ export PATH="$EXTRA_PATHS:$PATH" # su-to-root -X -c 'gsmartcontrol --no-scan' # xterm -e sux -c 'gsmartcontrol --no-scan' # sux asks for password in a terminal + full_cmd=""; if [ "$GSMARTCONTROL_SU" != "" ]; then - full_cmd="$GSMARTCONTROL_SU '$EXEC_BIN $@'"; + full_cmd="$GSMARTCONTROL_SU '$EXEC_BIN $final_args_quoted'"; elif [ "$found_su" = "sux" ]; then - full_cmd="xterm -e sux -c '$EXEC_BIN $@'"; + full_cmd="xterm -e sux -c '$EXEC_BIN $final_args_quoted'"; elif [ "$found_su" = "gksu" ]; then - full_cmd="$found_su '$EXEC_BIN $@'"; + full_cmd="$found_su '$EXEC_BIN $final_args_quoted'"; elif [ "$found_su" = "beesu" ]; then - full_cmd="$found_su -P '$EXEC_BIN $@'"; + full_cmd="$found_su -P '$EXEC_BIN $final_args_quoted'"; elif [ "$found_su" = "su-to-root" ]; then - full_cmd="$found_su -X -c '$EXEC_BIN $@'"; + full_cmd="$found_su -X -c '$EXEC_BIN $final_args_quoted'"; else # gnomesu, kdesu, xdg-su - full_cmd="$found_su -c '$EXEC_BIN $@'"; + full_cmd="$found_su -c '$EXEC_BIN $final_args_quoted'"; fi -#echo $full_cmd +# echo $full_cmd eval $full_cmd diff --git a/gsmartcontrol/src/gsc_init.cpp b/gsmartcontrol/src/gsc_init.cpp index 04e9508..aac98b0 100644 --- a/gsmartcontrol/src/gsc_init.cpp +++ b/gsmartcontrol/src/gsc_init.cpp @@ -236,12 +236,31 @@ inline bool parse_cmdline_args(CmdArgs& args, int& argc, char**& argv) // libdebug options; this will also automatically apply them g_option_context_add_group(context, debug_get_option_group()); - g_option_context_parse(context, &argc, &argv, &error); - g_option_context_free(context); - if (error) + // The command-line parser stops at the first unknown option. Since this + // is kind of inconsistent, we abort altogether. + bool parsed = g_option_context_parse(context, &argc, &argv, &error); + + if (error) { + std::string error_text = "\n" + std::string("Error parsing command-line options: "); + error_text += (error->message ? error->message : "invalid error"); + error_text += "\n\n"; g_error_free(error); - return true; +#if (GLIB_CHECK_VERSION(2,14,0)) + gchar* help_text = g_option_context_get_help(context, true, NULL); + if (help_text) { + error_text += help_text; + g_free(help_text); + } +#else + error_text += "Exiting.\n"; +#endif + + std::fprintf(stderr, "%s", error_text.c_str()); + } + g_option_context_free(context); + + return parsed; }