diff --git a/Nuclex.Support.csproj b/Nuclex.Support.csproj index 57a2698..3259424 100644 --- a/Nuclex.Support.csproj +++ b/Nuclex.Support.csproj @@ -114,11 +114,23 @@ LicenseKey.cs - - - CommandLineParser.cs + + CommandLine.cs + + + CommandLine.cs + + + CommandLine.cs + + + + BrokenCommandLineParser.cs + + CommandLine.cs + PathHelper.cs diff --git a/Source/Parsing/CommandLineParser.Test.cs b/Source/Parsing/BrokenCommandLineParser.Test.cs similarity index 95% rename from Source/Parsing/CommandLineParser.Test.cs rename to Source/Parsing/BrokenCommandLineParser.Test.cs index 14b78b1..be9f45d 100644 --- a/Source/Parsing/CommandLineParser.Test.cs +++ b/Source/Parsing/BrokenCommandLineParser.Test.cs @@ -26,6 +26,8 @@ using System.Text; using NUnit.Framework; +#if false // Too bugged. 100% test coverage not possible. + namespace Nuclex.Support.Parsing { /// Ensures that the command line parser is working properly @@ -130,4 +132,6 @@ namespace Nuclex.Support.Parsing { } // namespace Nuclex.Support.Parsing +#endif + #endif // UNITTEST \ No newline at end of file diff --git a/Source/Parsing/CommandLineParser.cs b/Source/Parsing/BrokenCommandLineParser.cs similarity index 96% rename from Source/Parsing/CommandLineParser.cs rename to Source/Parsing/BrokenCommandLineParser.cs index 51e8c9c..f7e8d03 100644 --- a/Source/Parsing/CommandLineParser.cs +++ b/Source/Parsing/BrokenCommandLineParser.cs @@ -21,6 +21,8 @@ License along with this library using System.Collections.Specialized; using System.Text.RegularExpressions; +#if false // Too bugged. 100% test coverage not possible. + namespace Nuclex.Support.Parsing { /// Parses an application's command line @@ -179,3 +181,5 @@ namespace Nuclex.Support.Parsing { } } // namespace Nuclex.Support.Parsing + +#endif diff --git a/Source/Parsing/CommandLine.Formatter.cs b/Source/Parsing/CommandLine.Formatter.cs new file mode 100644 index 0000000..f4cf1e6 --- /dev/null +++ b/Source/Parsing/CommandLine.Formatter.cs @@ -0,0 +1,32 @@ +#region CPL License +/* +Nuclex Framework +Copyright (C) 2002-2008 Nuclex Development Labs + +This library is free software; you can redistribute it and/or +modify it under the terms of the IBM Common Public License as +published by the IBM Corporation; either version 1.0 of the +License, or (at your option) any later version. + +This library is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +IBM Common Public License for more details. + +You should have received a copy of the IBM Common Public +License along with this library +*/ +#endregion + +using System; +using System.Collections.Generic; + +namespace Nuclex.Support.Parsing { + + partial class CommandLine { + + internal static class Formatter { } + + } + +} // namespace Nuclex.Support.Parsing diff --git a/Source/Parsing/CommandLine.Option.cs b/Source/Parsing/CommandLine.Option.cs new file mode 100644 index 0000000..dc4895f --- /dev/null +++ b/Source/Parsing/CommandLine.Option.cs @@ -0,0 +1,49 @@ +#region CPL License +/* +Nuclex Framework +Copyright (C) 2002-2008 Nuclex Development Labs + +This library is free software; you can redistribute it and/or +modify it under the terms of the IBM Common Public License as +published by the IBM Corporation; either version 1.0 of the +License, or (at your option) any later version. + +This library is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +IBM Common Public License for more details. + +You should have received a copy of the IBM Common Public +License along with this library +*/ +#endregion + +using System; +using System.Collections.Generic; + +namespace Nuclex.Support.Parsing { + + partial class CommandLine { + + /// Option that can be specified on an application's command line + public struct Option { + + /// Contains the raw string the command line argument was parsed from + public string Raw; // TODO: ToString() instead +/* + /// Method used to specify the argument (either '-', '--' or '/') + public string Method; +*/ + /// Name of the command line argument + public string Name; + /// Value that has been assigned to the command line argument + public string Value; +/* + /// Method used to assign the value (either '=', ':' or ' ') + public string Assignment; +*/ + } + + } + +} // namespace Nuclex.Support.Parsing diff --git a/Source/Parsing/CommandLine.Parser.cs b/Source/Parsing/CommandLine.Parser.cs new file mode 100644 index 0000000..55f07a9 --- /dev/null +++ b/Source/Parsing/CommandLine.Parser.cs @@ -0,0 +1,95 @@ +#region CPL License +/* +Nuclex Framework +Copyright (C) 2002-2008 Nuclex Development Labs + +This library is free software; you can redistribute it and/or +modify it under the terms of the IBM Common Public License as +published by the IBM Corporation; either version 1.0 of the +License, or (at your option) any later version. + +This library is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +IBM Common Public License for more details. + +You should have received a copy of the IBM Common Public +License along with this library +*/ +#endregion + +using System; +using System.Collections.Generic; + +namespace Nuclex.Support.Parsing { + + partial class CommandLine { + + /// Parses command line strings + private static class Parser { + + /// Parses a string containing command line arguments + /// String that will be parsed + /// The parsed command line arguments from the string + public static CommandLine Parse(string commandLineString) { + CommandLine commandLine = new CommandLine(); + if(commandLineString == null) { + return commandLine; + } + +/* + for(int index = 0; index < commandLineString.Length; ) { + char currentCharacter = commandLineString[index]; + + // We ignore whitespaces outside of quoted values + if(char.IsWhiteSpace(currentCharacter)) { + continue; + } + + switch(currentCharacter) { + case '-': + case '/': { + parseArgument(commandLine, commandLineString, ref index); + break; + } + case '"': { + parseQuotedValue(commandLine, commandLineString, ref index); + break; + } + default: { + parseUnquotedValue(commandLine, commandLineString, ref index); + break; + } + } + } +*/ + return null; + } + +/* + private static void parseArgument( + CommandLine commandLine, string commandLineString, ref int index + ) { + } + + private static void parseQuotedValue( + CommandLine commandLine, string commandLineString, ref int index + ) { + } + + private static void parseUnquotedValue( + CommandLine commandLine, string commandLineString, ref int index + ) { + int endIndex = commandLineString.IndexOfAny(WhitespaceCharacters, index); + + StringSegment argument = new StringSegment(commandLineString, index, endIndex - index); + + } + + private static readonly char[] WhitespaceCharacters = new char[] { ' ', '\t' }; +*/ + } + + } + +} // namespace Nuclex.Support.Parsing diff --git a/Source/Parsing/CommandLine.Test.cs b/Source/Parsing/CommandLine.Test.cs new file mode 100644 index 0000000..47222ca --- /dev/null +++ b/Source/Parsing/CommandLine.Test.cs @@ -0,0 +1,51 @@ +#region CPL License +/* +Nuclex Framework +Copyright (C) 2002-2008 Nuclex Development Labs + +This library is free software; you can redistribute it and/or +modify it under the terms of the IBM Common Public License as +published by the IBM Corporation; either version 1.0 of the +License, or (at your option) any later version. + +This library is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +IBM Common Public License for more details. + +You should have received a copy of the IBM Common Public +License along with this library +*/ +#endregion + +using System; +using System.Collections.Generic; +using System.Text; + +#if UNITTEST + +using NUnit.Framework; + +namespace Nuclex.Support.Parsing { + + /// Ensures that the command line parser is working properly + [TestFixture] + public class CommandLineTest { + + /// Validates that normal arguments can be parsed + [Test] + public void TestParseOptions() { + CommandLine.Parse("Hello -World /This --Is \"a test\""); + } + + /// Validates that null can be parsed + [Test] + public void TestParseNull() { + Assert.IsNotNull(CommandLine.Parse(null)); + } + + } + +} // namespace Nuclex.Support.Parsing + +#endif // UNITTEST \ No newline at end of file diff --git a/Source/Parsing/CommandLine.cs b/Source/Parsing/CommandLine.cs index 76df0d7..c8d6497 100644 --- a/Source/Parsing/CommandLine.cs +++ b/Source/Parsing/CommandLine.cs @@ -1,46 +1,89 @@ -using System; +#region CPL License +/* +Nuclex Framework +Copyright (C) 2002-2008 Nuclex Development Labs + +This library is free software; you can redistribute it and/or +modify it under the terms of the IBM Common Public License as +published by the IBM Corporation; either version 1.0 of the +License, or (at your option) any later version. + +This library is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +IBM Common Public License for more details. + +You should have received a copy of the IBM Common Public +License along with this library +*/ +#endregion + +using System; using System.Collections.Generic; using System.Text; -namespace Nuclex.Support.Source.Parsing { +namespace Nuclex.Support.Parsing { -#if false /// - /// Parses an application's command line parameters for easier consumption + /// Manages an application's command line parameters /// /// /// - /// At the time of the creation of this parser, there are already several command line - /// parsing libraries out there. Most of them, however, do way too much at once or at - /// the very least use one huge, untested clutter of classes and methods to arrive - /// at their results. + /// At the time of the creation of this component, there are already several command + /// line parsing libraries out there. Most of them, however, do way too much at once + /// or at the very least rely on huge, untested clutters of classes and methods to + /// arrive at their results. /// /// - /// This parser does nothing more than parse the command line arguments. It doesn't - /// interpret them and it doesn't check them for validity. Due to this, it can easily + /// This class does nothing more than represent the command line to the application. + /// It can parse a command line + /// parse the command line arguments. It doesn't + /// interpret them and it doesn't check them for validity. This promotes simplicity + /// and allows t /// be unit-tested and is an ideal building block to create actual command line /// interpreters that connect the parameters to program instructions and or fill /// structures in code. /// + /// + /// Terminology + /// + /// + /// Command line + /// + /// The entire command line either as a string or as + /// an already parsed data structure + /// + /// + /// + /// Option / Argument + /// + /// Can be specified on the command line and typically alters the behavior + /// of the application or changes a setting. For example, '--normalize' or + /// '/safemode'. + /// + /// + /// + /// Value + /// + /// Can either sit loosely in the command line (eg. 'update' or 'textfile.txt') + /// or as assignment to an option (eg. '--width=1280' or '/overwrite:always') + /// + /// + /// + /// /// - public class CommandLine { - public static CommandLine Parse(string commandLine) {} - } - - public struct CommandLineOption { - - /// Contains the raw string the command line argument was parsed from - public string Raw; - /// Method used to specify the argument (either '-', '--' or '/') - public string Method; - /// Name of the command line argument - public string Name; - /// Value that has been assigned to the command line argument - public string Value; - /// Method used to assign the value (either '=', ':' or ' ') - public string Assignment; + public partial class CommandLine { + + /// Initializes a new command line + public CommandLine() { } + + /// Parses the command line arguments from the provided string + /// String containing the command line arguments + /// The parsed command line + public static CommandLine Parse(string commandLineString) { + return Parser.Parse(commandLineString); + } } -#endif -} +} // namespace Nuclex.Support.Parsing diff --git a/Source/Plugins/PluginHost.Test.cs b/Source/Plugins/PluginHost.Test.cs index 63e1fa7..da7940f 100644 --- a/Source/Plugins/PluginHost.Test.cs +++ b/Source/Plugins/PluginHost.Test.cs @@ -78,6 +78,7 @@ namespace Nuclex.Support.Plugins { PluginHost testHost = new PluginHost(testEmployer, testRepository); + Assert.AreSame(testEmployer, testHost.Employer); Assert.AreEqual(1, testEmployer.Factories.Count); } @@ -121,6 +122,7 @@ namespace Nuclex.Support.Plugins { Assembly self = Assembly.GetAssembly(GetType()); testRepository.AddAssembly(self); + Assert.AreSame(testEmployer, testHost.Employer); Assert.AreEqual(1, testEmployer.Factories.Count); } @@ -137,6 +139,8 @@ namespace Nuclex.Support.Plugins { // the unit testing tool Assembly self = Assembly.GetAssembly(GetType()); testRepository.AddAssembly(self); + + Assert.AreSame(testRepository, testHost.Repository); } /// @@ -154,6 +158,7 @@ namespace Nuclex.Support.Plugins { Assembly self = Assembly.GetAssembly(GetType()); testRepository.AddAssembly(self); + Assert.AreSame(testRepository, testHost.Repository); Assert.AreEqual(0, testEmployer.Factories.Count); } diff --git a/Source/StringSegment.Test.cs b/Source/StringSegment.Test.cs index 57964d2..8020da1 100644 --- a/Source/StringSegment.Test.cs +++ b/Source/StringSegment.Test.cs @@ -188,6 +188,41 @@ namespace Nuclex.Support { Assert.IsFalse(helloWorld1Segment != helloWorld2Segment); } + /// Tests the ToString() method of the string segment + [Test] + public void TestToString() { + StringSegment helloWorldSegment = new StringSegment("hello world", 4, 3); + + Assert.AreEqual("o w", helloWorldSegment.ToString()); + } + + /// + /// Tests the ToString() method of the string segment with an invalid string + /// + [Test, ExpectedException(typeof(ArgumentNullException))] + public void TestToStringWithInvalidString() { + StringSegment helloWorldSegment = new StringSegment(null, 4, 3); + Assert.IsNotNull(helloWorldSegment.ToString()); + } + + /// + /// Tests the ToString() method of the string segment with an invalid offset + /// + [Test, ExpectedException(typeof(ArgumentOutOfRangeException))] + public void TestToStringWithInvalidOffset() { + StringSegment helloWorldSegment = new StringSegment("hello world", -4, 3); + Assert.IsNotNull(helloWorldSegment.ToString()); + } + + /// + /// Tests the ToString() method of the string segment with an invalid count + /// + [Test, ExpectedException(typeof(ArgumentOutOfRangeException))] + public void TestToStringWithInvalidCount() { + StringSegment helloWorldSegment = new StringSegment("hello world", 4, -3); + Assert.IsNotNull(helloWorldSegment.ToString()); + } + } } // namespace Nuclex.Support diff --git a/Source/StringSegment.cs b/Source/StringSegment.cs index 4d4fc54..ff1d109 100644 --- a/Source/StringSegment.cs +++ b/Source/StringSegment.cs @@ -201,6 +201,12 @@ namespace Nuclex.Support { return !(left == right); } + /// Returns a string representation of the string segment + /// The string representation of the string segment + public override string ToString() { + return this.text.Substring(this.offset, this.count); + } + /// String wrapped by the string segment private string text; /// Offset in the original string the segment begins at