Reactivated and fixed the command line parser; command line parser now creates a value on too many option initiators (like "//test" or "---test"); renamed CommandLine.Option to CommandLine.Argument; command line parser now uses the CommandLine.Argument class for all arguments (this enables interpreters to be build that understand spaced arguments because the order is kept intact); quoted value no longer construct an argument with a raw string that doesn't contain the quotes; achieved 100% test coverage

git-svn-id: file:///srv/devel/repo-conversion/nusu@129 d2e56fa2-650e-0410-a79f-9358c0239efd
This commit is contained in:
Markus Ewald 2009-04-14 20:25:56 +00:00
parent 6d2b39255f
commit 47b0039137
9 changed files with 465 additions and 252 deletions

View File

@ -148,7 +148,7 @@
<Compile Include="Source\Licensing\LicenseKey.Test.cs"> <Compile Include="Source\Licensing\LicenseKey.Test.cs">
<DependentUpon>LicenseKey.cs</DependentUpon> <DependentUpon>LicenseKey.cs</DependentUpon>
</Compile> </Compile>
<Compile Include="Source\Parsing\CommandLine.Option.cs"> <Compile Include="Source\Parsing\CommandLine.Argument.cs">
<DependentUpon>CommandLine.cs</DependentUpon> <DependentUpon>CommandLine.cs</DependentUpon>
</Compile> </Compile>
<Compile Include="Source\Parsing\CommandLine.Formatter.cs"> <Compile Include="Source\Parsing\CommandLine.Formatter.cs">

View File

@ -130,7 +130,7 @@
<Compile Include="Source\Licensing\LicenseKey.Test.cs"> <Compile Include="Source\Licensing\LicenseKey.Test.cs">
<DependentUpon>LicenseKey.cs</DependentUpon> <DependentUpon>LicenseKey.cs</DependentUpon>
</Compile> </Compile>
<Compile Include="Source\Parsing\CommandLine.Option.cs"> <Compile Include="Source\Parsing\CommandLine.Argument.cs">
<DependentUpon>CommandLine.cs</DependentUpon> <DependentUpon>CommandLine.cs</DependentUpon>
</Compile> </Compile>
<Compile Include="Source\Parsing\CommandLine.Formatter.cs"> <Compile Include="Source\Parsing\CommandLine.Formatter.cs">

View File

@ -26,7 +26,7 @@ using System.Text;
using NUnit.Framework; using NUnit.Framework;
#if false // Too bugged. 100% test coverage not possible. #if ENABLE_BROKEN_COMMAND_LINE_PARSER // Too bugged. 100% test coverage not possible.
namespace Nuclex.Support.Parsing { namespace Nuclex.Support.Parsing {
@ -142,6 +142,6 @@ namespace Nuclex.Support.Parsing {
} // namespace Nuclex.Support.Parsing } // namespace Nuclex.Support.Parsing
#endif #endif // ENABLE_BROKEN_COMMAND_LINE_PARSER
#endif // UNITTEST #endif // UNITTEST

View File

@ -21,7 +21,7 @@ License along with this library
using System.Collections.Specialized; using System.Collections.Specialized;
using System.Text.RegularExpressions; using System.Text.RegularExpressions;
#if false // Too bugged. 100% test coverage not possible. #if ENABLE_BROKEN_COMMAND_LINE_PARSER // Too bugged. 100% test coverage not possible.
namespace Nuclex.Support.Parsing { namespace Nuclex.Support.Parsing {
@ -182,4 +182,4 @@ namespace Nuclex.Support.Parsing {
} // namespace Nuclex.Support.Parsing } // namespace Nuclex.Support.Parsing
#endif #endif // ENABLE_BROKEN_COMMAND_LINE_PARSER

View File

@ -22,27 +22,40 @@ using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Diagnostics; using System.Diagnostics;
#if ENABLE_BROKEN_COMMAND_LINE_PARSER
namespace Nuclex.Support.Parsing { namespace Nuclex.Support.Parsing {
partial class CommandLine { partial class CommandLine {
/// <summary>Option being specified on an application's command line</summary> /// <summary>Argument being specified on an application's command line</summary>
public class Option { public class Argument {
/// <summary>Initializes a new option with only a name</summary> /// <summary>Initializes a new option with only a name</summary>
/// <param name="raw"> /// <param name="raw">
/// String segment containing the entire option as it was given on the command line /// String segment with the entire argument as it was given on the command line
/// </param> /// </param>
/// <param name="nameStart">Absolute index the option name starts at</param> /// <param name="nameStart">Absolute index the argument name starts at</param>
/// <param name="nameLength">Number of characters in the option name</param> /// <param name="nameLength">Number of characters in the option name</param>
/// <returns>The newly created option</returns> /// <returns>The newly created option</returns>
internal Option( internal static Argument OptionOnly(
StringSegment raw, StringSegment raw,
int nameStart, int nameLength int nameStart, int nameLength
) ) {
: this(raw, nameStart, nameLength, -1, -1) { } return new Argument(raw, nameStart, nameLength, -1, -1);
}
/// <summary>Initializes a new argument with only a value</summary>
/// <param name="raw">
/// String segment with the entire argument as it was given on the command line
/// </param>
/// <param name="valueStart">Absolute index the value starts at</param>
/// <param name="valueLength">Number of characters in the value</param>
/// <returns>The newly created option</returns>
internal static Argument ValueOnly(
StringSegment raw,
int valueStart, int valueLength
) {
return new Argument(raw, -1, -1, valueStart, valueLength);
}
/// <summary>Creates a new option with a name and an assigned value</summary> /// <summary>Creates a new option with a name and an assigned value</summary>
/// <param name="raw"> /// <param name="raw">
@ -53,7 +66,7 @@ namespace Nuclex.Support.Parsing {
/// <param name="valueStart">Absolute index the value starts at</param> /// <param name="valueStart">Absolute index the value starts at</param>
/// <param name="valueLength">Number of characters in the value</param> /// <param name="valueLength">Number of characters in the value</param>
/// <returns>The newly created option</returns> /// <returns>The newly created option</returns>
internal Option( internal Argument(
StringSegment raw, StringSegment raw,
int nameStart, int nameLength, int nameStart, int nameLength,
int valueStart, int valueLength int valueStart, int valueLength
@ -63,9 +76,6 @@ namespace Nuclex.Support.Parsing {
this.nameLength = nameLength; this.nameLength = nameLength;
this.valueStart = valueStart; this.valueStart = valueStart;
this.valueLength = valueLength; this.valueLength = valueLength;
Debug.Assert(this.nameStart != -1, "Name start index must not be -1");
Debug.Assert(this.nameLength != -1, "Name length must not be -1");
} }
/// <summary>Contains the raw string the command line argument was parsed from</summary> /// <summary>Contains the raw string the command line argument was parsed from</summary>
@ -76,22 +86,33 @@ namespace Nuclex.Support.Parsing {
/// <summary>Characters used to initiate this option</summary> /// <summary>Characters used to initiate this option</summary>
public string Initiator { public string Initiator {
get { get {
if(this.nameStart == -1) {
return null;
} else {
return this.raw.Text.Substring( return this.raw.Text.Substring(
this.raw.Offset, this.nameStart - this.raw.Offset this.raw.Offset, this.nameStart - this.raw.Offset
); );
} }
} }
}
/// <summary>Name of the command line option</summary> /// <summary>Name of the command line option</summary>
public string Name { public string Name {
get { get {
if(this.nameStart == -1) {
return null;
} else {
return this.raw.Text.Substring(this.nameStart, this.nameLength); return this.raw.Text.Substring(this.nameStart, this.nameLength);
} }
} }
}
/// <summary>Characters used to associate a value to this option</summary> /// <summary>Characters used to associate a value to this option</summary>
public string Associator { public string Associator {
get { get {
if(this.nameStart == -1) {
return null;
} else {
int associatorStart = this.nameStart + this.nameLength; int associatorStart = this.nameStart + this.nameLength;
if(this.valueStart == -1) { if(this.valueStart == -1) {
@ -99,11 +120,14 @@ namespace Nuclex.Support.Parsing {
if(characterCount == 0) { if(characterCount == 0) {
return null; return null;
} }
} else if(this.valueStart == associatorStart) {
return null;
} }
return this.raw.Text.Substring(associatorStart, 1); return this.raw.Text.Substring(associatorStart, 1);
} }
} }
}
/// <summary>Name of the command line option</summary> /// <summary>Name of the command line option</summary>
public string Value { public string Value {
@ -135,5 +159,3 @@ namespace Nuclex.Support.Parsing {
} }
} // namespace Nuclex.Support.Parsing } // namespace Nuclex.Support.Parsing
#endif // ENABLE_BROKEN_COMMAND_LINE_PARSER

View File

@ -21,8 +21,6 @@ License along with this library
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
#if ENABLE_BROKEN_COMMAND_LINE_PARSER
namespace Nuclex.Support.Parsing { namespace Nuclex.Support.Parsing {
partial class CommandLine { partial class CommandLine {
@ -32,5 +30,3 @@ namespace Nuclex.Support.Parsing {
} }
} // namespace Nuclex.Support.Parsing } // namespace Nuclex.Support.Parsing
#endif // ENABLE_BROKEN_COMMAND_LINE_PARSER

View File

@ -23,8 +23,6 @@ using System.Collections.Generic;
using System.Diagnostics; using System.Diagnostics;
using System.IO; using System.IO;
#if ENABLE_BROKEN_COMMAND_LINE_PARSER
namespace Nuclex.Support.Parsing { namespace Nuclex.Support.Parsing {
partial class CommandLine { partial class CommandLine {
@ -119,7 +117,7 @@ namespace Nuclex.Support.Parsing {
// Windows style argument using '/' as its initiator // Windows style argument using '/' as its initiator
case '/': { case '/': {
// The '/ character is only used to initiate argument on windows and can be // The '/ character is only used to initiate argument on windows and can be
// toggled off. The application decides, whether this is done depending on the // toggled off. The application decides whether this is done depending on the
// operating system or whether uniform behavior across platforms is desired. // operating system or whether uniform behavior across platforms is desired.
if(!this.windowsMode) { if(!this.windowsMode) {
goto default; goto default;
@ -174,14 +172,16 @@ namespace Nuclex.Support.Parsing {
// Look for the first character that ends the option. If it is not an actual option, // Look for the first character that ends the option. If it is not an actual option,
// the very first character might be the end // the very first character might be the end
index = commandLineString.IndexOfAny(OptionNameEndingCharacters, nameStartIndex); if(commandLineString[index] != commandLineString[initiatorStartIndex]) {
index = commandLineString.IndexOfAny(NameEndingCharacters, nameStartIndex);
if(index == -1) { if(index == -1) {
index = commandLineString.Length; index = commandLineString.Length;
} }
}
// If the first character of the supposed option is not valid for an option name, // If the first character of the supposed option is not valid for an option name,
// we have to consider this to be a loose value // we have to consider this to be a loose value
if(index == nameStartIndex) { if((index == nameStartIndex)/* && !isAssignmentCharacter(commandLineString[index])*/) {
index = commandLineString.IndexOfAny(WhitespaceCharacters, index); index = commandLineString.IndexOfAny(WhitespaceCharacters, index);
if(index == -1) { if(index == -1) {
index = commandLineString.Length; index = commandLineString.Length;
@ -195,9 +195,10 @@ namespace Nuclex.Support.Parsing {
return; return;
} }
parseOptionAssignment( parsePotentialOptionAssignment(
commandLineString, initiatorStartIndex, nameStartIndex, ref index commandLineString, initiatorStartIndex, nameStartIndex, ref index
); );
} }
/// <summary>Parses the value assignment in a command line option</summary> /// <summary>Parses the value assignment in a command line option</summary>
@ -209,49 +210,43 @@ namespace Nuclex.Support.Parsing {
/// Position of the first character in the option's name /// Position of the first character in the option's name
/// </param> /// </param>
/// <param name="index">Index at which the option name ended</param> /// <param name="index">Index at which the option name ended</param>
private void parseOptionAssignment( private void parsePotentialOptionAssignment(
string commandLineString, int initiatorStartIndex, int nameStartIndex, ref int index string commandLineString, int initiatorStartIndex, int nameStartIndex, ref int index
) { ) {
int nameEndIndex = index; int nameEndIndex = index;
int valueStartIndex; int valueStartIndex;
int valueEndIndex; int valueEndIndex;
if(index == commandLineString.Length) { // See if this is an assignment character. If it is, the assigned value
valueStartIndex = -1; // should follow to the right.
valueEndIndex = -1;
} else {
char currentCharacter = commandLineString[index];
bool isAssignment = bool isAssignment =
(currentCharacter == ':') || (index < commandLineString.Length) &&
(currentCharacter == '='); isAssignmentCharacter(commandLineString[index]);
// Does the string end after the suspected assignment character?
bool argumentEndReached = ((index + 1) == commandLineString.Length);
// If it's an assignment, we can proceed parsing the assigned value
if(isAssignment) { if(isAssignment) {
++index;
parseOptionValue(commandLineString, initiatorStartIndex, nameStartIndex, ref index); parseOptionValue(commandLineString, initiatorStartIndex, nameStartIndex, ref index);
return; return;
} else { } else { // No, it's an option name without an assignment
bool isModifier = bool isModifier =
(currentCharacter == '+') || (commandLineString[index - 1] == '+') ||
(currentCharacter == '-'); (commandLineString[index - 1] == '-');
if(isModifier) { if(isModifier) {
valueStartIndex = index; valueStartIndex = index - 1;
++index;
valueEndIndex = index; valueEndIndex = index;
--nameEndIndex;
} else { } else {
valueStartIndex = -1; valueStartIndex = -1;
valueEndIndex = -1; valueEndIndex = -1;
} }
} }
}
int argumentLength = index - initiatorStartIndex; int argumentLength = index - initiatorStartIndex;
this.commandLine.addOption( this.commandLine.addArgument(
new Option( new Argument(
new StringSegment(commandLineString, initiatorStartIndex, argumentLength), new StringSegment(commandLineString, initiatorStartIndex, argumentLength),
nameStartIndex, nameEndIndex - nameStartIndex, nameStartIndex, nameEndIndex - nameStartIndex,
valueStartIndex, valueEndIndex - valueStartIndex valueStartIndex, valueEndIndex - valueStartIndex
@ -271,22 +266,21 @@ namespace Nuclex.Support.Parsing {
private void parseOptionValue( private void parseOptionValue(
string commandLineString, int initiatorStartIndex, int nameStartIndex, ref int index string commandLineString, int initiatorStartIndex, int nameStartIndex, ref int index
) { ) {
int nameEndIndex = index; int nameEndIndex = index - 1;
int valueStartIndex, valueEndIndex; int valueStartIndex, valueEndIndex;
// Does the string end after the suspected assignment character? // Does the string end after the suspected assignment character?
bool argumentEndReached = ((index + 1) == commandLineString.Length); bool argumentEndReached = (index == commandLineString.Length);
if(argumentEndReached) { if(argumentEndReached) {
++index;
valueStartIndex = -1; valueStartIndex = -1;
valueEndIndex = -1; valueEndIndex = -1;
} else { } else {
char nextCharacter = commandLineString[index + 1]; char nextCharacter = commandLineString[index];
// Is this a quoted assignment // Is this a quoted assignment
if(nextCharacter == '"') { if(nextCharacter == '"') {
index += 2; ++index;
valueStartIndex = index; valueStartIndex = index;
index = commandLineString.IndexOf('"', index); index = commandLineString.IndexOf('"', index);
if(index == -1) { if(index == -1) {
@ -297,7 +291,6 @@ namespace Nuclex.Support.Parsing {
++index; ++index;
} }
} else { // Nope, assuming unquoted assignment or empty assignment } else { // Nope, assuming unquoted assignment or empty assignment
++index;
valueStartIndex = index; valueStartIndex = index;
index = commandLineString.IndexOfAny(WhitespaceCharacters, index); index = commandLineString.IndexOfAny(WhitespaceCharacters, index);
if(index == -1) { if(index == -1) {
@ -315,8 +308,8 @@ namespace Nuclex.Support.Parsing {
} }
int argumentLength = index - initiatorStartIndex; int argumentLength = index - initiatorStartIndex;
this.commandLine.addOption( this.commandLine.addArgument(
new Option( new Argument(
new StringSegment(commandLineString, initiatorStartIndex, argumentLength), new StringSegment(commandLineString, initiatorStartIndex, argumentLength),
nameStartIndex, nameEndIndex - nameStartIndex, nameStartIndex, nameEndIndex - nameStartIndex,
valueStartIndex, valueEndIndex - valueStartIndex valueStartIndex, valueEndIndex - valueStartIndex
@ -328,19 +321,26 @@ namespace Nuclex.Support.Parsing {
/// <param name="commandLineString">String the quoted value is parsed from</param> /// <param name="commandLineString">String the quoted value is parsed from</param>
/// <param name="index">Index at which the quoted value begins</param> /// <param name="index">Index at which the quoted value begins</param>
private void parseQuotedValue(string commandLineString, ref int index) { private void parseQuotedValue(string commandLineString, ref int index) {
int startIndex = index;
char quoteCharacter = commandLineString[index]; char quoteCharacter = commandLineString[index];
int startIndex = index + 1; int valueIndex = startIndex + 1;
// Search for the closing quote // Search for the closing quote
index = commandLineString.IndexOf(quoteCharacter, startIndex); index = commandLineString.IndexOf(quoteCharacter, valueIndex);
if(index == -1) { if(index == -1) {
index = commandLineString.Length; // value ends at string end index = commandLineString.Length; // value ends at string end
commandLine.addValue( commandLine.addArgument(
new StringSegment(commandLineString, startIndex, index - startIndex) Argument.ValueOnly(
new StringSegment(commandLineString, startIndex, index - startIndex),
valueIndex, index - valueIndex
)
); );
} else { // A closing quote was found } else { // A closing quote was found
commandLine.addValue( commandLine.addArgument(
new StringSegment(commandLineString, startIndex, index - startIndex) Argument.ValueOnly(
new StringSegment(commandLineString, startIndex, index - startIndex + 1),
valueIndex, index - valueIndex
)
); );
++index; // Skip the closing quote ++index; // Skip the closing quote
} }
@ -362,9 +362,22 @@ namespace Nuclex.Support.Parsing {
); );
} }
/// <summary>
/// Determines whether the specified character indicates an assignment
/// </summary>
/// <param name="character">
/// Character that will be checked for being an assignemnt
/// </param>
/// <returns>
/// True if the specified character indicated an assignment, otherwise false
/// </returns>
private static bool isAssignmentCharacter(char character) {
return (character == ':') || (character == '=');
}
/// <summary>Characters which end an option name when they are encountered</summary> /// <summary>Characters which end an option name when they are encountered</summary>
private static readonly char[] OptionNameEndingCharacters = new char[] { private static readonly char[] NameEndingCharacters = new char[] {
' ', '\t', '=', ':', '/', '-', '+', '"' ' ', '\t', '=', ':', '"'
}; };
/// <summary>Characters the parser considers to be whitespace</summary> /// <summary>Characters the parser considers to be whitespace</summary>
@ -380,5 +393,3 @@ namespace Nuclex.Support.Parsing {
} }
} // namespace Nuclex.Support.Parsing } // namespace Nuclex.Support.Parsing
#endif // ENABLE_BROKEN_COMMAND_LINE_PARSER

View File

@ -26,118 +26,117 @@ using System.Text;
using NUnit.Framework; using NUnit.Framework;
#if ENABLE_BROKEN_COMMAND_LINE_PARSER
namespace Nuclex.Support.Parsing { namespace Nuclex.Support.Parsing {
/// <summary>Ensures that the command line parser is working properly</summary> /// <summary>Ensures that the command line parser is working properly</summary>
[TestFixture] [TestFixture]
public class CommandLineTest { public class CommandLineTest {
#region class OptionTest #region class ArgumentTest
/// <summary>Unit test for the command line option class</summary> /// <summary>Unit test for the command line option class</summary>
[TestFixture] [TestFixture]
public class OptionTest { public class ArgumentTest {
/// <summary> /// <summary>
/// Verifies that the name of a command line option without a value can be extracted /// Verifies that the name of a command line argument without a value can
/// be extracted
/// </summary> /// </summary>
[Test] [Test]
public void TestNameExtraction() { public void TestNameExtraction() {
CommandLine.Option option = new CommandLine.Option( CommandLine.Argument argument = CommandLine.Argument.OptionOnly(
new StringSegment("--test"), 2, 4 new StringSegment("--test"), 2, 4
); );
Assert.AreEqual("--test", option.Raw); Assert.AreEqual("--test", argument.Raw);
Assert.AreEqual("--", option.Initiator); Assert.AreEqual("--", argument.Initiator);
Assert.AreEqual("test", option.Name); Assert.AreEqual("test", argument.Name);
Assert.IsNull(option.Associator); Assert.IsNull(argument.Associator);
Assert.IsNull(option.Value); Assert.IsNull(argument.Value);
} }
/// <summary> /// <summary>
/// Verifies that the name of a command line option without a value can be extracted /// Verifies that the name of a command line argument without a value can be
/// when the option is contained in a substring of a larger string /// extracted when the argument is contained in a substring of a larger string
/// </summary> /// </summary>
[Test] [Test]
public void TestNameExtractionFromSubstring() { public void TestNameExtractionFromSubstring() {
CommandLine.Option option = new CommandLine.Option( CommandLine.Argument argument = CommandLine.Argument.OptionOnly(
new StringSegment("||--test||", 2, 6), 4, 4 new StringSegment("||--test||", 2, 6), 4, 4
); );
Assert.AreEqual("--test", option.Raw); Assert.AreEqual("--test", argument.Raw);
Assert.AreEqual("--", option.Initiator); Assert.AreEqual("--", argument.Initiator);
Assert.AreEqual("test", option.Name); Assert.AreEqual("test", argument.Name);
Assert.IsNull(option.Associator); Assert.IsNull(argument.Associator);
Assert.IsNull(option.Value); Assert.IsNull(argument.Value);
} }
/// <summary> /// <summary>
/// Varifies that the name and value of a command line option can be extracted /// Varifies that the name and value of a command line argument can be extracted
/// </summary> /// </summary>
[Test] [Test]
public void TestValueExtraction() { public void TestValueExtraction() {
CommandLine.Option option = new CommandLine.Option( CommandLine.Argument argument = new CommandLine.Argument(
new StringSegment("--test=123"), 2, 4, 7, 3 new StringSegment("--test=123"), 2, 4, 7, 3
); );
Assert.AreEqual("--test=123", option.Raw); Assert.AreEqual("--test=123", argument.Raw);
Assert.AreEqual("--", option.Initiator); Assert.AreEqual("--", argument.Initiator);
Assert.AreEqual("test", option.Name); Assert.AreEqual("test", argument.Name);
Assert.AreEqual("=", option.Associator); Assert.AreEqual("=", argument.Associator);
Assert.AreEqual("123", option.Value); Assert.AreEqual("123", argument.Value);
} }
/// <summary> /// <summary>
/// Varifies that the name and value of a command line option can be extracted /// Varifies that the name and value of a command line argument can be extracted
/// when the option is contained in a substring of a larger string /// when the argument is contained in a substring of a larger string
/// </summary> /// </summary>
[Test] [Test]
public void TestValueExtractionFromSubstring() { public void TestValueExtractionFromSubstring() {
CommandLine.Option option = new CommandLine.Option( CommandLine.Argument argument = new CommandLine.Argument(
new StringSegment("||--test=123||", 2, 10), 4, 4, 9, 3 new StringSegment("||--test=123||", 2, 10), 4, 4, 9, 3
); );
Assert.AreEqual("--test=123", option.Raw); Assert.AreEqual("--test=123", argument.Raw);
Assert.AreEqual("--", option.Initiator); Assert.AreEqual("--", argument.Initiator);
Assert.AreEqual("test", option.Name); Assert.AreEqual("test", argument.Name);
Assert.AreEqual("=", option.Associator); Assert.AreEqual("=", argument.Associator);
Assert.AreEqual("123", option.Value); Assert.AreEqual("123", argument.Value);
} }
/// <summary> /// <summary>
/// Varifies that the name and value of a command line option can be extracted /// Varifies that the name and value of a command line argument can be extracted
/// when the option is assigned a quoted value /// when the option is assigned a quoted value
/// </summary> /// </summary>
[Test] [Test]
public void TestQuotedValueExtraction() { public void TestQuotedValueExtraction() {
CommandLine.Option option = new CommandLine.Option( CommandLine.Argument argument = new CommandLine.Argument(
new StringSegment("--test=\"123\"", 0, 12), 2, 4, 8, 3 new StringSegment("--test=\"123\"", 0, 12), 2, 4, 8, 3
); );
Assert.AreEqual("--test=\"123\"", option.Raw); Assert.AreEqual("--test=\"123\"", argument.Raw);
Assert.AreEqual("--", option.Initiator); Assert.AreEqual("--", argument.Initiator);
Assert.AreEqual("test", option.Name); Assert.AreEqual("test", argument.Name);
Assert.AreEqual("=", option.Associator); Assert.AreEqual("=", argument.Associator);
Assert.AreEqual("123", option.Value); Assert.AreEqual("123", argument.Value);
} }
/// <summary> /// <summary>
/// Varifies that the associator of a command line option with an open ended value /// Varifies that the associator of a command line argument with an open ended
/// assignment can be retrieved /// value assignment can be retrieved
/// </summary> /// </summary>
[Test] [Test]
public void TestValuelessAssociatorRetrieval() { public void TestValuelessAssociatorRetrieval() {
CommandLine.Option option = new CommandLine.Option( CommandLine.Argument argument = CommandLine.Argument.OptionOnly(
new StringSegment("--test="), 2, 4 new StringSegment("--test="), 2, 4
); );
Assert.AreEqual("--test=", option.Raw); Assert.AreEqual("--test=", argument.Raw);
Assert.AreEqual("--", option.Initiator); Assert.AreEqual("--", argument.Initiator);
Assert.AreEqual("test", option.Name); Assert.AreEqual("test", argument.Name);
Assert.AreEqual("=", option.Associator); Assert.AreEqual("=", argument.Associator);
Assert.IsNull(option.Value); Assert.IsNull(argument.Value);
} }
/// <summary> /// <summary>
@ -147,8 +146,8 @@ namespace Nuclex.Support.Parsing {
/// </summary> /// </summary>
[Test] [Test]
public void TestValuelessAssociatorRetrievalFromSubstring() { public void TestValuelessAssociatorRetrievalFromSubstring() {
CommandLine.Option option = new CommandLine.Option( CommandLine.Argument option = CommandLine.Argument.OptionOnly(
new StringSegment("||--test=||", 2, 7), 4, 4//, 9, -1 new StringSegment("||--test=||", 2, 7), 4, 4
); );
Assert.AreEqual("--test=", option.Raw); Assert.AreEqual("--test=", option.Raw);
@ -158,21 +157,91 @@ namespace Nuclex.Support.Parsing {
Assert.IsNull(option.Value); Assert.IsNull(option.Value);
} }
/// <summary>
/// Varifies that a command line argument without an option name can be retrieved
/// </summary>
[Test]
public void TestNamelessValueRetrieval() {
CommandLine.Argument argument = CommandLine.Argument.ValueOnly(
new StringSegment("\"hello world\""), 1, 11
);
Assert.AreEqual("\"hello world\"", argument.Raw);
Assert.IsNull(argument.Initiator);
Assert.IsNull(argument.Name);
Assert.IsNull(argument.Associator);
Assert.AreEqual("hello world", argument.Value);
} }
#endregion // class OptionTest /// <summary>
/// Varifies that a command line argument without an option name can be retrieved
/// that is contained in a substring of larger string
/// </summary>
[Test]
public void TestNamelessValueRetrievalFromSubstring() {
CommandLine.Argument argument = CommandLine.Argument.ValueOnly(
new StringSegment("||\"hello world\"||", 2, 13), 3, 11
);
Assert.AreEqual("\"hello world\"", argument.Raw);
Assert.IsNull(argument.Initiator);
Assert.IsNull(argument.Name);
Assert.IsNull(argument.Associator);
Assert.AreEqual("hello world", argument.Value);
}
}
#endregion // class ArgumentTest
/// <summary> /// <summary>
/// Validates that the parser can handle an argument initiator without an obvious name /// Validates that the parser can handle an argument initiator with an
/// assignment that is missing a name
/// </summary> /// </summary>
[Test] [Test]
public void TestParseAmbiguousNameResolution() { public void TestParseAmbiguousNameResolution() {
CommandLine commandLine = CommandLine.Parse("--:test"); CommandLine commandLine = CommandLine.Parse("--:test");
Assert.AreEqual(0, commandLine.Values.Count); // Without a name, this is not a valid command line option, so it will
Assert.AreEqual(1, commandLine.Options.Count); // be parsed as a loose value instead.
Assert.AreEqual("-", commandLine.Options[0].Name); Assert.AreEqual(1, commandLine.Arguments.Count);
Assert.AreEqual("test", commandLine.Options[0].Value); Assert.AreEqual("--:test", commandLine.Arguments[0].Raw);
Assert.IsNull(commandLine.Arguments[0].Initiator);
Assert.IsNull(commandLine.Arguments[0].Name);
Assert.IsNull(commandLine.Arguments[0].Associator);
Assert.AreEqual("--:test", commandLine.Arguments[0].Value);
}
/// <summary>
/// Verifies that a lone short argument initiator without anything behind
/// can be parsed
/// </summary>
[Test]
public void TestParseShortArgumentInitiatorOnly() {
CommandLine commandLine = CommandLine.Parse("-");
Assert.AreEqual(1, commandLine.Arguments.Count);
Assert.AreEqual("-", commandLine.Arguments[0].Raw);
Assert.IsNull(commandLine.Arguments[0].Initiator);
Assert.IsNull(commandLine.Arguments[0].Name);
Assert.IsNull(commandLine.Arguments[0].Associator);
Assert.AreEqual("-", commandLine.Arguments[0].Value);
}
/// <summary>
/// Verifies that a lone long argument initiator without anything behind
/// can be parsed
/// </summary>
[Test]
public void TestParseLongArgumentInitiatorOnly() {
CommandLine commandLine = CommandLine.Parse("--");
Assert.AreEqual(1, commandLine.Arguments.Count);
Assert.AreEqual("--", commandLine.Arguments[0].Raw);
Assert.IsNull(commandLine.Arguments[0].Initiator);
Assert.IsNull(commandLine.Arguments[0].Name);
Assert.IsNull(commandLine.Arguments[0].Associator);
Assert.AreEqual("--", commandLine.Arguments[0].Value);
} }
/// <summary> /// <summary>
@ -183,25 +252,51 @@ namespace Nuclex.Support.Parsing {
public void TestParseArgumentInitiatorAtEnd() { public void TestParseArgumentInitiatorAtEnd() {
CommandLine commandLine = CommandLine.Parse("-hello:-world -"); CommandLine commandLine = CommandLine.Parse("-hello:-world -");
Assert.AreEqual(1, commandLine.Values.Count); Assert.AreEqual(2, commandLine.Arguments.Count);
Assert.AreEqual(1, commandLine.Options.Count);
Assert.AreEqual("hello", commandLine.Options[0].Name); Assert.AreEqual("-hello:-world", commandLine.Arguments[0].Raw);
Assert.AreEqual("-world", commandLine.Options[0].Value); Assert.AreEqual("-", commandLine.Arguments[0].Initiator);
Assert.AreEqual("-", commandLine.Values[0]); Assert.AreEqual("hello", commandLine.Arguments[0].Name);
Assert.AreEqual(":", commandLine.Arguments[0].Associator);
Assert.AreEqual("-world", commandLine.Arguments[0].Value);
Assert.AreEqual("-", commandLine.Arguments[1].Raw);
Assert.IsNull(commandLine.Arguments[1].Initiator);
Assert.IsNull(commandLine.Arguments[1].Name);
Assert.IsNull(commandLine.Arguments[1].Associator);
Assert.AreEqual("-", commandLine.Arguments[1].Value);
} }
/// <summary>Validates that quoted arguments can be parsed</summary> /// <summary>Validates that quoted arguments can be parsed</summary>
[Test] [Test]
public void TestParseQuotedOption() { public void TestParseQuotedValue() {
CommandLine commandLine = CommandLine.Parse("hello -world --this -is=\"a test\""); CommandLine commandLine = CommandLine.Parse("hello -world --this -is=\"a test\"");
Assert.AreEqual(1, commandLine.Values.Count); Assert.AreEqual(4, commandLine.Arguments.Count);
Assert.AreEqual(3, commandLine.Options.Count);
Assert.AreEqual("hello", commandLine.Values[0]); Assert.AreEqual("hello", commandLine.Arguments[0].Raw);
Assert.AreEqual("world", commandLine.Options[0].Name); Assert.IsNull(commandLine.Arguments[0].Initiator);
Assert.AreEqual("this", commandLine.Options[1].Name); Assert.IsNull(commandLine.Arguments[0].Name);
Assert.AreEqual("is", commandLine.Options[2].Name); Assert.IsNull(commandLine.Arguments[0].Associator);
Assert.AreEqual("a test", commandLine.Options[2].Value); Assert.AreEqual("hello", commandLine.Arguments[0].Value);
Assert.AreEqual("-world", commandLine.Arguments[1].Raw);
Assert.AreEqual("-", commandLine.Arguments[1].Initiator);
Assert.AreEqual("world", commandLine.Arguments[1].Name);
Assert.IsNull(commandLine.Arguments[1].Associator);
Assert.IsNull(commandLine.Arguments[1].Value);
Assert.AreEqual("--this", commandLine.Arguments[2].Raw);
Assert.AreEqual("--", commandLine.Arguments[2].Initiator);
Assert.AreEqual("this", commandLine.Arguments[2].Name);
Assert.IsNull(commandLine.Arguments[2].Associator);
Assert.IsNull(commandLine.Arguments[2].Value);
Assert.AreEqual("-is=\"a test\"", commandLine.Arguments[3].Raw);
Assert.AreEqual("-", commandLine.Arguments[3].Initiator);
Assert.AreEqual("is", commandLine.Arguments[3].Name);
Assert.AreEqual("=", commandLine.Arguments[3].Associator);
Assert.AreEqual("a test", commandLine.Arguments[3].Value);
} }
/// <summary>Validates that null can be parsed</summary> /// <summary>Validates that null can be parsed</summary>
@ -209,8 +304,7 @@ namespace Nuclex.Support.Parsing {
public void TestParseNull() { public void TestParseNull() {
CommandLine commandLine = CommandLine.Parse(null); CommandLine commandLine = CommandLine.Parse(null);
Assert.AreEqual(0, commandLine.Values.Count); Assert.AreEqual(0, commandLine.Arguments.Count);
Assert.AreEqual(0, commandLine.Options.Count);
} }
/// <summary>Validates that a single argument without quotes can be parsed</summary> /// <summary>Validates that a single argument without quotes can be parsed</summary>
@ -218,9 +312,12 @@ namespace Nuclex.Support.Parsing {
public void TestParseSingleNakedValue() { public void TestParseSingleNakedValue() {
CommandLine commandLine = CommandLine.Parse("hello"); CommandLine commandLine = CommandLine.Parse("hello");
Assert.AreEqual(1, commandLine.Values.Count); Assert.AreEqual(1, commandLine.Arguments.Count);
Assert.AreEqual(0, commandLine.Options.Count); Assert.AreEqual("hello", commandLine.Arguments[0].Raw);
Assert.AreEqual("hello", commandLine.Values[0]); Assert.IsNull(commandLine.Arguments[0].Initiator);
Assert.IsNull(commandLine.Arguments[0].Name);
Assert.IsNull(commandLine.Arguments[0].Associator);
Assert.AreEqual("hello", commandLine.Arguments[0].Value);
} }
/// <summary> /// <summary>
@ -231,9 +328,28 @@ namespace Nuclex.Support.Parsing {
public void TestParseQuotedArgumentWithoutClosingQuote() { public void TestParseQuotedArgumentWithoutClosingQuote() {
CommandLine commandLine = CommandLine.Parse("\"Quoted argument"); CommandLine commandLine = CommandLine.Parse("\"Quoted argument");
Assert.AreEqual(1, commandLine.Values.Count); Assert.AreEqual(1, commandLine.Arguments.Count);
Assert.AreEqual(0, commandLine.Options.Count); Assert.AreEqual("\"Quoted argument", commandLine.Arguments[0].Raw);
Assert.AreEqual("Quoted argument", commandLine.Values[0]); Assert.IsNull(commandLine.Arguments[0].Initiator);
Assert.IsNull(commandLine.Arguments[0].Name);
Assert.IsNull(commandLine.Arguments[0].Associator);
Assert.AreEqual("Quoted argument", commandLine.Arguments[0].Value);
}
/// <summary>
/// Validates that the parser correctly handles a quoted value assignment that's
/// missing the closing quote
/// </summary>
[Test]
public void TestParseQuotedValueWithoutClosingQuote() {
CommandLine commandLine = CommandLine.Parse("--test=\"Quoted argument");
Assert.AreEqual(1, commandLine.Arguments.Count);
Assert.AreEqual("--test=\"Quoted argument", commandLine.Arguments[0].Raw);
Assert.AreEqual("--", commandLine.Arguments[0].Initiator);
Assert.AreEqual("test", commandLine.Arguments[0].Name);
Assert.AreEqual("=", commandLine.Arguments[0].Associator);
Assert.AreEqual("Quoted argument", commandLine.Arguments[0].Value);
} }
/// <summary> /// <summary>
@ -243,8 +359,23 @@ namespace Nuclex.Support.Parsing {
public void TestParseSpacesOnly() { public void TestParseSpacesOnly() {
CommandLine commandLine = CommandLine.Parse(" \t "); CommandLine commandLine = CommandLine.Parse(" \t ");
Assert.AreEqual(0, commandLine.Values.Count); Assert.AreEqual(0, commandLine.Arguments.Count);
Assert.AreEqual(0, commandLine.Options.Count); }
/// <summary>
/// Validates that the parser can handle a quoted option
/// </summary>
[Test]
public void TestParseQuotedOption() {
CommandLine commandLine = CommandLine.Parse("--\"hello\"");
// Quoted options are not supported, so this becomes a loose value
Assert.AreEqual(1, commandLine.Arguments.Count);
Assert.AreEqual("--\"hello\"", commandLine.Arguments[0].Raw);
Assert.IsNull(commandLine.Arguments[0].Initiator);
Assert.IsNull(commandLine.Arguments[0].Name);
Assert.IsNull(commandLine.Arguments[0].Associator);
Assert.AreEqual("--\"hello\"", commandLine.Arguments[0].Value);
} }
/// <summary> /// <summary>
@ -255,10 +386,19 @@ namespace Nuclex.Support.Parsing {
public void TestParseMultipleLoneArgumentInitiators() { public void TestParseMultipleLoneArgumentInitiators() {
CommandLine commandLine = CommandLine.Parse("--- --"); CommandLine commandLine = CommandLine.Parse("--- --");
Assert.AreEqual(0, commandLine.Values.Count); Assert.AreEqual(2, commandLine.Arguments.Count);
Assert.AreEqual(2, commandLine.Options.Count);
Assert.AreEqual("-", commandLine.Options[1].Name); Assert.AreEqual("---", commandLine.Arguments[0].Raw);
Assert.AreEqual("-", commandLine.Options[2].Name); Assert.IsNull(commandLine.Arguments[0].Initiator);
Assert.IsNull(commandLine.Arguments[0].Name);
Assert.IsNull(commandLine.Arguments[0].Associator);
Assert.AreEqual("---", commandLine.Arguments[0].Value);
Assert.AreEqual("--", commandLine.Arguments[1].Raw);
Assert.IsNull(commandLine.Arguments[1].Initiator);
Assert.IsNull(commandLine.Arguments[1].Name);
Assert.IsNull(commandLine.Arguments[1].Associator);
Assert.AreEqual("--", commandLine.Arguments[1].Value);
} }
/// <summary> /// <summary>
@ -268,10 +408,19 @@ namespace Nuclex.Support.Parsing {
public void TestParseOptionWithEmbeddedInitiator() { public void TestParseOptionWithEmbeddedInitiator() {
CommandLine commandLine = CommandLine.Parse("-hello/world=123 -test-case"); CommandLine commandLine = CommandLine.Parse("-hello/world=123 -test-case");
Assert.AreEqual(0, commandLine.Values.Count); Assert.AreEqual(2, commandLine.Arguments.Count);
Assert.AreEqual(2, commandLine.Options.Count);
Assert.AreEqual("hello/world", commandLine.Options[0].Name); Assert.AreEqual("-hello/world=123", commandLine.Arguments[0].Raw);
Assert.AreEqual("test-case", commandLine.Options[1].Name); Assert.AreEqual("-", commandLine.Arguments[0].Initiator);
Assert.AreEqual("hello/world", commandLine.Arguments[0].Name);
Assert.AreEqual("=", commandLine.Arguments[0].Associator);
Assert.AreEqual("123", commandLine.Arguments[0].Value);
Assert.AreEqual("-test-case", commandLine.Arguments[1].Raw);
Assert.AreEqual("-", commandLine.Arguments[1].Initiator);
Assert.AreEqual("test-case", commandLine.Arguments[1].Name);
Assert.IsNull(commandLine.Arguments[1].Associator);
Assert.IsNull(commandLine.Arguments[1].Value);
} }
/// <summary> /// <summary>
@ -281,11 +430,25 @@ namespace Nuclex.Support.Parsing {
public void TestParseOptionAndValueWithoutSpaces() { public void TestParseOptionAndValueWithoutSpaces() {
CommandLine commandLine = CommandLine.Parse("\"value\"-option\"value\""); CommandLine commandLine = CommandLine.Parse("\"value\"-option\"value\"");
Assert.AreEqual(2, commandLine.Values.Count); Assert.AreEqual(3, commandLine.Arguments.Count);
Assert.AreEqual(1, commandLine.Options.Count);
Assert.AreEqual("value", commandLine.Values[0]); Assert.AreEqual("\"value\"", commandLine.Arguments[0].Raw);
Assert.AreEqual("option", commandLine.Options[0].Name); Assert.IsNull(commandLine.Arguments[0].Initiator);
Assert.AreEqual("value", commandLine.Values[1]); Assert.IsNull(commandLine.Arguments[0].Name);
Assert.IsNull(commandLine.Arguments[0].Associator);
Assert.AreEqual("value", commandLine.Arguments[0].Value);
Assert.AreEqual("-option", commandLine.Arguments[1].Raw);
Assert.AreEqual("-", commandLine.Arguments[1].Initiator);
Assert.AreEqual("option", commandLine.Arguments[1].Name);
Assert.IsNull(commandLine.Arguments[1].Associator);
Assert.IsNull(commandLine.Arguments[1].Value);
Assert.AreEqual("\"value\"", commandLine.Arguments[2].Raw);
Assert.IsNull(commandLine.Arguments[2].Initiator);
Assert.IsNull(commandLine.Arguments[2].Name);
Assert.IsNull(commandLine.Arguments[2].Associator);
Assert.AreEqual("value", commandLine.Arguments[2].Value);
} }
/// <summary> /// <summary>
@ -296,10 +459,19 @@ namespace Nuclex.Support.Parsing {
public void TestParseOptionWithModifierAtEnd() { public void TestParseOptionWithModifierAtEnd() {
CommandLine commandLine = CommandLine.Parse("--test-value- -test+"); CommandLine commandLine = CommandLine.Parse("--test-value- -test+");
Assert.AreEqual(0, commandLine.Values.Count); Assert.AreEqual(2, commandLine.Arguments.Count);
Assert.AreEqual(2, commandLine.Options.Count);
Assert.AreEqual("test-value", commandLine.Options[0].Name); Assert.AreEqual("--test-value-", commandLine.Arguments[0].Raw);
Assert.AreEqual("test", commandLine.Options[1].Name); Assert.AreEqual("--", commandLine.Arguments[0].Initiator);
Assert.AreEqual("test-value", commandLine.Arguments[0].Name);
Assert.IsNull(commandLine.Arguments[0].Associator);
Assert.AreEqual("-", commandLine.Arguments[0].Value);
Assert.AreEqual("-test+", commandLine.Arguments[1].Raw);
Assert.AreEqual("-", commandLine.Arguments[1].Initiator);
Assert.AreEqual("test", commandLine.Arguments[1].Name);
Assert.IsNull(commandLine.Arguments[1].Associator);
Assert.AreEqual("+", commandLine.Arguments[1].Value);
} }
/// <summary> /// <summary>
@ -307,14 +479,21 @@ namespace Nuclex.Support.Parsing {
/// </summary> /// </summary>
[Test] [Test]
public void TestParseOptionWithAssignment() { public void TestParseOptionWithAssignment() {
CommandLine commandLine = CommandLine.Parse("-hello:123 -world=321"); CommandLine commandLine = CommandLine.Parse("-hello: -world=321");
Assert.AreEqual(0, commandLine.Values.Count); Assert.AreEqual(2, commandLine.Arguments.Count);
Assert.AreEqual(2, commandLine.Options.Count);
Assert.AreEqual("hello", commandLine.Options[0].Name); Assert.AreEqual("-hello:", commandLine.Arguments[0].Raw);
Assert.AreEqual("123", commandLine.Options[0].Value); Assert.AreEqual("-", commandLine.Arguments[0].Initiator);
Assert.AreEqual("world", commandLine.Options[1].Name); Assert.AreEqual("hello", commandLine.Arguments[0].Name);
Assert.AreEqual("321", commandLine.Options[1].Value); Assert.AreEqual(":", commandLine.Arguments[0].Associator);
Assert.IsNull(commandLine.Arguments[0].Value);
Assert.AreEqual("-world=321", commandLine.Arguments[1].Raw);
Assert.AreEqual("-", commandLine.Arguments[1].Initiator);
Assert.AreEqual("world", commandLine.Arguments[1].Name);
Assert.AreEqual("=", commandLine.Arguments[1].Associator);
Assert.AreEqual("321", commandLine.Arguments[1].Value);
} }
/// <summary> /// <summary>
@ -325,9 +504,12 @@ namespace Nuclex.Support.Parsing {
public void TestParseOptionAtEndOfString() { public void TestParseOptionAtEndOfString() {
CommandLine commandLine = CommandLine.Parse("--test:"); CommandLine commandLine = CommandLine.Parse("--test:");
Assert.AreEqual(0, commandLine.Values.Count); Assert.AreEqual(1, commandLine.Arguments.Count);
Assert.AreEqual(1, commandLine.Options.Count); Assert.AreEqual("--test:", commandLine.Arguments[0].Raw);
Assert.AreEqual("test", commandLine.Options[0].Name); Assert.AreEqual("--", commandLine.Arguments[0].Initiator);
Assert.AreEqual("test", commandLine.Arguments[0].Name);
Assert.AreEqual(":", commandLine.Arguments[0].Associator);
Assert.IsNull(commandLine.Arguments[0].Value);
} }
/// <summary> /// <summary>
@ -338,11 +520,19 @@ namespace Nuclex.Support.Parsing {
public void TestWindowsOptionInitiator() { public void TestWindowsOptionInitiator() {
CommandLine commandLine = CommandLine.Parse("/hello //world", true); CommandLine commandLine = CommandLine.Parse("/hello //world", true);
Assert.AreEqual(1, commandLine.Values.Count); Assert.AreEqual(2, commandLine.Arguments.Count);
Assert.AreEqual(2, commandLine.Options.Count);
Assert.AreEqual("hello", commandLine.Options[0].Name); Assert.AreEqual("/hello", commandLine.Arguments[0].Raw);
Assert.AreEqual("/", commandLine.Options[0].Value); Assert.AreEqual("/", commandLine.Arguments[0].Initiator);
Assert.AreEqual("world", commandLine.Options[1].Name); Assert.AreEqual("hello", commandLine.Arguments[0].Name);
Assert.IsNull(commandLine.Arguments[0].Associator);
Assert.IsNull(commandLine.Arguments[0].Value);
Assert.AreEqual("//world", commandLine.Arguments[1].Raw);
Assert.IsNull(commandLine.Arguments[1].Initiator);
Assert.IsNull(commandLine.Arguments[1].Name);
Assert.IsNull(commandLine.Arguments[1].Associator);
Assert.AreEqual("//world", commandLine.Arguments[1].Value);
} }
/// <summary> /// <summary>
@ -353,16 +543,23 @@ namespace Nuclex.Support.Parsing {
public void TestNonWindowsOptionValues() { public void TestNonWindowsOptionValues() {
CommandLine commandLine = CommandLine.Parse("/hello //world", false); CommandLine commandLine = CommandLine.Parse("/hello //world", false);
Assert.AreEqual(2, commandLine.Values.Count); Assert.AreEqual(2, commandLine.Arguments.Count);
Assert.AreEqual(0, commandLine.Options.Count);
Assert.AreEqual("/hello", commandLine.Values[0]); Assert.AreEqual("/hello", commandLine.Arguments[0].Raw);
Assert.AreEqual("//world", commandLine.Values[1]); Assert.IsNull(commandLine.Arguments[0].Initiator);
Assert.IsNull(commandLine.Arguments[0].Name);
Assert.IsNull(commandLine.Arguments[0].Associator);
Assert.AreEqual("/hello", commandLine.Arguments[0].Value);
Assert.AreEqual("//world", commandLine.Arguments[1].Raw);
Assert.IsNull(commandLine.Arguments[1].Initiator);
Assert.IsNull(commandLine.Arguments[1].Name);
Assert.IsNull(commandLine.Arguments[1].Associator);
Assert.AreEqual("//world", commandLine.Arguments[1].Value);
} }
} }
} // namespace Nuclex.Support.Parsing } // namespace Nuclex.Support.Parsing
#endif // ENABLE_BROKEN_COMMAND_LINE_PARSER
#endif // UNITTEST #endif // UNITTEST

View File

@ -24,13 +24,9 @@ using System.IO;
using Nuclex.Support.Collections; using Nuclex.Support.Collections;
#if ENABLE_BROKEN_COMMAND_LINE_PARSER
namespace Nuclex.Support.Parsing { namespace Nuclex.Support.Parsing {
/// <summary> /// <summary>Parses and stores an application's command line parameters</summary>
/// Manages an application's command line parameters
/// </summary>
/// <remarks> /// <remarks>
/// <para> /// <para>
/// At the time of the creation of this component, there are already several command /// At the time of the creation of this component, there are already several command
@ -39,14 +35,14 @@ namespace Nuclex.Support.Parsing {
/// arrive at their results. /// arrive at their results.
/// </para> /// </para>
/// <para> /// <para>
/// This class does nothing more than represent the command line to the application. /// This command line parser does nothing more than represent the command line to
/// It can parse a command line /// the application through a convenient interface. It parses a command line and
/// parse the command line arguments. It doesn't /// extracts the arguments, but doesn't interpret them and or check them for validity.
/// interpret them and it doesn't check them for validity. This promotes simplicity /// </para>
/// and allows t /// <para>
/// be unit-tested and is an ideal building block to create actual command line /// This design promotes simplicity and makes is an ideal building block to create
/// interpreters that connect the parameters to program instructions and or fill /// actual command line interpreters that connect the parameters to program
/// structures in code. /// instructions and or fill structures in code.
/// </para> /// </para>
/// <para> /// <para>
/// Terminology /// Terminology
@ -82,18 +78,12 @@ namespace Nuclex.Support.Parsing {
/// </item> /// </item>
/// </list> /// </list>
/// </para> /// </para>
/// <para>
/// What this parser doesn't support is spaced assignments (eg. '--format png') since
/// these are ambiguous if the parser doesn't know beforehand whether "format" accepts
/// a non-optional argument.
/// </para>
/// </remarks> /// </remarks>
public partial class CommandLine { public partial class CommandLine {
/// <summary>Initializes a new command line</summary> /// <summary>Initializes a new command line</summary>
public CommandLine() { public CommandLine() {
this.options = new List<Option>(); this.arguments = new List<Argument>();
this.values = new List<string>();
} }
/// <summary>Parses the command line arguments from the provided string</summary> /// <summary>Parses the command line arguments from the provided string</summary>
@ -117,42 +107,39 @@ namespace Nuclex.Support.Parsing {
/// <summary>Adds a loose value to the command line</summary> /// <summary>Adds a loose value to the command line</summary>
/// <param name="value">Value taht will be added</param> /// <param name="value">Value taht will be added</param>
internal void addValue(StringSegment value) { internal void addValue(StringSegment value) {
/*
Console.WriteLine("Discovered loose value: '" + value.ToString() + "'"); Console.WriteLine("Discovered loose value: '" + value.ToString() + "'");
*/
this.values.Add(value.ToString()); this.arguments.Add(
Argument.ValueOnly(value, value.Offset, value.Count)
);
} }
/// <summary>Adds an option to the command line</summary> /// <summary>Adds an argument to the command line</summary>
/// <param name="option">Option that will be added</param> /// <param name="argument">Argument that will be added</param>
internal void addOption(Option option) { internal void addArgument(Argument argument) {
Console.WriteLine("Discovered option: '" + option.Raw.ToString() + "'"); /*
Console.WriteLine(" Name: '" + option.Name + "'"); Console.WriteLine("Discovered option: '" + argument.Raw.ToString() + "'");
if(option.Value != null) { Console.WriteLine(" Name: '" + argument.Name + "'");
Console.WriteLine(" Value: '" + option.Value + "'"); if(argument.Value != null) {
Console.WriteLine(" Value: '" + argument.Value + "'");
} }
*/
this.options.Add(option); this.arguments.Add(argument);
} }
#endregion // To Be Removed #endregion // To Be Removed
/// <summary>Options that were specified on the command line</summary> /// <summary>Options that were specified on the command line</summary>
public IList<Option> Options { public IList<Argument> Arguments {
get { return this.options; } get { return this.arguments; }
}
/// <summary>Loose values that were given on the command line</summary>
public IList<string> Values {
get { return this.values; }
} }
/// <summary>Options that were specified on the command line</summary> /// <summary>Options that were specified on the command line</summary>
private List<Option> options; private List<Argument> arguments;
/// <summary>Loose values that were given on the command line</summary>
private List<string> values;
} }
} // namespace Nuclex.Support.Parsing } // namespace Nuclex.Support.Parsing
#endif // ENABLE_BROKEN_COMMAND_LINE_PARSER