mirror of
https://github.com/Tyrrrz/CliFx.git
synced 2025-10-25 15:19:17 +00:00
Add support for space-separated command names in input parser
This enables multi-level subcommands Closes #2
This commit is contained in:
@@ -22,7 +22,7 @@ namespace CliFx.Tests
|
||||
public Task ExecuteAsync(CommandContext context) => Task.CompletedTask;
|
||||
}
|
||||
|
||||
[Command("faulty-command")]
|
||||
[Command("faulty command")]
|
||||
private class TestFaultyCommand : ICommand
|
||||
{
|
||||
public Task ExecuteAsync(CommandContext context) => Task.FromException(new CommandErrorException(-1337));
|
||||
@@ -115,17 +115,17 @@ namespace CliFx.Tests
|
||||
|
||||
yield return new TestCaseData(
|
||||
new[] {typeof(TestFaultyCommand)},
|
||||
new[] {"faulty-command", "--help"}
|
||||
new[] {"faulty", "command", "--help"}
|
||||
);
|
||||
|
||||
yield return new TestCaseData(
|
||||
new[] {typeof(TestFaultyCommand)},
|
||||
new[] {"faulty-command", "-h"}
|
||||
new[] {"faulty", "command", "-h"}
|
||||
);
|
||||
|
||||
yield return new TestCaseData(
|
||||
new[] {typeof(TestFaultyCommand)},
|
||||
new[] {"faulty-command", "-?"}
|
||||
new[] {"faulty", "command", "-?"}
|
||||
);
|
||||
}
|
||||
|
||||
@@ -165,7 +165,7 @@ namespace CliFx.Tests
|
||||
|
||||
yield return new TestCaseData(
|
||||
new Type[0],
|
||||
new[] {"faulty-command"}
|
||||
new[] {"faulty", "command"}
|
||||
);
|
||||
|
||||
// Specified command is not defined
|
||||
@@ -194,7 +194,7 @@ namespace CliFx.Tests
|
||||
|
||||
yield return new TestCaseData(
|
||||
new[] {typeof(TestFaultyCommand)},
|
||||
new[] {"faulty-command"}
|
||||
new[] {"faulty", "command"}
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
using CliFx.Attributes;
|
||||
using CliFx.Models;
|
||||
using CliFx.Services;
|
||||
using NUnit.Framework;
|
||||
@@ -9,6 +10,7 @@ namespace CliFx.Tests
|
||||
{
|
||||
public partial class CommandFactoryTests
|
||||
{
|
||||
[Command]
|
||||
private class TestCommand : ICommand
|
||||
{
|
||||
public Task ExecuteAsync(CommandContext context) => throw new NotImplementedException();
|
||||
|
||||
@@ -11,6 +11,7 @@ namespace CliFx.Tests
|
||||
{
|
||||
public partial class CommandInitializerTests
|
||||
{
|
||||
[Command]
|
||||
public class TestCommand : ICommand
|
||||
{
|
||||
[CommandOption("int", 'i', IsRequired = true)]
|
||||
|
||||
@@ -151,6 +151,11 @@ namespace CliFx.Tests
|
||||
new CommandOptionInput("option", "value")
|
||||
})
|
||||
);
|
||||
|
||||
yield return new TestCaseData(
|
||||
new[] {"long", "command", "name"},
|
||||
new CommandInput("long command name")
|
||||
);
|
||||
}
|
||||
|
||||
[Test]
|
||||
|
||||
@@ -21,7 +21,7 @@ namespace CliFx.Services
|
||||
string optionName = null;
|
||||
|
||||
// Loop through all arguments
|
||||
var isFirstArgument = true;
|
||||
var encounteredFirstOption = false;
|
||||
foreach (var commandLineArgument in commandLineArguments)
|
||||
{
|
||||
// Option name
|
||||
@@ -32,6 +32,8 @@ namespace CliFx.Services
|
||||
|
||||
if (rawOptions.GetValueOrDefault(optionName) == null)
|
||||
rawOptions[optionName] = new List<string>();
|
||||
|
||||
encounteredFirstOption = true;
|
||||
}
|
||||
|
||||
// Short option name
|
||||
@@ -42,6 +44,8 @@ namespace CliFx.Services
|
||||
|
||||
if (rawOptions.GetValueOrDefault(optionName) == null)
|
||||
rawOptions[optionName] = new List<string>();
|
||||
|
||||
encounteredFirstOption = true;
|
||||
}
|
||||
|
||||
// Multiple stacked short options
|
||||
@@ -54,12 +58,17 @@ namespace CliFx.Services
|
||||
if (rawOptions.GetValueOrDefault(optionName) == null)
|
||||
rawOptions[optionName] = new List<string>();
|
||||
}
|
||||
|
||||
encounteredFirstOption = true;
|
||||
}
|
||||
|
||||
// Command name
|
||||
else if (isFirstArgument)
|
||||
else if (!encounteredFirstOption)
|
||||
{
|
||||
if (commandName.IsNullOrWhiteSpace())
|
||||
commandName = commandLineArgument;
|
||||
else
|
||||
commandName += " " + commandLineArgument;
|
||||
}
|
||||
|
||||
// Option value
|
||||
@@ -68,8 +77,6 @@ namespace CliFx.Services
|
||||
// ReSharper disable once AssignNullToNotNullAttribute
|
||||
rawOptions[optionName].Add(commandLineArgument);
|
||||
}
|
||||
|
||||
isFirstArgument = false;
|
||||
}
|
||||
|
||||
return new CommandInput(commandName, rawOptions.Select(p => new CommandOptionInput(p.Key, p.Value)).ToArray());
|
||||
|
||||
Reference in New Issue
Block a user