Menu

#34 improved parsing of parameters a bit

v1.1.0
open
nobody
Argv (3)
5
2006-07-13
2006-07-13
mousio
No

Hello

For my project I get better results with these two
changes in private void Nini.Util.ArgvParser.Extract
(string[] args): changed the regex and changed the
conditions to add an argument as a parameter value.

private void Extract(string[] args)
{
parameters = new StringDictionary();
Regex splitter = new Regex ( @"^([/-]|--){1}(?
<name>\w[\w_+-]*)([:=])?(?<value>.+)?$",

RegexOptions.Compiled); //REGEX
CHANGED (IMPROVED NAME)
char[] trimChars = {'"','\''};
string parameter = null;
Match part;

// Valid parameters forms: {-,/,--}param{ ,
= ,:}((",')value(",'))
// Examples: -param1 value1 --
param2 /param3:"Test-:-work"
// /param4 = happy -param5 '-- = nice = --'
foreach(string arg in args)
{
part = splitter.Match(arg);
if (!part.Success) {
// Found a value (for the
last parameter found (space separator))
if (parameter != null &&
parameters[parameter] == "") { //CONDITION ADDED
parameters[parameter]
= arg.Trim (trimChars);
}
} else {
// Matched a name, optionally
with inline value
parameter = part.Groups
["name"].Value;
parameters.Add (parameter,

part.Groups["value"].Value.Trim (trimChars));
}
}
}

This is just a simple modification. We still parse
wrong in this case:
application -someswitch="" someargument

Discussion


Log in to post a comment.