From fd6ed3ca72c2e99f95f521a47c97eb73d2884129 Mon Sep 17 00:00:00 2001 From: Alexey Golub Date: Sat, 8 Jun 2019 23:50:56 +0300 Subject: [PATCH] Add support for stacked options followed by a value --- CliFx.Tests/CommandOptionParserTests.cs | 13 +++++++++++-- CliFx.Tests/DummyTests.cs | 8 ++++---- CliFx/Internal/Extensions.cs | 5 +---- CliFx/Services/CommandOptionParser.cs | 4 ++-- 4 files changed, 18 insertions(+), 12 deletions(-) diff --git a/CliFx.Tests/CommandOptionParserTests.cs b/CliFx.Tests/CommandOptionParserTests.cs index d986069..d4d33a8 100644 --- a/CliFx.Tests/CommandOptionParserTests.cs +++ b/CliFx.Tests/CommandOptionParserTests.cs @@ -98,6 +98,15 @@ namespace CliFx.Tests }) ); + yield return new TestCaseData( + new[] {"-ab", "value"}, + new CommandOptionSet(new Dictionary + { + {"a", null}, + {"b", "value"} + }) + ); + yield return new TestCaseData( new[] {"command"}, new CommandOptionSet("command") @@ -123,8 +132,8 @@ namespace CliFx.Tests var optionSet = parser.ParseOptions(commandLineArguments); // Assert - Assert.That(optionSet.CommandName, Is.EqualTo(expectedCommandOptionSet.CommandName), "Command name"); - Assert.That(optionSet.Options, Is.EqualTo(expectedCommandOptionSet.Options), "Options"); + Assert.That(optionSet.CommandName, Is.EqualTo(expectedCommandOptionSet.CommandName), nameof(optionSet.CommandName)); + Assert.That(optionSet.Options, Is.EqualTo(expectedCommandOptionSet.Options), nameof(optionSet.Options)); } } } \ No newline at end of file diff --git a/CliFx.Tests/DummyTests.cs b/CliFx.Tests/DummyTests.cs index 0a434bc..6277d88 100644 --- a/CliFx.Tests/DummyTests.cs +++ b/CliFx.Tests/DummyTests.cs @@ -8,7 +8,7 @@ namespace CliFx.Tests [TestFixture] public class DummyTests { - private string DummyFilePath => Path.Combine(TestContext.CurrentContext.TestDirectory, "CliFx.Tests.Dummy.exe"); + private static string DummyFilePath => Path.Combine(TestContext.CurrentContext.TestDirectory, "CliFx.Tests.Dummy.exe"); [Test] [TestCase("", "Hello world")] @@ -24,9 +24,9 @@ namespace CliFx.Tests var result = await Cli.Wrap(DummyFilePath).SetArguments(arguments).ExecuteAsync(); // Assert - Assert.That(result.ExitCode, Is.Zero, "Exit code"); - Assert.That(result.StandardOutput.Trim(), Is.EqualTo(expectedOutput), "Stdout"); - Assert.That(result.StandardError.Trim(), Is.Empty, "Stderr"); + Assert.That(result.ExitCode, Is.Zero, nameof(result.ExitCode)); + Assert.That(result.StandardOutput.Trim(), Is.EqualTo(expectedOutput), nameof(result.StandardOutput)); + Assert.That(result.StandardError.Trim(), Is.Empty, nameof(result.StandardError)); } } } \ No newline at end of file diff --git a/CliFx/Internal/Extensions.cs b/CliFx/Internal/Extensions.cs index ca9bc2a..73c7020 100644 --- a/CliFx/Internal/Extensions.cs +++ b/CliFx/Internal/Extensions.cs @@ -42,13 +42,10 @@ namespace CliFx.Internal public static bool IsDerivedFrom(this Type type, Type baseType) { - var currentType = type; - while (currentType != null) + for (var currentType = type; currentType != null; currentType = currentType.BaseType) { if (currentType == baseType) return true; - - currentType = currentType.BaseType; } return false; diff --git a/CliFx/Services/CommandOptionParser.cs b/CliFx/Services/CommandOptionParser.cs index ada4964..b555c65 100644 --- a/CliFx/Services/CommandOptionParser.cs +++ b/CliFx/Services/CommandOptionParser.cs @@ -42,10 +42,10 @@ namespace CliFx.Services // Multiple stacked short options else if (commandLineArgument.StartsWith("-", StringComparison.OrdinalIgnoreCase)) { - optionName = null; foreach (var c in commandLineArgument.Substring(1)) { - options[c.ToString(CultureInfo.InvariantCulture)] = null; + optionName = c.ToString(CultureInfo.InvariantCulture); + options[optionName] = null; } }