commit ee578dbb494ca89512556cc50efcc78dc60f910c Author: Jason Klein trac.torproject.org@my.jrklein.com Date: Wed Apr 20 16:55:29 2011 -0500
Vidalia: Improved Parameter Handling (#2965)
Vidalia ignores parameters and values that are separated by an equal sign. (ie: Vidalia --datadir=data --logfile=log)
Attached patch updates parseArguments() to support keys and values separated by equal sign.
Vidalia does not reject parameters that should include a value. (ie: Vidalia --datadir --logfile)
Attached patch calls argNeedsValue() from validateArguments() to determine if value is required. Vidalia notifies user if required parameter value is missing.
NOTE: validateArguments() was static, so could not call argNeedsValue(). No longer static. If function needs to be static, could pass an instance of Vidalia as a function parameter instead. --- src/vidalia/Vidalia.cpp | 16 ++++++++++++++++ src/vidalia/Vidalia.h | 2 +- 2 files changed, 17 insertions(+), 1 deletions(-)
diff --git a/src/vidalia/Vidalia.cpp b/src/vidalia/Vidalia.cpp index c763fac..53b41c9 100644 --- a/src/vidalia/Vidalia.cpp +++ b/src/vidalia/Vidalia.cpp @@ -252,6 +252,12 @@ Vidalia::parseArguments(QStringList args) if (arg.startsWith("-")) { arg = arg.mid((arg.startsWith("--") ? 2 : 1)); } + /* Argument names do not include equal sign. Assume value follows. */ + if (arg.indexOf("=") > -1) { + value = arg.right(arg.length() - (arg.indexOf("=")+1)); + arg = arg.left(arg.indexOf("=")); + } + else /* Check if it takes a value and there is one on the command-line */ if (i < args.size()-1 && argNeedsValue(arg)) { value = args.at(++i); @@ -265,6 +271,16 @@ Vidalia::parseArguments(QStringList args) bool Vidalia::validateArguments(QString &errmsg) { + /* Check for missing parameter values */ + QMapIterator<QString, QString> _i(_args); + QString tmp; + while(_i.hasNext()) { + _i.next(); + if(argNeedsValue(_i.key()) && (_i.value() == "")) { + errmsg = tr("Value required for parameter :") + _i.key(); + return false; + } + } /* Check for a language that Vidalia recognizes. */ if (_args.contains(ARG_LANGUAGE) && !LanguageSupport::isValidLanguageCode(_args.value(ARG_LANGUAGE))) { diff --git a/src/vidalia/Vidalia.h b/src/vidalia/Vidalia.h index 421ae68..b7d09f4 100644 --- a/src/vidalia/Vidalia.h +++ b/src/vidalia/Vidalia.h @@ -54,7 +54,7 @@ public: ~Vidalia();
/** Validates that all arguments were well-formed. */ - static bool validateArguments(QString &errmsg); + bool validateArguments(QString &errmsg); /** Displays usage information for command-line args. */ static void showUsageMessageBox(); /** Returns true if the user wants to see usage information. */