Add support for stacked options followed by a value

This commit is contained in:
Alexey Golub
2019-06-08 23:50:56 +03:00
parent 3a9ac3d36c
commit fd6ed3ca72
4 changed files with 18 additions and 12 deletions

View File

@@ -98,6 +98,15 @@ namespace CliFx.Tests
}) })
); );
yield return new TestCaseData(
new[] {"-ab", "value"},
new CommandOptionSet(new Dictionary<string, string>
{
{"a", null},
{"b", "value"}
})
);
yield return new TestCaseData( yield return new TestCaseData(
new[] {"command"}, new[] {"command"},
new CommandOptionSet("command") new CommandOptionSet("command")
@@ -123,8 +132,8 @@ namespace CliFx.Tests
var optionSet = parser.ParseOptions(commandLineArguments); var optionSet = parser.ParseOptions(commandLineArguments);
// Assert // Assert
Assert.That(optionSet.CommandName, Is.EqualTo(expectedCommandOptionSet.CommandName), "Command name"); Assert.That(optionSet.CommandName, Is.EqualTo(expectedCommandOptionSet.CommandName), nameof(optionSet.CommandName));
Assert.That(optionSet.Options, Is.EqualTo(expectedCommandOptionSet.Options), "Options"); Assert.That(optionSet.Options, Is.EqualTo(expectedCommandOptionSet.Options), nameof(optionSet.Options));
} }
} }
} }

View File

@@ -8,7 +8,7 @@ namespace CliFx.Tests
[TestFixture] [TestFixture]
public class DummyTests 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] [Test]
[TestCase("", "Hello world")] [TestCase("", "Hello world")]
@@ -24,9 +24,9 @@ namespace CliFx.Tests
var result = await Cli.Wrap(DummyFilePath).SetArguments(arguments).ExecuteAsync(); var result = await Cli.Wrap(DummyFilePath).SetArguments(arguments).ExecuteAsync();
// Assert // Assert
Assert.That(result.ExitCode, Is.Zero, "Exit code"); Assert.That(result.ExitCode, Is.Zero, nameof(result.ExitCode));
Assert.That(result.StandardOutput.Trim(), Is.EqualTo(expectedOutput), "Stdout"); Assert.That(result.StandardOutput.Trim(), Is.EqualTo(expectedOutput), nameof(result.StandardOutput));
Assert.That(result.StandardError.Trim(), Is.Empty, "Stderr"); Assert.That(result.StandardError.Trim(), Is.Empty, nameof(result.StandardError));
} }
} }
} }

View File

@@ -42,13 +42,10 @@ namespace CliFx.Internal
public static bool IsDerivedFrom(this Type type, Type baseType) public static bool IsDerivedFrom(this Type type, Type baseType)
{ {
var currentType = type; for (var currentType = type; currentType != null; currentType = currentType.BaseType)
while (currentType != null)
{ {
if (currentType == baseType) if (currentType == baseType)
return true; return true;
currentType = currentType.BaseType;
} }
return false; return false;

View File

@@ -42,10 +42,10 @@ namespace CliFx.Services
// Multiple stacked short options // Multiple stacked short options
else if (commandLineArgument.StartsWith("-", StringComparison.OrdinalIgnoreCase)) else if (commandLineArgument.StartsWith("-", StringComparison.OrdinalIgnoreCase))
{ {
optionName = null;
foreach (var c in commandLineArgument.Substring(1)) foreach (var c in commandLineArgument.Substring(1))
{ {
options[c.ToString(CultureInfo.InvariantCulture)] = null; optionName = c.ToString(CultureInfo.InvariantCulture);
options[optionName] = null;
} }
} }