GSmartControl now prints an error message and a help text if an invalid

command-line options was specified.
This commit is contained in:
Alexander Shaduri
2012-02-19 15:04:47 +00:00
parent c7bdaad30a
commit 7b13cdf7b3
2 changed files with 66 additions and 18 deletions
+43 -14
View File
@@ -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 [<desktop> [program_options]]
if [ "$1" == "auto" ] || [ "$1" == "kde" ] || [ "$1" == "gnome" ] || [ "$1" == "other" ]; then
DESKTOP="$1";
shift; # remove $1
else
# New syntax:
# gsmartcontrol-root [--desktop=<auto|kde|gnome|other>] [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 [<auto|kde|gnome|other> [<${prog_name}_options>] ]";
echo "Usage: $0 [--desktop=<auto|kde|gnome|other>] [<${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
+23 -4
View File
@@ -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;
}