Added a command line parser that extracts command line arguments and assignments for console-based utility programs
git-svn-id: file:///srv/devel/repo-conversion/nusu@50 d2e56fa2-650e-0410-a79f-9358c0239efd
This commit is contained in:
parent
504d96eb0a
commit
93092637cf
|
@ -179,6 +179,15 @@
|
|||
<Name>SimpleRectanglePacker.Test</Name>
|
||||
<DependentUpon>SimpleRectanglePacker.cs</DependentUpon>
|
||||
</Compile>
|
||||
<Compile Include="Source\Parsing\CommandLineParser.cs">
|
||||
<XNAUseContentPipeline>false</XNAUseContentPipeline>
|
||||
<Name>CommandLineParser</Name>
|
||||
</Compile>
|
||||
<Compile Include="Source\Parsing\CommandLineParser.Test.cs">
|
||||
<XNAUseContentPipeline>false</XNAUseContentPipeline>
|
||||
<Name>CommandLineParser.Test</Name>
|
||||
<DependentUpon>CommandLineParser.cs</DependentUpon>
|
||||
</Compile>
|
||||
<Compile Include="Source\PathHelper.cs">
|
||||
<XNAUseContentPipeline>false</XNAUseContentPipeline>
|
||||
<Name>PathHelper</Name>
|
||||
|
@ -308,7 +317,6 @@
|
|||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Folder Include="Documents\" />
|
||||
<Folder Include="Source\Parsing\" />
|
||||
</ItemGroup>
|
||||
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
|
||||
<Import Project="$(MSBuildExtensionsPath)\Microsoft\XNA\Game Studio Express\v1.0\Microsoft.Xna.ContentPipeline.targets" />
|
||||
|
|
59
Source/Parsing/CommandLineParser.Test.cs
Normal file
59
Source/Parsing/CommandLineParser.Test.cs
Normal file
|
@ -0,0 +1,59 @@
|
|||
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 CommandLineParserTest {
|
||||
|
||||
/// <summary>Validates that normal arguments can be parsed</summary>
|
||||
[Test]
|
||||
public void TestPlainArguments() {
|
||||
Assert.AreEqual(
|
||||
true.ToString(),
|
||||
new CommandLineParser(new string[] { "-hello" })["hello"],
|
||||
"Argument with minus sign is recognized"
|
||||
);
|
||||
Assert.AreEqual(
|
||||
true.ToString(),
|
||||
new CommandLineParser(new string[] { "--hello" })["hello"],
|
||||
"Argument with double minus sign is recognized"
|
||||
);
|
||||
Assert.AreEqual(
|
||||
true.ToString(),
|
||||
new CommandLineParser(new string[] { "/hello" })["hello"],
|
||||
"Argument with slash is recognized"
|
||||
);
|
||||
}
|
||||
|
||||
/// <summary>Validates that argument assignments are working</summary>
|
||||
[Test]
|
||||
public void TestAssignments() {
|
||||
Assert.AreEqual(
|
||||
"world",
|
||||
new CommandLineParser(new string[] { "-hello:world" })["hello"],
|
||||
"Argument can be assigned with a double colon"
|
||||
);
|
||||
Assert.AreEqual(
|
||||
"world",
|
||||
new CommandLineParser(new string[] { "-hello=world" })["hello"],
|
||||
"Argument can be assigned with a equality sign"
|
||||
);
|
||||
Assert.AreEqual(
|
||||
"world",
|
||||
new CommandLineParser(new string[] { "-hello", "world" })["hello"],
|
||||
"Argument can be assigned with a space"
|
||||
);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
} // namespace Nuclex.Support.Parsing
|
||||
|
||||
#endif // UNITTEST
|
157
Source/Parsing/CommandLineParser.cs
Normal file
157
Source/Parsing/CommandLineParser.cs
Normal file
|
@ -0,0 +1,157 @@
|
|||
#region CPL License
|
||||
/*
|
||||
Nuclex Framework
|
||||
Copyright (C) 2002-2007 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.Collections.Specialized;
|
||||
using System.Text.RegularExpressions;
|
||||
|
||||
namespace Nuclex.Support.Parsing {
|
||||
|
||||
/// <summary>Parses an application's command line</summary>
|
||||
/// <remarks>
|
||||
/// <para>
|
||||
/// Based on an article Richard Lopes published on "The Code Project" at
|
||||
/// http://www.codeproject.com/csharp/command_line.asp
|
||||
/// </para>
|
||||
/// <para>
|
||||
/// Valid forms for command line arguments: {-|/|--}param[{ |=|:}[{"|'}]value[{"|'}]]
|
||||
/// </para>
|
||||
/// <example>
|
||||
/// <code>
|
||||
/// -param1 value1
|
||||
/// --param2
|
||||
/// /param3:"Test-:-work"
|
||||
/// /param4=happy
|
||||
/// -param5 '--=nice=--'
|
||||
/// </code>
|
||||
/// </example>
|
||||
/// </remarks>
|
||||
public class CommandLineParser {
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new command line parser using the running program's command line
|
||||
/// </summary>
|
||||
public CommandLineParser() : this(System.Environment.CommandLine) { }
|
||||
|
||||
/// <summary>Initializes a new command line parser</summary>
|
||||
/// <param name="arguments">All supplied command line arguments as a single string</param>
|
||||
public CommandLineParser(string arguments)
|
||||
: this(arguments.Split(new char[] { ' ', '\t' })) { }
|
||||
|
||||
/// <summary>Initializes a new command line parser</summary>
|
||||
/// <param name="arguments">Arguments that have been passed in the command line</param>
|
||||
public CommandLineParser(string[] arguments) {
|
||||
this.arguments = new StringDictionary();
|
||||
|
||||
string activeParameter = null;
|
||||
|
||||
foreach(string argument in arguments) {
|
||||
|
||||
// Look for arguments ('-', '/', '--') with their assignments ('=', ':')
|
||||
string[] parts = splitter.Split(argument, 3);
|
||||
switch(parts.Length) {
|
||||
|
||||
// Value found without an argument being specified (eg. file name)
|
||||
case 1: {
|
||||
|
||||
if(activeParameter != null) {
|
||||
if(!this.arguments.ContainsKey(activeParameter)) {
|
||||
parts[0] = remover.Replace(parts[0], "$1");
|
||||
this.arguments.Add(activeParameter, parts[0]);
|
||||
}
|
||||
activeParameter = null;
|
||||
}
|
||||
|
||||
// Error: No argument is waiting for a value. Skip this argument.
|
||||
break;
|
||||
}
|
||||
|
||||
// Found an argument with no value assignment
|
||||
case 2: {
|
||||
|
||||
// In case the previous argument is still waiting for a value we need to finish
|
||||
// it up before switching to the argument we just found.
|
||||
if(activeParameter != null)
|
||||
if(!this.arguments.ContainsKey(activeParameter))
|
||||
this.arguments.Add(activeParameter, true.ToString());
|
||||
|
||||
// Remember argument to allow for a later value assignment
|
||||
activeParameter = parts[1];
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
// Found an argument with a proper assignment declaration
|
||||
case 3: {
|
||||
|
||||
// In case the previous argument is still waiting for a value we need to finish
|
||||
// it up before switching to the argument we just found.
|
||||
if(activeParameter != null)
|
||||
if(!this.arguments.ContainsKey(activeParameter))
|
||||
this.arguments.Add(activeParameter, true.ToString());
|
||||
|
||||
activeParameter = parts[1];
|
||||
|
||||
// Remove any quotes that might be enclosing this argument (",')
|
||||
if(!this.arguments.ContainsKey(activeParameter)) {
|
||||
parts[2] = remover.Replace(parts[2], "$1");
|
||||
this.arguments.Add(activeParameter, parts[2]);
|
||||
}
|
||||
|
||||
activeParameter = null;
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// In case the previous argument is still waiting for a value we need to finish
|
||||
// it up before leaving the parsing method.
|
||||
if(activeParameter != null) {
|
||||
if(!this.arguments.ContainsKey(activeParameter)) {
|
||||
this.arguments.Add(activeParameter, true.ToString());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>Returns the value of an argument by the argument's name</summary>
|
||||
/// <param name="argumentName">Name of the argument whose value will be returned</param>
|
||||
/// <returns>The value of the argument with the specified name</returns>
|
||||
public string this[string argumentName] {
|
||||
get { return this.arguments[argumentName]; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Regular Expression used to split the arguments and their assigned values
|
||||
/// </summary>
|
||||
private static Regex splitter =
|
||||
new Regex(@"^-{1,2}|^/|=|:", RegexOptions.IgnoreCase | RegexOptions.Compiled);
|
||||
|
||||
/// <summary>
|
||||
/// Regular Expression used to remove quotations around an argument's value
|
||||
/// </summary>
|
||||
private static Regex remover =
|
||||
new Regex(@"^['""]?(.*?)['""]?$", RegexOptions.IgnoreCase | RegexOptions.Compiled);
|
||||
|
||||
/// <summary>Stores the parsed arguments</summary>
|
||||
private StringDictionary arguments;
|
||||
|
||||
}
|
||||
|
||||
} // namespace Nuclex.Support.Parsing
|
Loading…
Reference in New Issue
Block a user