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:
parent
6d2b39255f
commit
47b0039137
|
@ -148,7 +148,7 @@
|
|||
<Compile Include="Source\Licensing\LicenseKey.Test.cs">
|
||||
<DependentUpon>LicenseKey.cs</DependentUpon>
|
||||
</Compile>
|
||||
<Compile Include="Source\Parsing\CommandLine.Option.cs">
|
||||
<Compile Include="Source\Parsing\CommandLine.Argument.cs">
|
||||
<DependentUpon>CommandLine.cs</DependentUpon>
|
||||
</Compile>
|
||||
<Compile Include="Source\Parsing\CommandLine.Formatter.cs">
|
||||
|
|
|
@ -130,7 +130,7 @@
|
|||
<Compile Include="Source\Licensing\LicenseKey.Test.cs">
|
||||
<DependentUpon>LicenseKey.cs</DependentUpon>
|
||||
</Compile>
|
||||
<Compile Include="Source\Parsing\CommandLine.Option.cs">
|
||||
<Compile Include="Source\Parsing\CommandLine.Argument.cs">
|
||||
<DependentUpon>CommandLine.cs</DependentUpon>
|
||||
</Compile>
|
||||
<Compile Include="Source\Parsing\CommandLine.Formatter.cs">
|
||||
|
|
|
@ -26,7 +26,7 @@ using System.Text;
|
|||
|
||||
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 {
|
||||
|
||||
|
@ -142,6 +142,6 @@ namespace Nuclex.Support.Parsing {
|
|||
|
||||
} // namespace Nuclex.Support.Parsing
|
||||
|
||||
#endif
|
||||
#endif // ENABLE_BROKEN_COMMAND_LINE_PARSER
|
||||
|
||||
#endif // UNITTEST
|
|
@ -21,7 +21,7 @@ License along with this library
|
|||
using System.Collections.Specialized;
|
||||
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 {
|
||||
|
||||
|
@ -182,4 +182,4 @@ namespace Nuclex.Support.Parsing {
|
|||
|
||||
} // namespace Nuclex.Support.Parsing
|
||||
|
||||
#endif
|
||||
#endif // ENABLE_BROKEN_COMMAND_LINE_PARSER
|
||||
|
|
|
@ -22,27 +22,40 @@ using System;
|
|||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
|
||||
#if ENABLE_BROKEN_COMMAND_LINE_PARSER
|
||||
|
||||
namespace Nuclex.Support.Parsing {
|
||||
|
||||
partial class CommandLine {
|
||||
|
||||
/// <summary>Option being specified on an application's command line</summary>
|
||||
public class Option {
|
||||
/// <summary>Argument being specified on an application's command line</summary>
|
||||
public class Argument {
|
||||
|
||||
/// <summary>Initializes a new option with only a name</summary>
|
||||
/// <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 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>
|
||||
/// <returns>The newly created option</returns>
|
||||
internal Option(
|
||||
internal static Argument OptionOnly(
|
||||
StringSegment raw,
|
||||
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>
|
||||
/// <param name="raw">
|
||||
|
@ -53,7 +66,7 @@ namespace Nuclex.Support.Parsing {
|
|||
/// <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 Option(
|
||||
internal Argument(
|
||||
StringSegment raw,
|
||||
int nameStart, int nameLength,
|
||||
int valueStart, int valueLength
|
||||
|
@ -63,9 +76,6 @@ namespace Nuclex.Support.Parsing {
|
|||
this.nameLength = nameLength;
|
||||
this.valueStart = valueStart;
|
||||
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>
|
||||
|
@ -76,32 +86,46 @@ namespace Nuclex.Support.Parsing {
|
|||
/// <summary>Characters used to initiate this option</summary>
|
||||
public string Initiator {
|
||||
get {
|
||||
return this.raw.Text.Substring(
|
||||
this.raw.Offset, this.nameStart - this.raw.Offset
|
||||
);
|
||||
if(this.nameStart == -1) {
|
||||
return null;
|
||||
} else {
|
||||
return this.raw.Text.Substring(
|
||||
this.raw.Offset, this.nameStart - this.raw.Offset
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>Name of the command line option</summary>
|
||||
public string Name {
|
||||
get {
|
||||
return this.raw.Text.Substring(this.nameStart, this.nameLength);
|
||||
if(this.nameStart == -1) {
|
||||
return null;
|
||||
} else {
|
||||
return this.raw.Text.Substring(this.nameStart, this.nameLength);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>Characters used to associate a value to this option</summary>
|
||||
public string Associator {
|
||||
get {
|
||||
int associatorStart = this.nameStart + this.nameLength;
|
||||
if(this.nameStart == -1) {
|
||||
return null;
|
||||
} else {
|
||||
int associatorStart = this.nameStart + this.nameLength;
|
||||
|
||||
if(this.valueStart == -1) {
|
||||
int characterCount = (this.raw.Offset + this.raw.Count) - associatorStart;
|
||||
if(characterCount == 0) {
|
||||
if(this.valueStart == -1) {
|
||||
int characterCount = (this.raw.Offset + this.raw.Count) - associatorStart;
|
||||
if(characterCount == 0) {
|
||||
return null;
|
||||
}
|
||||
} else if(this.valueStart == associatorStart) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
return this.raw.Text.Substring(associatorStart, 1);
|
||||
return this.raw.Text.Substring(associatorStart, 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -135,5 +159,3 @@ namespace Nuclex.Support.Parsing {
|
|||
}
|
||||
|
||||
} // namespace Nuclex.Support.Parsing
|
||||
|
||||
#endif // ENABLE_BROKEN_COMMAND_LINE_PARSER
|
|
@ -21,8 +21,6 @@ License along with this library
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
#if ENABLE_BROKEN_COMMAND_LINE_PARSER
|
||||
|
||||
namespace Nuclex.Support.Parsing {
|
||||
|
||||
partial class CommandLine {
|
||||
|
@ -32,5 +30,3 @@ namespace Nuclex.Support.Parsing {
|
|||
}
|
||||
|
||||
} // namespace Nuclex.Support.Parsing
|
||||
|
||||
#endif // ENABLE_BROKEN_COMMAND_LINE_PARSER
|
||||
|
|
|
@ -23,8 +23,6 @@ using System.Collections.Generic;
|
|||
using System.Diagnostics;
|
||||
using System.IO;
|
||||
|
||||
#if ENABLE_BROKEN_COMMAND_LINE_PARSER
|
||||
|
||||
namespace Nuclex.Support.Parsing {
|
||||
|
||||
partial class CommandLine {
|
||||
|
@ -119,7 +117,7 @@ namespace Nuclex.Support.Parsing {
|
|||
// Windows style argument using '/' as its initiator
|
||||
case '/': {
|
||||
// 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.
|
||||
if(!this.windowsMode) {
|
||||
goto default;
|
||||
|
@ -155,7 +153,7 @@ namespace Nuclex.Support.Parsing {
|
|||
/// </param>
|
||||
/// <returns>The number of characters consumed</returns>
|
||||
private void parsePotentialOption(
|
||||
string commandLineString, int initiatorStartIndex, ref int index
|
||||
string commandLineString, int initiatorStartIndex, ref int index
|
||||
) {
|
||||
|
||||
// If the string ends here this can only be considered as a loose value
|
||||
|
@ -174,14 +172,16 @@ namespace Nuclex.Support.Parsing {
|
|||
|
||||
// Look for the first character that ends the option. If it is not an actual option,
|
||||
// the very first character might be the end
|
||||
index = commandLineString.IndexOfAny(OptionNameEndingCharacters, nameStartIndex);
|
||||
if(index == -1) {
|
||||
index = commandLineString.Length;
|
||||
if(commandLineString[index] != commandLineString[initiatorStartIndex]) {
|
||||
index = commandLineString.IndexOfAny(NameEndingCharacters, nameStartIndex);
|
||||
if(index == -1) {
|
||||
index = commandLineString.Length;
|
||||
}
|
||||
}
|
||||
|
||||
// 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
|
||||
if(index == nameStartIndex) {
|
||||
if((index == nameStartIndex)/* && !isAssignmentCharacter(commandLineString[index])*/) {
|
||||
index = commandLineString.IndexOfAny(WhitespaceCharacters, index);
|
||||
if(index == -1) {
|
||||
index = commandLineString.Length;
|
||||
|
@ -195,9 +195,10 @@ namespace Nuclex.Support.Parsing {
|
|||
return;
|
||||
}
|
||||
|
||||
parseOptionAssignment(
|
||||
parsePotentialOptionAssignment(
|
||||
commandLineString, initiatorStartIndex, nameStartIndex, ref index
|
||||
);
|
||||
|
||||
}
|
||||
|
||||
/// <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
|
||||
/// </param>
|
||||
/// <param name="index">Index at which the option name ended</param>
|
||||
private void parseOptionAssignment(
|
||||
string commandLineString, int initiatorStartIndex, int nameStartIndex, ref int index
|
||||
private void parsePotentialOptionAssignment(
|
||||
string commandLineString, int initiatorStartIndex, int nameStartIndex, ref int index
|
||||
) {
|
||||
int nameEndIndex = index;
|
||||
int valueStartIndex;
|
||||
int valueEndIndex;
|
||||
|
||||
if(index == commandLineString.Length) {
|
||||
valueStartIndex = -1;
|
||||
valueEndIndex = -1;
|
||||
} else {
|
||||
// See if this is an assignment character. If it is, the assigned value
|
||||
// should follow to the right.
|
||||
bool isAssignment =
|
||||
(index < commandLineString.Length) &&
|
||||
isAssignmentCharacter(commandLineString[index]);
|
||||
|
||||
char currentCharacter = commandLineString[index];
|
||||
bool isAssignment =
|
||||
(currentCharacter == ':') ||
|
||||
(currentCharacter == '=');
|
||||
// If it's an assignment, we can proceed parsing the assigned value
|
||||
if(isAssignment) {
|
||||
++index;
|
||||
parseOptionValue(commandLineString, initiatorStartIndex, nameStartIndex, ref index);
|
||||
return;
|
||||
} else { // No, it's an option name without an assignment
|
||||
|
||||
// Does the string end after the suspected assignment character?
|
||||
bool argumentEndReached = ((index + 1) == commandLineString.Length);
|
||||
bool isModifier =
|
||||
(commandLineString[index - 1] == '+') ||
|
||||
(commandLineString[index - 1] == '-');
|
||||
|
||||
if(isAssignment) {
|
||||
parseOptionValue(commandLineString, initiatorStartIndex, nameStartIndex, ref index);
|
||||
return;
|
||||
if(isModifier) {
|
||||
valueStartIndex = index - 1;
|
||||
valueEndIndex = index;
|
||||
--nameEndIndex;
|
||||
} else {
|
||||
|
||||
bool isModifier =
|
||||
(currentCharacter == '+') ||
|
||||
(currentCharacter == '-');
|
||||
|
||||
if(isModifier) {
|
||||
valueStartIndex = index;
|
||||
++index;
|
||||
valueEndIndex = index;
|
||||
} else {
|
||||
valueStartIndex = -1;
|
||||
valueEndIndex = -1;
|
||||
}
|
||||
valueStartIndex = -1;
|
||||
valueEndIndex = -1;
|
||||
}
|
||||
}
|
||||
|
||||
int argumentLength = index - initiatorStartIndex;
|
||||
this.commandLine.addOption(
|
||||
new Option(
|
||||
this.commandLine.addArgument(
|
||||
new Argument(
|
||||
new StringSegment(commandLineString, initiatorStartIndex, argumentLength),
|
||||
nameStartIndex, nameEndIndex - nameStartIndex,
|
||||
valueStartIndex, valueEndIndex - valueStartIndex
|
||||
|
@ -269,24 +264,23 @@ namespace Nuclex.Support.Parsing {
|
|||
/// </param>
|
||||
/// <param name="index">Index at which the option name ended</param>
|
||||
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;
|
||||
|
||||
// Does the string end after the suspected assignment character?
|
||||
bool argumentEndReached = ((index + 1) == commandLineString.Length);
|
||||
bool argumentEndReached = (index == commandLineString.Length);
|
||||
|
||||
if(argumentEndReached) {
|
||||
++index;
|
||||
valueStartIndex = -1;
|
||||
valueEndIndex = -1;
|
||||
} else {
|
||||
char nextCharacter = commandLineString[index + 1];
|
||||
char nextCharacter = commandLineString[index];
|
||||
|
||||
// Is this a quoted assignment
|
||||
if(nextCharacter == '"') {
|
||||
index += 2;
|
||||
++index;
|
||||
valueStartIndex = index;
|
||||
index = commandLineString.IndexOf('"', index);
|
||||
if(index == -1) {
|
||||
|
@ -297,7 +291,6 @@ namespace Nuclex.Support.Parsing {
|
|||
++index;
|
||||
}
|
||||
} else { // Nope, assuming unquoted assignment or empty assignment
|
||||
++index;
|
||||
valueStartIndex = index;
|
||||
index = commandLineString.IndexOfAny(WhitespaceCharacters, index);
|
||||
if(index == -1) {
|
||||
|
@ -315,8 +308,8 @@ namespace Nuclex.Support.Parsing {
|
|||
}
|
||||
|
||||
int argumentLength = index - initiatorStartIndex;
|
||||
this.commandLine.addOption(
|
||||
new Option(
|
||||
this.commandLine.addArgument(
|
||||
new Argument(
|
||||
new StringSegment(commandLineString, initiatorStartIndex, argumentLength),
|
||||
nameStartIndex, nameEndIndex - nameStartIndex,
|
||||
valueStartIndex, valueEndIndex - valueStartIndex
|
||||
|
@ -328,19 +321,26 @@ namespace Nuclex.Support.Parsing {
|
|||
/// <param name="commandLineString">String the quoted value is parsed from</param>
|
||||
/// <param name="index">Index at which the quoted value begins</param>
|
||||
private void parseQuotedValue(string commandLineString, ref int index) {
|
||||
int startIndex = index;
|
||||
char quoteCharacter = commandLineString[index];
|
||||
int startIndex = index + 1;
|
||||
int valueIndex = startIndex + 1;
|
||||
|
||||
// Search for the closing quote
|
||||
index = commandLineString.IndexOf(quoteCharacter, startIndex);
|
||||
index = commandLineString.IndexOf(quoteCharacter, valueIndex);
|
||||
if(index == -1) {
|
||||
index = commandLineString.Length; // value ends at string end
|
||||
commandLine.addValue(
|
||||
new StringSegment(commandLineString, startIndex, index - startIndex)
|
||||
commandLine.addArgument(
|
||||
Argument.ValueOnly(
|
||||
new StringSegment(commandLineString, startIndex, index - startIndex),
|
||||
valueIndex, index - valueIndex
|
||||
)
|
||||
);
|
||||
} else { // A closing quote was found
|
||||
commandLine.addValue(
|
||||
new StringSegment(commandLineString, startIndex, index - startIndex)
|
||||
commandLine.addArgument(
|
||||
Argument.ValueOnly(
|
||||
new StringSegment(commandLineString, startIndex, index - startIndex + 1),
|
||||
valueIndex, index - valueIndex
|
||||
)
|
||||
);
|
||||
++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>
|
||||
private static readonly char[] OptionNameEndingCharacters = new char[] {
|
||||
' ', '\t', '=', ':', '/', '-', '+', '"'
|
||||
private static readonly char[] NameEndingCharacters = new char[] {
|
||||
' ', '\t', '=', ':', '"'
|
||||
};
|
||||
|
||||
/// <summary>Characters the parser considers to be whitespace</summary>
|
||||
|
@ -380,5 +393,3 @@ namespace Nuclex.Support.Parsing {
|
|||
}
|
||||
|
||||
} // namespace Nuclex.Support.Parsing
|
||||
|
||||
#endif // ENABLE_BROKEN_COMMAND_LINE_PARSER
|
||||
|
|
|
@ -26,118 +26,117 @@ using System.Text;
|
|||
|
||||
using NUnit.Framework;
|
||||
|
||||
#if ENABLE_BROKEN_COMMAND_LINE_PARSER
|
||||
|
||||
namespace Nuclex.Support.Parsing {
|
||||
|
||||
/// <summary>Ensures that the command line parser is working properly</summary>
|
||||
[TestFixture]
|
||||
public class CommandLineTest {
|
||||
|
||||
#region class OptionTest
|
||||
#region class ArgumentTest
|
||||
|
||||
/// <summary>Unit test for the command line option class</summary>
|
||||
[TestFixture]
|
||||
public class OptionTest {
|
||||
public class ArgumentTest {
|
||||
|
||||
/// <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>
|
||||
[Test]
|
||||
public void TestNameExtraction() {
|
||||
CommandLine.Option option = new CommandLine.Option(
|
||||
CommandLine.Argument argument = CommandLine.Argument.OptionOnly(
|
||||
new StringSegment("--test"), 2, 4
|
||||
);
|
||||
|
||||
Assert.AreEqual("--test", option.Raw);
|
||||
Assert.AreEqual("--", option.Initiator);
|
||||
Assert.AreEqual("test", option.Name);
|
||||
Assert.IsNull(option.Associator);
|
||||
Assert.IsNull(option.Value);
|
||||
Assert.AreEqual("--test", argument.Raw);
|
||||
Assert.AreEqual("--", argument.Initiator);
|
||||
Assert.AreEqual("test", argument.Name);
|
||||
Assert.IsNull(argument.Associator);
|
||||
Assert.IsNull(argument.Value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Verifies that the name of a command line option without a value can be extracted
|
||||
/// when the option is contained in a substring of a larger string
|
||||
/// Verifies that the name of a command line argument without a value can be
|
||||
/// extracted when the argument is contained in a substring of a larger string
|
||||
/// </summary>
|
||||
[Test]
|
||||
public void TestNameExtractionFromSubstring() {
|
||||
CommandLine.Option option = new CommandLine.Option(
|
||||
CommandLine.Argument argument = CommandLine.Argument.OptionOnly(
|
||||
new StringSegment("||--test||", 2, 6), 4, 4
|
||||
);
|
||||
|
||||
Assert.AreEqual("--test", option.Raw);
|
||||
Assert.AreEqual("--", option.Initiator);
|
||||
Assert.AreEqual("test", option.Name);
|
||||
Assert.IsNull(option.Associator);
|
||||
Assert.IsNull(option.Value);
|
||||
Assert.AreEqual("--test", argument.Raw);
|
||||
Assert.AreEqual("--", argument.Initiator);
|
||||
Assert.AreEqual("test", argument.Name);
|
||||
Assert.IsNull(argument.Associator);
|
||||
Assert.IsNull(argument.Value);
|
||||
}
|
||||
|
||||
/// <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>
|
||||
[Test]
|
||||
public void TestValueExtraction() {
|
||||
CommandLine.Option option = new CommandLine.Option(
|
||||
CommandLine.Argument argument = new CommandLine.Argument(
|
||||
new StringSegment("--test=123"), 2, 4, 7, 3
|
||||
);
|
||||
|
||||
Assert.AreEqual("--test=123", option.Raw);
|
||||
Assert.AreEqual("--", option.Initiator);
|
||||
Assert.AreEqual("test", option.Name);
|
||||
Assert.AreEqual("=", option.Associator);
|
||||
Assert.AreEqual("123", option.Value);
|
||||
Assert.AreEqual("--test=123", argument.Raw);
|
||||
Assert.AreEqual("--", argument.Initiator);
|
||||
Assert.AreEqual("test", argument.Name);
|
||||
Assert.AreEqual("=", argument.Associator);
|
||||
Assert.AreEqual("123", argument.Value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Varifies that the name and value of a command line option can be extracted
|
||||
/// when the option is contained in a substring of a larger string
|
||||
/// Varifies that the name and value of a command line argument can be extracted
|
||||
/// when the argument is contained in a substring of a larger string
|
||||
/// </summary>
|
||||
[Test]
|
||||
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
|
||||
);
|
||||
|
||||
Assert.AreEqual("--test=123", option.Raw);
|
||||
Assert.AreEqual("--", option.Initiator);
|
||||
Assert.AreEqual("test", option.Name);
|
||||
Assert.AreEqual("=", option.Associator);
|
||||
Assert.AreEqual("123", option.Value);
|
||||
Assert.AreEqual("--test=123", argument.Raw);
|
||||
Assert.AreEqual("--", argument.Initiator);
|
||||
Assert.AreEqual("test", argument.Name);
|
||||
Assert.AreEqual("=", argument.Associator);
|
||||
Assert.AreEqual("123", argument.Value);
|
||||
}
|
||||
|
||||
/// <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
|
||||
/// </summary>
|
||||
[Test]
|
||||
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
|
||||
);
|
||||
|
||||
Assert.AreEqual("--test=\"123\"", option.Raw);
|
||||
Assert.AreEqual("--", option.Initiator);
|
||||
Assert.AreEqual("test", option.Name);
|
||||
Assert.AreEqual("=", option.Associator);
|
||||
Assert.AreEqual("123", option.Value);
|
||||
Assert.AreEqual("--test=\"123\"", argument.Raw);
|
||||
Assert.AreEqual("--", argument.Initiator);
|
||||
Assert.AreEqual("test", argument.Name);
|
||||
Assert.AreEqual("=", argument.Associator);
|
||||
Assert.AreEqual("123", argument.Value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Varifies that the associator of a command line option with an open ended value
|
||||
/// assignment can be retrieved
|
||||
/// Varifies that the associator of a command line argument with an open ended
|
||||
/// value assignment can be retrieved
|
||||
/// </summary>
|
||||
[Test]
|
||||
public void TestValuelessAssociatorRetrieval() {
|
||||
CommandLine.Option option = new CommandLine.Option(
|
||||
CommandLine.Argument argument = CommandLine.Argument.OptionOnly(
|
||||
new StringSegment("--test="), 2, 4
|
||||
);
|
||||
|
||||
Assert.AreEqual("--test=", option.Raw);
|
||||
Assert.AreEqual("--", option.Initiator);
|
||||
Assert.AreEqual("test", option.Name);
|
||||
Assert.AreEqual("=", option.Associator);
|
||||
Assert.IsNull(option.Value);
|
||||
Assert.AreEqual("--test=", argument.Raw);
|
||||
Assert.AreEqual("--", argument.Initiator);
|
||||
Assert.AreEqual("test", argument.Name);
|
||||
Assert.AreEqual("=", argument.Associator);
|
||||
Assert.IsNull(argument.Value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
@ -147,8 +146,8 @@ namespace Nuclex.Support.Parsing {
|
|||
/// </summary>
|
||||
[Test]
|
||||
public void TestValuelessAssociatorRetrievalFromSubstring() {
|
||||
CommandLine.Option option = new CommandLine.Option(
|
||||
new StringSegment("||--test=||", 2, 7), 4, 4//, 9, -1
|
||||
CommandLine.Argument option = CommandLine.Argument.OptionOnly(
|
||||
new StringSegment("||--test=||", 2, 7), 4, 4
|
||||
);
|
||||
|
||||
Assert.AreEqual("--test=", option.Raw);
|
||||
|
@ -158,21 +157,91 @@ namespace Nuclex.Support.Parsing {
|
|||
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);
|
||||
}
|
||||
|
||||
/// <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 OptionTest
|
||||
#endregion // class ArgumentTest
|
||||
|
||||
/// <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>
|
||||
[Test]
|
||||
public void TestParseAmbiguousNameResolution() {
|
||||
CommandLine commandLine = CommandLine.Parse("--:test");
|
||||
|
||||
Assert.AreEqual(0, commandLine.Values.Count);
|
||||
Assert.AreEqual(1, commandLine.Options.Count);
|
||||
Assert.AreEqual("-", commandLine.Options[0].Name);
|
||||
Assert.AreEqual("test", commandLine.Options[0].Value);
|
||||
// Without a name, this is not a valid command line option, so it will
|
||||
// be parsed as a loose value instead.
|
||||
Assert.AreEqual(1, commandLine.Arguments.Count);
|
||||
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>
|
||||
|
@ -183,25 +252,51 @@ namespace Nuclex.Support.Parsing {
|
|||
public void TestParseArgumentInitiatorAtEnd() {
|
||||
CommandLine commandLine = CommandLine.Parse("-hello:-world -");
|
||||
|
||||
Assert.AreEqual(1, commandLine.Values.Count);
|
||||
Assert.AreEqual(1, commandLine.Options.Count);
|
||||
Assert.AreEqual("hello", commandLine.Options[0].Name);
|
||||
Assert.AreEqual("-world", commandLine.Options[0].Value);
|
||||
Assert.AreEqual("-", commandLine.Values[0]);
|
||||
Assert.AreEqual(2, commandLine.Arguments.Count);
|
||||
|
||||
Assert.AreEqual("-hello:-world", commandLine.Arguments[0].Raw);
|
||||
Assert.AreEqual("-", commandLine.Arguments[0].Initiator);
|
||||
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>
|
||||
[Test]
|
||||
public void TestParseQuotedOption() {
|
||||
public void TestParseQuotedValue() {
|
||||
CommandLine commandLine = CommandLine.Parse("hello -world --this -is=\"a test\"");
|
||||
|
||||
Assert.AreEqual(1, commandLine.Values.Count);
|
||||
Assert.AreEqual(3, commandLine.Options.Count);
|
||||
Assert.AreEqual("hello", commandLine.Values[0]);
|
||||
Assert.AreEqual("world", commandLine.Options[0].Name);
|
||||
Assert.AreEqual("this", commandLine.Options[1].Name);
|
||||
Assert.AreEqual("is", commandLine.Options[2].Name);
|
||||
Assert.AreEqual("a test", commandLine.Options[2].Value);
|
||||
Assert.AreEqual(4, 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);
|
||||
|
||||
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>
|
||||
|
@ -209,8 +304,7 @@ namespace Nuclex.Support.Parsing {
|
|||
public void TestParseNull() {
|
||||
CommandLine commandLine = CommandLine.Parse(null);
|
||||
|
||||
Assert.AreEqual(0, commandLine.Values.Count);
|
||||
Assert.AreEqual(0, commandLine.Options.Count);
|
||||
Assert.AreEqual(0, commandLine.Arguments.Count);
|
||||
}
|
||||
|
||||
/// <summary>Validates that a single argument without quotes can be parsed</summary>
|
||||
|
@ -218,9 +312,12 @@ namespace Nuclex.Support.Parsing {
|
|||
public void TestParseSingleNakedValue() {
|
||||
CommandLine commandLine = CommandLine.Parse("hello");
|
||||
|
||||
Assert.AreEqual(1, commandLine.Values.Count);
|
||||
Assert.AreEqual(0, commandLine.Options.Count);
|
||||
Assert.AreEqual("hello", commandLine.Values[0]);
|
||||
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>
|
||||
|
@ -231,9 +328,28 @@ namespace Nuclex.Support.Parsing {
|
|||
public void TestParseQuotedArgumentWithoutClosingQuote() {
|
||||
CommandLine commandLine = CommandLine.Parse("\"Quoted argument");
|
||||
|
||||
Assert.AreEqual(1, commandLine.Values.Count);
|
||||
Assert.AreEqual(0, commandLine.Options.Count);
|
||||
Assert.AreEqual("Quoted argument", commandLine.Values[0]);
|
||||
Assert.AreEqual(1, commandLine.Arguments.Count);
|
||||
Assert.AreEqual("\"Quoted argument", commandLine.Arguments[0].Raw);
|
||||
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>
|
||||
|
@ -243,8 +359,23 @@ namespace Nuclex.Support.Parsing {
|
|||
public void TestParseSpacesOnly() {
|
||||
CommandLine commandLine = CommandLine.Parse(" \t ");
|
||||
|
||||
Assert.AreEqual(0, commandLine.Values.Count);
|
||||
Assert.AreEqual(0, commandLine.Options.Count);
|
||||
Assert.AreEqual(0, commandLine.Arguments.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>
|
||||
|
@ -255,10 +386,19 @@ namespace Nuclex.Support.Parsing {
|
|||
public void TestParseMultipleLoneArgumentInitiators() {
|
||||
CommandLine commandLine = CommandLine.Parse("--- --");
|
||||
|
||||
Assert.AreEqual(0, commandLine.Values.Count);
|
||||
Assert.AreEqual(2, commandLine.Options.Count);
|
||||
Assert.AreEqual("-", commandLine.Options[1].Name);
|
||||
Assert.AreEqual("-", commandLine.Options[2].Name);
|
||||
Assert.AreEqual(2, 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);
|
||||
|
||||
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>
|
||||
|
@ -268,10 +408,19 @@ namespace Nuclex.Support.Parsing {
|
|||
public void TestParseOptionWithEmbeddedInitiator() {
|
||||
CommandLine commandLine = CommandLine.Parse("-hello/world=123 -test-case");
|
||||
|
||||
Assert.AreEqual(0, commandLine.Values.Count);
|
||||
Assert.AreEqual(2, commandLine.Options.Count);
|
||||
Assert.AreEqual("hello/world", commandLine.Options[0].Name);
|
||||
Assert.AreEqual("test-case", commandLine.Options[1].Name);
|
||||
Assert.AreEqual(2, commandLine.Arguments.Count);
|
||||
|
||||
Assert.AreEqual("-hello/world=123", commandLine.Arguments[0].Raw);
|
||||
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>
|
||||
|
@ -281,11 +430,25 @@ namespace Nuclex.Support.Parsing {
|
|||
public void TestParseOptionAndValueWithoutSpaces() {
|
||||
CommandLine commandLine = CommandLine.Parse("\"value\"-option\"value\"");
|
||||
|
||||
Assert.AreEqual(2, commandLine.Values.Count);
|
||||
Assert.AreEqual(1, commandLine.Options.Count);
|
||||
Assert.AreEqual("value", commandLine.Values[0]);
|
||||
Assert.AreEqual("option", commandLine.Options[0].Name);
|
||||
Assert.AreEqual("value", commandLine.Values[1]);
|
||||
Assert.AreEqual(3, commandLine.Arguments.Count);
|
||||
|
||||
Assert.AreEqual("\"value\"", commandLine.Arguments[0].Raw);
|
||||
Assert.IsNull(commandLine.Arguments[0].Initiator);
|
||||
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>
|
||||
|
@ -296,10 +459,19 @@ namespace Nuclex.Support.Parsing {
|
|||
public void TestParseOptionWithModifierAtEnd() {
|
||||
CommandLine commandLine = CommandLine.Parse("--test-value- -test+");
|
||||
|
||||
Assert.AreEqual(0, commandLine.Values.Count);
|
||||
Assert.AreEqual(2, commandLine.Options.Count);
|
||||
Assert.AreEqual("test-value", commandLine.Options[0].Name);
|
||||
Assert.AreEqual("test", commandLine.Options[1].Name);
|
||||
Assert.AreEqual(2, commandLine.Arguments.Count);
|
||||
|
||||
Assert.AreEqual("--test-value-", commandLine.Arguments[0].Raw);
|
||||
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>
|
||||
|
@ -307,14 +479,21 @@ namespace Nuclex.Support.Parsing {
|
|||
/// </summary>
|
||||
[Test]
|
||||
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.Options.Count);
|
||||
Assert.AreEqual("hello", commandLine.Options[0].Name);
|
||||
Assert.AreEqual("123", commandLine.Options[0].Value);
|
||||
Assert.AreEqual("world", commandLine.Options[1].Name);
|
||||
Assert.AreEqual("321", commandLine.Options[1].Value);
|
||||
Assert.AreEqual(2, commandLine.Arguments.Count);
|
||||
|
||||
Assert.AreEqual("-hello:", commandLine.Arguments[0].Raw);
|
||||
Assert.AreEqual("-", commandLine.Arguments[0].Initiator);
|
||||
Assert.AreEqual("hello", commandLine.Arguments[0].Name);
|
||||
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>
|
||||
|
@ -325,9 +504,12 @@ namespace Nuclex.Support.Parsing {
|
|||
public void TestParseOptionAtEndOfString() {
|
||||
CommandLine commandLine = CommandLine.Parse("--test:");
|
||||
|
||||
Assert.AreEqual(0, commandLine.Values.Count);
|
||||
Assert.AreEqual(1, commandLine.Options.Count);
|
||||
Assert.AreEqual("test", commandLine.Options[0].Name);
|
||||
Assert.AreEqual(1, commandLine.Arguments.Count);
|
||||
Assert.AreEqual("--test:", commandLine.Arguments[0].Raw);
|
||||
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>
|
||||
|
@ -338,11 +520,19 @@ namespace Nuclex.Support.Parsing {
|
|||
public void TestWindowsOptionInitiator() {
|
||||
CommandLine commandLine = CommandLine.Parse("/hello //world", true);
|
||||
|
||||
Assert.AreEqual(1, commandLine.Values.Count);
|
||||
Assert.AreEqual(2, commandLine.Options.Count);
|
||||
Assert.AreEqual("hello", commandLine.Options[0].Name);
|
||||
Assert.AreEqual("/", commandLine.Options[0].Value);
|
||||
Assert.AreEqual("world", commandLine.Options[1].Name);
|
||||
Assert.AreEqual(2, commandLine.Arguments.Count);
|
||||
|
||||
Assert.AreEqual("/hello", commandLine.Arguments[0].Raw);
|
||||
Assert.AreEqual("/", commandLine.Arguments[0].Initiator);
|
||||
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>
|
||||
|
@ -353,16 +543,23 @@ namespace Nuclex.Support.Parsing {
|
|||
public void TestNonWindowsOptionValues() {
|
||||
CommandLine commandLine = CommandLine.Parse("/hello //world", false);
|
||||
|
||||
Assert.AreEqual(2, commandLine.Values.Count);
|
||||
Assert.AreEqual(0, commandLine.Options.Count);
|
||||
Assert.AreEqual("/hello", commandLine.Values[0]);
|
||||
Assert.AreEqual("//world", commandLine.Values[1]);
|
||||
Assert.AreEqual(2, 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);
|
||||
|
||||
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
|
||||
|
||||
#endif // ENABLE_BROKEN_COMMAND_LINE_PARSER
|
||||
|
||||
#endif // UNITTEST
|
|
@ -24,13 +24,9 @@ using System.IO;
|
|||
|
||||
using Nuclex.Support.Collections;
|
||||
|
||||
#if ENABLE_BROKEN_COMMAND_LINE_PARSER
|
||||
|
||||
namespace Nuclex.Support.Parsing {
|
||||
|
||||
/// <summary>
|
||||
/// Manages an application's command line parameters
|
||||
/// </summary>
|
||||
/// <summary>Parses and stores an application's command line parameters</summary>
|
||||
/// <remarks>
|
||||
/// <para>
|
||||
/// 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.
|
||||
/// </para>
|
||||
/// <para>
|
||||
/// 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.
|
||||
/// This command line parser does nothing more than represent the command line to
|
||||
/// the application through a convenient interface. It parses a command line and
|
||||
/// extracts the arguments, but doesn't interpret them and or check them for validity.
|
||||
/// </para>
|
||||
/// <para>
|
||||
/// This design promotes simplicity and makes is an ideal building block to create
|
||||
/// actual command line interpreters that connect the parameters to program
|
||||
/// instructions and or fill structures in code.
|
||||
/// </para>
|
||||
/// <para>
|
||||
/// Terminology
|
||||
|
@ -82,18 +78,12 @@ namespace Nuclex.Support.Parsing {
|
|||
/// </item>
|
||||
/// </list>
|
||||
/// </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>
|
||||
public partial class CommandLine {
|
||||
|
||||
/// <summary>Initializes a new command line</summary>
|
||||
public CommandLine() {
|
||||
this.options = new List<Option>();
|
||||
this.values = new List<string>();
|
||||
this.arguments = new List<Argument>();
|
||||
}
|
||||
|
||||
/// <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>
|
||||
/// <param name="value">Value taht will be added</param>
|
||||
internal void addValue(StringSegment value) {
|
||||
/*
|
||||
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>
|
||||
/// <param name="option">Option that will be added</param>
|
||||
internal void addOption(Option option) {
|
||||
Console.WriteLine("Discovered option: '" + option.Raw.ToString() + "'");
|
||||
Console.WriteLine(" Name: '" + option.Name + "'");
|
||||
if(option.Value != null) {
|
||||
Console.WriteLine(" Value: '" + option.Value + "'");
|
||||
/// <summary>Adds an argument to the command line</summary>
|
||||
/// <param name="argument">Argument that will be added</param>
|
||||
internal void addArgument(Argument argument) {
|
||||
/*
|
||||
Console.WriteLine("Discovered option: '" + argument.Raw.ToString() + "'");
|
||||
Console.WriteLine(" Name: '" + argument.Name + "'");
|
||||
if(argument.Value != null) {
|
||||
Console.WriteLine(" Value: '" + argument.Value + "'");
|
||||
}
|
||||
*/
|
||||
|
||||
this.options.Add(option);
|
||||
this.arguments.Add(argument);
|
||||
}
|
||||
|
||||
#endregion // To Be Removed
|
||||
|
||||
/// <summary>Options that were specified on the command line</summary>
|
||||
public IList<Option> Options {
|
||||
get { return this.options; }
|
||||
}
|
||||
|
||||
/// <summary>Loose values that were given on the command line</summary>
|
||||
public IList<string> Values {
|
||||
get { return this.values; }
|
||||
public IList<Argument> Arguments {
|
||||
get { return this.arguments; }
|
||||
}
|
||||
|
||||
/// <summary>Options that were specified on the command line</summary>
|
||||
private List<Option> options;
|
||||
/// <summary>Loose values that were given on the command line</summary>
|
||||
private List<string> values;
|
||||
private List<Argument> arguments;
|
||||
|
||||
}
|
||||
|
||||
} // namespace Nuclex.Support.Parsing
|
||||
|
||||
#endif // ENABLE_BROKEN_COMMAND_LINE_PARSER
|
||||
|
|
Loading…
Reference in New Issue
Block a user