Command line parser now also collects loose arguments like typically used to specify file names or commands in a console application; marginally improved documentation in various places

git-svn-id: file:///srv/devel/repo-conversion/nusu@51 d2e56fa2-650e-0410-a79f-9358c0239efd
This commit is contained in:
Markus Ewald 2007-09-25 19:37:45 +00:00
parent 93092637cf
commit 932fdcb6f7
4 changed files with 52 additions and 14 deletions

View File

@ -15,19 +15,16 @@ namespace Nuclex.Support.Parsing {
/// <summary>Validates that normal arguments can be parsed</summary>
[Test]
public void TestPlainArguments() {
Assert.AreEqual(
true.ToString(),
new CommandLineParser(new string[] { "-hello" })["hello"],
Assert.IsTrue(
new CommandLineParser(new string[] { "-hello" }).HasArgument("hello"),
"Argument with minus sign is recognized"
);
Assert.AreEqual(
true.ToString(),
new CommandLineParser(new string[] { "--hello" })["hello"],
Assert.IsTrue(
new CommandLineParser(new string[] { "--hello" }).HasArgument("hello"),
"Argument with double minus sign is recognized"
);
Assert.AreEqual(
true.ToString(),
new CommandLineParser(new string[] { "/hello" })["hello"],
Assert.IsTrue(
new CommandLineParser(new string[] { "/hello" }).HasArgument("hello"),
"Argument with slash is recognized"
);
}
@ -52,6 +49,21 @@ namespace Nuclex.Support.Parsing {
);
}
/// <summary>
/// Validates that loosely specified values are recognized by the parser
/// </summary>
[Test]
public void TestLooseValues() {
Assert.IsTrue(
new CommandLineParser(new string[] { "hello" }).Values.Contains("hello"),
"Plain loose value is recognized"
);
Assert.IsTrue(
new CommandLineParser(new string[] { "-hello:world", "foo" }).Values.Contains("foo"),
"Loose value following an assignment is recognized"
);
}
}
} // namespace Nuclex.Support.Parsing

View File

@ -58,6 +58,7 @@ namespace Nuclex.Support.Parsing {
/// <param name="arguments">Arguments that have been passed in the command line</param>
public CommandLineParser(string[] arguments) {
this.arguments = new StringDictionary();
this.values = new StringCollection();
string activeParameter = null;
@ -76,6 +77,8 @@ namespace Nuclex.Support.Parsing {
this.arguments.Add(activeParameter, parts[0]);
}
activeParameter = null;
} else {
this.values.Add(parts[0]);
}
// Error: No argument is waiting for a value. Skip this argument.
@ -89,7 +92,7 @@ namespace Nuclex.Support.Parsing {
// it up before switching to the argument we just found.
if(activeParameter != null)
if(!this.arguments.ContainsKey(activeParameter))
this.arguments.Add(activeParameter, true.ToString());
this.arguments.Add(activeParameter, null);
// Remember argument to allow for a later value assignment
activeParameter = parts[1];
@ -104,7 +107,7 @@ namespace Nuclex.Support.Parsing {
// it up before switching to the argument we just found.
if(activeParameter != null)
if(!this.arguments.ContainsKey(activeParameter))
this.arguments.Add(activeParameter, true.ToString());
this.arguments.Add(activeParameter, null);
activeParameter = parts[1];
@ -125,7 +128,7 @@ namespace Nuclex.Support.Parsing {
// it up before leaving the parsing method.
if(activeParameter != null) {
if(!this.arguments.ContainsKey(activeParameter)) {
this.arguments.Add(activeParameter, true.ToString());
this.arguments.Add(activeParameter, null);
}
}
}
@ -137,6 +140,23 @@ namespace Nuclex.Support.Parsing {
get { return this.arguments[argumentName]; }
}
/// <summary>
/// Checks whether the specified argument was specified on the command line
/// </summary>
/// <param name="argumentName">Name of the argument to check</param>
/// <returns>True if the specified command was given on the command line</returns>
public bool HasArgument(string argumentName) {
return this.arguments.ContainsKey(argumentName);
}
/// <summary>
/// Any values loosely specified on the command line without being assigned
/// to an argument.
/// </summary>
public StringCollection Values {
get { return this.values; }
}
/// <summary>
/// Regular Expression used to split the arguments and their assigned values
/// </summary>
@ -151,6 +171,10 @@ namespace Nuclex.Support.Parsing {
/// <summary>Stores the parsed arguments</summary>
private StringDictionary arguments;
/// <summary>
/// Stores any values passed on the command line without assigning an argument
/// </summary>
private StringCollection values;
}

View File

@ -30,7 +30,9 @@ namespace Nuclex.Support {
break;
}
// If the paths don't share a common root, we have to use an absolute path
// If the paths don't share a common root, we have to use an absolute path.
// Should the absolutePath parameter actually be a relative path, this will
// also trigger the return of the absolutePath as-is.
if(lastCommonRoot == -1)
return absolutePath;

View File

@ -86,7 +86,7 @@ namespace Nuclex.Support.Tracking {
//
// We can *not* optimize this lock away since we absolutely must not create
// two doneEvents -- someone might call .WaitOne() on the first one when only
// the second one is references by this.doneEvent and thus gets set in the end.
// the second one is referenced by this.doneEvent and thus gets set in the end.
if(this.doneEvent == null) {
lock(this) {