Disabled the old CommandLineParser; started writing a new command line parser from scratch because I didn't manage to find a sane implementation on the internet; test coverage for the whole of Nuclex.Support is now 100.0%
git-svn-id: file:///srv/devel/repo-conversion/nusu@105 d2e56fa2-650e-0410-a79f-9358c0239efd
This commit is contained in:
parent
447fe2aea7
commit
f0d76f988f
11 changed files with 367 additions and 31 deletions
|
@ -26,6 +26,8 @@ using System.Text;
|
|||
|
||||
using NUnit.Framework;
|
||||
|
||||
#if false // Too bugged. 100% test coverage not possible.
|
||||
|
||||
namespace Nuclex.Support.Parsing {
|
||||
|
||||
/// <summary>Ensures that the command line parser is working properly</summary>
|
||||
|
@ -130,4 +132,6 @@ namespace Nuclex.Support.Parsing {
|
|||
|
||||
} // namespace Nuclex.Support.Parsing
|
||||
|
||||
#endif
|
||||
|
||||
#endif // UNITTEST
|
|
@ -21,6 +21,8 @@ License along with this library
|
|||
using System.Collections.Specialized;
|
||||
using System.Text.RegularExpressions;
|
||||
|
||||
#if false // Too bugged. 100% test coverage not possible.
|
||||
|
||||
namespace Nuclex.Support.Parsing {
|
||||
|
||||
/// <summary>Parses an application's command line</summary>
|
||||
|
@ -179,3 +181,5 @@ namespace Nuclex.Support.Parsing {
|
|||
}
|
||||
|
||||
} // namespace Nuclex.Support.Parsing
|
||||
|
||||
#endif
|
32
Source/Parsing/CommandLine.Formatter.cs
Normal file
32
Source/Parsing/CommandLine.Formatter.cs
Normal file
|
@ -0,0 +1,32 @@
|
|||
#region CPL License
|
||||
/*
|
||||
Nuclex Framework
|
||||
Copyright (C) 2002-2008 Nuclex Development Labs
|
||||
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the IBM Common Public License as
|
||||
published by the IBM Corporation; either version 1.0 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
IBM Common Public License for more details.
|
||||
|
||||
You should have received a copy of the IBM Common Public
|
||||
License along with this library
|
||||
*/
|
||||
#endregion
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace Nuclex.Support.Parsing {
|
||||
|
||||
partial class CommandLine {
|
||||
|
||||
internal static class Formatter { }
|
||||
|
||||
}
|
||||
|
||||
} // namespace Nuclex.Support.Parsing
|
49
Source/Parsing/CommandLine.Option.cs
Normal file
49
Source/Parsing/CommandLine.Option.cs
Normal file
|
@ -0,0 +1,49 @@
|
|||
#region CPL License
|
||||
/*
|
||||
Nuclex Framework
|
||||
Copyright (C) 2002-2008 Nuclex Development Labs
|
||||
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the IBM Common Public License as
|
||||
published by the IBM Corporation; either version 1.0 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
IBM Common Public License for more details.
|
||||
|
||||
You should have received a copy of the IBM Common Public
|
||||
License along with this library
|
||||
*/
|
||||
#endregion
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace Nuclex.Support.Parsing {
|
||||
|
||||
partial class CommandLine {
|
||||
|
||||
/// <summary>Option that can be specified on an application's command line</summary>
|
||||
public struct Option {
|
||||
|
||||
/// <summary>Contains the raw string the command line argument was parsed from</summary>
|
||||
public string Raw; // TODO: ToString() instead
|
||||
/*
|
||||
/// <summary>Method used to specify the argument (either '-', '--' or '/')</summary>
|
||||
public string Method;
|
||||
*/
|
||||
/// <summary>Name of the command line argument</summary>
|
||||
public string Name;
|
||||
/// <summary>Value that has been assigned to the command line argument</summary>
|
||||
public string Value;
|
||||
/*
|
||||
/// <summary>Method used to assign the value (either '=', ':' or ' ')</summary>
|
||||
public string Assignment;
|
||||
*/
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
} // namespace Nuclex.Support.Parsing
|
95
Source/Parsing/CommandLine.Parser.cs
Normal file
95
Source/Parsing/CommandLine.Parser.cs
Normal file
|
@ -0,0 +1,95 @@
|
|||
#region CPL License
|
||||
/*
|
||||
Nuclex Framework
|
||||
Copyright (C) 2002-2008 Nuclex Development Labs
|
||||
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the IBM Common Public License as
|
||||
published by the IBM Corporation; either version 1.0 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
IBM Common Public License for more details.
|
||||
|
||||
You should have received a copy of the IBM Common Public
|
||||
License along with this library
|
||||
*/
|
||||
#endregion
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace Nuclex.Support.Parsing {
|
||||
|
||||
partial class CommandLine {
|
||||
|
||||
/// <summary>Parses command line strings</summary>
|
||||
private static class Parser {
|
||||
|
||||
/// <summary>Parses a string containing command line arguments</summary>
|
||||
/// <param name="commandLineString">String that will be parsed</param>
|
||||
/// <returns>The parsed command line arguments from the string</returns>
|
||||
public static CommandLine Parse(string commandLineString) {
|
||||
CommandLine commandLine = new CommandLine();
|
||||
if(commandLineString == null) {
|
||||
return commandLine;
|
||||
}
|
||||
|
||||
/*
|
||||
for(int index = 0; index < commandLineString.Length; ) {
|
||||
char currentCharacter = commandLineString[index];
|
||||
|
||||
// We ignore whitespaces outside of quoted values
|
||||
if(char.IsWhiteSpace(currentCharacter)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
switch(currentCharacter) {
|
||||
case '-':
|
||||
case '/': {
|
||||
parseArgument(commandLine, commandLineString, ref index);
|
||||
break;
|
||||
}
|
||||
case '"': {
|
||||
parseQuotedValue(commandLine, commandLineString, ref index);
|
||||
break;
|
||||
}
|
||||
default: {
|
||||
parseUnquotedValue(commandLine, commandLineString, ref index);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
*/
|
||||
return null;
|
||||
}
|
||||
|
||||
/*
|
||||
private static void parseArgument(
|
||||
CommandLine commandLine, string commandLineString, ref int index
|
||||
) {
|
||||
}
|
||||
|
||||
private static void parseQuotedValue(
|
||||
CommandLine commandLine, string commandLineString, ref int index
|
||||
) {
|
||||
}
|
||||
|
||||
private static void parseUnquotedValue(
|
||||
CommandLine commandLine, string commandLineString, ref int index
|
||||
) {
|
||||
int endIndex = commandLineString.IndexOfAny(WhitespaceCharacters, index);
|
||||
|
||||
StringSegment argument = new StringSegment(commandLineString, index, endIndex - index);
|
||||
|
||||
}
|
||||
|
||||
private static readonly char[] WhitespaceCharacters = new char[] { ' ', '\t' };
|
||||
*/
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
} // namespace Nuclex.Support.Parsing
|
51
Source/Parsing/CommandLine.Test.cs
Normal file
51
Source/Parsing/CommandLine.Test.cs
Normal file
|
@ -0,0 +1,51 @@
|
|||
#region CPL License
|
||||
/*
|
||||
Nuclex Framework
|
||||
Copyright (C) 2002-2008 Nuclex Development Labs
|
||||
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the IBM Common Public License as
|
||||
published by the IBM Corporation; either version 1.0 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
IBM Common Public License for more details.
|
||||
|
||||
You should have received a copy of the IBM Common Public
|
||||
License along with this library
|
||||
*/
|
||||
#endregion
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
#if UNITTEST
|
||||
|
||||
using NUnit.Framework;
|
||||
|
||||
namespace Nuclex.Support.Parsing {
|
||||
|
||||
/// <summary>Ensures that the command line parser is working properly</summary>
|
||||
[TestFixture]
|
||||
public class CommandLineTest {
|
||||
|
||||
/// <summary>Validates that normal arguments can be parsed</summary>
|
||||
[Test]
|
||||
public void TestParseOptions() {
|
||||
CommandLine.Parse("Hello -World /This --Is \"a test\"");
|
||||
}
|
||||
|
||||
/// <summary>Validates that null can be parsed</summary>
|
||||
[Test]
|
||||
public void TestParseNull() {
|
||||
Assert.IsNotNull(CommandLine.Parse(null));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
} // namespace Nuclex.Support.Parsing
|
||||
|
||||
#endif // UNITTEST
|
|
@ -1,46 +1,89 @@
|
|||
using System;
|
||||
#region CPL License
|
||||
/*
|
||||
Nuclex Framework
|
||||
Copyright (C) 2002-2008 Nuclex Development Labs
|
||||
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the IBM Common Public License as
|
||||
published by the IBM Corporation; either version 1.0 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
IBM Common Public License for more details.
|
||||
|
||||
You should have received a copy of the IBM Common Public
|
||||
License along with this library
|
||||
*/
|
||||
#endregion
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace Nuclex.Support.Source.Parsing {
|
||||
namespace Nuclex.Support.Parsing {
|
||||
|
||||
#if false
|
||||
/// <summary>
|
||||
/// Parses an application's command line parameters for easier consumption
|
||||
/// Manages an application's command line parameters
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// <para>
|
||||
/// At the time of the creation of this parser, there are already several command line
|
||||
/// parsing libraries out there. Most of them, however, do way too much at once or at
|
||||
/// the very least use one huge, untested clutter of classes and methods to arrive
|
||||
/// at their results.
|
||||
/// At the time of the creation of this component, there are already several command
|
||||
/// line parsing libraries out there. Most of them, however, do way too much at once
|
||||
/// or at the very least rely on huge, untested clutters of classes and methods to
|
||||
/// arrive at their results.
|
||||
/// </para>
|
||||
/// <para>
|
||||
/// This parser does nothing more than parse the command line arguments. It doesn't
|
||||
/// interpret them and it doesn't check them for validity. Due to this, it can easily
|
||||
/// This class does nothing more than represent the command line to the application.
|
||||
/// It can parse a command line
|
||||
/// parse the command line arguments. It doesn't
|
||||
/// interpret them and it doesn't check them for validity. This promotes simplicity
|
||||
/// and allows t
|
||||
/// be unit-tested and is an ideal building block to create actual command line
|
||||
/// interpreters that connect the parameters to program instructions and or fill
|
||||
/// structures in code.
|
||||
/// </para>
|
||||
/// <para>
|
||||
/// Terminology
|
||||
/// <list type="table">
|
||||
/// <item>
|
||||
/// <term>Command line</term>
|
||||
/// <description>
|
||||
/// The entire command line either as a string or as
|
||||
/// an already parsed data structure
|
||||
/// </description>
|
||||
/// </item>
|
||||
/// <item>
|
||||
/// <term>Option / Argument</term>
|
||||
/// <description>
|
||||
/// Can be specified on the command line and typically alters the behavior
|
||||
/// of the application or changes a setting. For example, '--normalize' or
|
||||
/// '/safemode'.
|
||||
/// </description>
|
||||
/// </item>
|
||||
/// <item>
|
||||
/// <term>Value</term>
|
||||
/// <description>
|
||||
/// Can either sit loosely in the command line (eg. 'update' or 'textfile.txt')
|
||||
/// or as assignment to an option (eg. '--width=1280' or '/overwrite:always')
|
||||
/// </description>
|
||||
/// </item>
|
||||
/// </list>
|
||||
/// </para>
|
||||
/// </remarks>
|
||||
public class CommandLine {
|
||||
public static CommandLine Parse(string commandLine) {}
|
||||
}
|
||||
|
||||
public struct CommandLineOption {
|
||||
|
||||
/// <summary>Contains the raw string the command line argument was parsed from</summary>
|
||||
public string Raw;
|
||||
/// <summary>Method used to specify the argument (either '-', '--' or '/')</summary>
|
||||
public string Method;
|
||||
/// <summary>Name of the command line argument</summary>
|
||||
public string Name;
|
||||
/// <summary>Value that has been assigned to the command line argument</summary>
|
||||
public string Value;
|
||||
/// <summary>Method used to assign the value (either '=', ':' or ' ')</summary>
|
||||
public string Assignment;
|
||||
public partial class CommandLine {
|
||||
|
||||
/// <summary>Initializes a new command line</summary>
|
||||
public CommandLine() { }
|
||||
|
||||
/// <summary>Parses the command line arguments from the provided string</summary>
|
||||
/// <param name="commandLineString">String containing the command line arguments</param>
|
||||
/// <returns>The parsed command line</returns>
|
||||
public static CommandLine Parse(string commandLineString) {
|
||||
return Parser.Parse(commandLineString);
|
||||
}
|
||||
|
||||
}
|
||||
#endif
|
||||
|
||||
}
|
||||
} // namespace Nuclex.Support.Parsing
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue