From 932fdcb6f72b0053ca6df523288b7b6f570442f3 Mon Sep 17 00:00:00 2001 From: Markus Ewald Date: Tue, 25 Sep 2007 19:37:45 +0000 Subject: [PATCH] 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 --- Source/Parsing/CommandLineParser.Test.cs | 30 +++++++++++++++++------- Source/Parsing/CommandLineParser.cs | 30 +++++++++++++++++++++--- Source/PathHelper.cs | 4 +++- Source/Tracking/Progression.cs | 2 +- 4 files changed, 52 insertions(+), 14 deletions(-) diff --git a/Source/Parsing/CommandLineParser.Test.cs b/Source/Parsing/CommandLineParser.Test.cs index c5cb11d..a5d0cfe 100644 --- a/Source/Parsing/CommandLineParser.Test.cs +++ b/Source/Parsing/CommandLineParser.Test.cs @@ -15,19 +15,16 @@ namespace Nuclex.Support.Parsing { /// Validates that normal arguments can be parsed [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 { ); } + /// + /// Validates that loosely specified values are recognized by the parser + /// + [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 diff --git a/Source/Parsing/CommandLineParser.cs b/Source/Parsing/CommandLineParser.cs index 5d788cf..a5131d0 100644 --- a/Source/Parsing/CommandLineParser.cs +++ b/Source/Parsing/CommandLineParser.cs @@ -58,6 +58,7 @@ namespace Nuclex.Support.Parsing { /// Arguments that have been passed in the command line 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]; } } + /// + /// Checks whether the specified argument was specified on the command line + /// + /// Name of the argument to check + /// True if the specified command was given on the command line + public bool HasArgument(string argumentName) { + return this.arguments.ContainsKey(argumentName); + } + + /// + /// Any values loosely specified on the command line without being assigned + /// to an argument. + /// + public StringCollection Values { + get { return this.values; } + } + /// /// Regular Expression used to split the arguments and their assigned values /// @@ -151,6 +171,10 @@ namespace Nuclex.Support.Parsing { /// Stores the parsed arguments private StringDictionary arguments; + /// + /// Stores any values passed on the command line without assigning an argument + /// + private StringCollection values; } diff --git a/Source/PathHelper.cs b/Source/PathHelper.cs index d3912d0..9c291e3 100644 --- a/Source/PathHelper.cs +++ b/Source/PathHelper.cs @@ -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; diff --git a/Source/Tracking/Progression.cs b/Source/Tracking/Progression.cs index 526b34f..6814f91 100644 --- a/Source/Tracking/Progression.cs +++ b/Source/Tracking/Progression.cs @@ -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) {