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:
parent
93092637cf
commit
932fdcb6f7
|
@ -15,19 +15,16 @@ namespace Nuclex.Support.Parsing {
|
||||||
/// <summary>Validates that normal arguments can be parsed</summary>
|
/// <summary>Validates that normal arguments can be parsed</summary>
|
||||||
[Test]
|
[Test]
|
||||||
public void TestPlainArguments() {
|
public void TestPlainArguments() {
|
||||||
Assert.AreEqual(
|
Assert.IsTrue(
|
||||||
true.ToString(),
|
new CommandLineParser(new string[] { "-hello" }).HasArgument("hello"),
|
||||||
new CommandLineParser(new string[] { "-hello" })["hello"],
|
|
||||||
"Argument with minus sign is recognized"
|
"Argument with minus sign is recognized"
|
||||||
);
|
);
|
||||||
Assert.AreEqual(
|
Assert.IsTrue(
|
||||||
true.ToString(),
|
new CommandLineParser(new string[] { "--hello" }).HasArgument("hello"),
|
||||||
new CommandLineParser(new string[] { "--hello" })["hello"],
|
|
||||||
"Argument with double minus sign is recognized"
|
"Argument with double minus sign is recognized"
|
||||||
);
|
);
|
||||||
Assert.AreEqual(
|
Assert.IsTrue(
|
||||||
true.ToString(),
|
new CommandLineParser(new string[] { "/hello" }).HasArgument("hello"),
|
||||||
new CommandLineParser(new string[] { "/hello" })["hello"],
|
|
||||||
"Argument with slash is recognized"
|
"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
|
} // namespace Nuclex.Support.Parsing
|
||||||
|
|
|
@ -58,6 +58,7 @@ namespace Nuclex.Support.Parsing {
|
||||||
/// <param name="arguments">Arguments that have been passed in the command line</param>
|
/// <param name="arguments">Arguments that have been passed in the command line</param>
|
||||||
public CommandLineParser(string[] arguments) {
|
public CommandLineParser(string[] arguments) {
|
||||||
this.arguments = new StringDictionary();
|
this.arguments = new StringDictionary();
|
||||||
|
this.values = new StringCollection();
|
||||||
|
|
||||||
string activeParameter = null;
|
string activeParameter = null;
|
||||||
|
|
||||||
|
@ -76,6 +77,8 @@ namespace Nuclex.Support.Parsing {
|
||||||
this.arguments.Add(activeParameter, parts[0]);
|
this.arguments.Add(activeParameter, parts[0]);
|
||||||
}
|
}
|
||||||
activeParameter = null;
|
activeParameter = null;
|
||||||
|
} else {
|
||||||
|
this.values.Add(parts[0]);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Error: No argument is waiting for a value. Skip this argument.
|
// 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.
|
// it up before switching to the argument we just found.
|
||||||
if(activeParameter != null)
|
if(activeParameter != null)
|
||||||
if(!this.arguments.ContainsKey(activeParameter))
|
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
|
// Remember argument to allow for a later value assignment
|
||||||
activeParameter = parts[1];
|
activeParameter = parts[1];
|
||||||
|
@ -104,7 +107,7 @@ namespace Nuclex.Support.Parsing {
|
||||||
// it up before switching to the argument we just found.
|
// it up before switching to the argument we just found.
|
||||||
if(activeParameter != null)
|
if(activeParameter != null)
|
||||||
if(!this.arguments.ContainsKey(activeParameter))
|
if(!this.arguments.ContainsKey(activeParameter))
|
||||||
this.arguments.Add(activeParameter, true.ToString());
|
this.arguments.Add(activeParameter, null);
|
||||||
|
|
||||||
activeParameter = parts[1];
|
activeParameter = parts[1];
|
||||||
|
|
||||||
|
@ -125,7 +128,7 @@ namespace Nuclex.Support.Parsing {
|
||||||
// it up before leaving the parsing method.
|
// it up before leaving the parsing method.
|
||||||
if(activeParameter != null) {
|
if(activeParameter != null) {
|
||||||
if(!this.arguments.ContainsKey(activeParameter)) {
|
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]; }
|
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>
|
/// <summary>
|
||||||
/// Regular Expression used to split the arguments and their assigned values
|
/// Regular Expression used to split the arguments and their assigned values
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
@ -151,6 +171,10 @@ namespace Nuclex.Support.Parsing {
|
||||||
|
|
||||||
/// <summary>Stores the parsed arguments</summary>
|
/// <summary>Stores the parsed arguments</summary>
|
||||||
private StringDictionary arguments;
|
private StringDictionary arguments;
|
||||||
|
/// <summary>
|
||||||
|
/// Stores any values passed on the command line without assigning an argument
|
||||||
|
/// </summary>
|
||||||
|
private StringCollection values;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -30,7 +30,9 @@ namespace Nuclex.Support {
|
||||||
break;
|
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)
|
if(lastCommonRoot == -1)
|
||||||
return absolutePath;
|
return absolutePath;
|
||||||
|
|
||||||
|
|
|
@ -86,7 +86,7 @@ namespace Nuclex.Support.Tracking {
|
||||||
//
|
//
|
||||||
// We can *not* optimize this lock away since we absolutely must not create
|
// 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
|
// 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) {
|
if(this.doneEvent == null) {
|
||||||
|
|
||||||
lock(this) {
|
lock(this) {
|
||||||
|
|
Loading…
Reference in New Issue
Block a user