Skip to content

Commit 9b63793

Browse files
committed
enum support
1 parent 23cba7b commit 9b63793

File tree

4 files changed

+111
-30
lines changed

4 files changed

+111
-30
lines changed

FluentCommandLineParser.Tests/FluentCommandLineParserTests.cs

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -289,47 +289,47 @@ public void Ensure_Negative_Double_Can_Be_Specified_With_Unix_Style()
289289

290290
#region Enum Option
291291

292-
//enum TestEnum
293-
//{
294-
// Value0 = 0,
295-
// Value1 = 1
296-
//}
292+
enum TestEnum
293+
{
294+
Value0 = 0,
295+
Value1 = 1
296+
}
297297

298-
//[Test]
299-
//public void Ensure_Parser_Calls_The_Callback_With_Expected_Enum_When_Using_Short_option()
300-
//{
301-
// const TestEnum expected = TestEnum.Value1;
298+
[Test]
299+
public void Ensure_Parser_Calls_The_Callback_With_Expected_Enum_When_Using_Short_option()
300+
{
301+
const TestEnum expected = TestEnum.Value1;
302302

303-
// TestEnum actual = TestEnum.Value0;
303+
TestEnum actual = TestEnum.Value0;
304304

305-
// IFluentCommandLineParser parser = new FluentCommandLineParser();
305+
var parser = CreateFluentParser();
306306

307-
// parser
308-
// .Setup<TestEnum>("e")
309-
// .Callback(val => actual = val);
307+
parser
308+
.Setup<TestEnum>('e')
309+
.Callback(val => actual = val);
310310

311-
// parser.Parse(new[] { "-e", expected.ToString() });
311+
parser.Parse(new[] { "-e", expected.ToString() });
312312

313-
// Assert.AreEqual(expected, actual);
314-
//}
313+
Assert.AreEqual(expected, actual);
314+
}
315315

316-
//[Test]
317-
//public void Ensure_Parser_Calls_The_Callback_With_Expected_Enum_When_Using_Long_option()
318-
//{
319-
// const TestEnum expected = TestEnum.Value1;
316+
[Test]
317+
public void Ensure_Parser_Calls_The_Callback_With_Expected_Enum_When_Using_Long_option()
318+
{
319+
const TestEnum expected = TestEnum.Value1;
320320

321-
// TestEnum actual = TestEnum.Value0;
321+
TestEnum actual = TestEnum.Value0;
322322

323-
// IFluentCommandLineParser parser = new FluentCommandLineParser();
323+
var parser = CreateFluentParser();
324324

325-
// parser
326-
// .Setup<TestEnum>("e", "enum")
327-
// .Callback(val => actual = val);
325+
parser
326+
.Setup<TestEnum>("e", "enum")
327+
.Callback(val => actual = val);
328328

329-
// parser.Parse(new[] { "--enum", expected.ToString() });
329+
parser.Parse(new[] { "--enum", expected.ToString() });
330330

331-
// Assert.AreEqual(expected, actual);
332-
//}
331+
Assert.AreEqual(expected, actual);
332+
}
333333

334334
#endregion Enum Option
335335

FluentCommandLineParser/FluentCommandLineParser.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@
9090
<Compile Include="Internals\Parsing\OptionParsers\CommandLineOptionParserFactory.cs" />
9191
<Compile Include="Internals\Parsing\OptionParsers\DateTimeCommandLineOptionParser.cs" />
9292
<Compile Include="Internals\Parsing\OptionParsers\DoubleCommandLineOptionParser.cs" />
93+
<Compile Include="Internals\Parsing\OptionParsers\EnumCommandLineOptionParser.cs" />
9394
<Compile Include="Internals\Parsing\OptionParsers\ICommandLineOptionParser.cs" />
9495
<Compile Include="Internals\Parsing\OptionParsers\Int32CommandLineOptionParser.cs" />
9596
<Compile Include="Internals\Parsing\OptionParsers\ListCommandLineOptionParser.cs" />

FluentCommandLineParser/Internals/Parsing/OptionParsers/CommandLineOptionParserFactory.cs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,20 @@ public ICommandLineOptionParser<T> CreateParser<T>()
8181
{
8282
var type = typeof(T);
8383

84-
if (!this.Parsers.ContainsKey(type)) throw new UnsupportedTypeException();
84+
if (!this.Parsers.ContainsKey(type))
85+
{
86+
if (! typeof(T).IsEnum)
87+
{
88+
throw new UnsupportedTypeException();
89+
}
90+
91+
type = typeof(EnumCommandLineOptionParser<T>);
92+
if (!this.Parsers.ContainsKey(type))
93+
{
94+
this.AddOrReplace(new EnumCommandLineOptionParser<T>());
95+
}
96+
type = typeof(T);
97+
}
8598

8699
return (ICommandLineOptionParser<T>)this.Parsers[type];
87100
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
#region License
2+
// BoolCommandLineOptionParser.cs
3+
// Copyright (c) 2013, Simon Williams
4+
// All rights reserved.
5+
//
6+
// Redistribution and use in source and binary forms, with or without modification, are permitted provide
7+
// d that the following conditions are met:
8+
//
9+
// Redistributions of source code must retain the above copyright notice, this list of conditions and the
10+
// following disclaimer.
11+
//
12+
// Redistributions in binary form must reproduce the above copyright notice, this list of conditions and
13+
// the following disclaimer in the documentation and/or other materials provided with the distribution.
14+
//
15+
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
16+
// WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
17+
// PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
18+
// ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
19+
// TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20+
// HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
21+
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
22+
// POSSIBILITY OF SUCH DAMAGE.
23+
#endregion
24+
25+
using System;
26+
using System.Linq;
27+
using Fclp.Internals.Extensions;
28+
29+
namespace Fclp.Internals.Parsing.OptionParsers
30+
{
31+
/// <summary>
32+
/// Parser used to convert to <see cref="Enum"/>.
33+
/// </summary>
34+
/// <remarks>For <see cref="System.Boolean"/> types the value is optional. If no value is provided for the Option then <c>true</c> is returned.</remarks>
35+
public class EnumCommandLineOptionParser<T> : ICommandLineOptionParser<T>
36+
{
37+
/// <summary>
38+
/// Parses the specified <see cref="System.String"/> into a <see cref="System.Boolean"/>.
39+
/// </summary>
40+
/// <param name="parsedOption"></param>
41+
/// <returns>
42+
/// A <see cref="System.Boolean"/> representing the parsed value.
43+
/// The value is optional. If no value is provided then <c>true</c> is returned.
44+
/// </returns>
45+
public T Parse(ParsedOption parsedOption)
46+
{
47+
return (T)Enum.Parse(typeof(T), parsedOption.Value);
48+
}
49+
50+
/// <summary>
51+
/// Determines whether the specified <see cref="System.String"/> can be parsed by this <see cref="ICommandLineOptionParser{T}"/>.
52+
/// </summary>
53+
/// <param name="parsedOption"></param>
54+
/// <returns><c>true</c> if the specified <see cref="System.String"/> can be parsed by this <see cref="ICommandLineOptionParser{T}"/>; otherwise <c>false</c>.</returns>
55+
public bool CanParse(ParsedOption parsedOption)
56+
{
57+
if (parsedOption.Value.IsNullOrWhiteSpace()) return false;
58+
if (parsedOption.HasValue == false) return false;
59+
60+
string value = parsedOption.Value.Trim();
61+
62+
var items = value.SplitOnWhitespace();
63+
64+
return items.Count() == 1;
65+
}
66+
}
67+
}

0 commit comments

Comments
 (0)