Fail when there are no commands defined

This commit is contained in:
Alexey Golub
2019-07-25 23:06:35 +03:00
parent d2599af90b
commit dd2789790e
2 changed files with 77 additions and 38 deletions

View File

@@ -23,7 +23,7 @@ namespace CliFx.Tests
}
[Command("faulty-command")]
private class FaultyCommand : ICommand
private class TestFaultyCommand : ICommand
{
public Task ExecuteAsync(CommandContext context) => Task.FromException(new CommandErrorException(-1337));
}
@@ -86,6 +86,53 @@ namespace CliFx.Tests
// Default command is not defined
yield return new TestCaseData(
new[] {typeof(TestNamedCommand)},
new string[0]
);
yield return new TestCaseData(
new[] {typeof(TestNamedCommand)},
new[] {"--version"}
);
yield return new TestCaseData(
new[] {typeof(TestNamedCommand)},
new[] {"--help"}
);
yield return new TestCaseData(
new[] {typeof(TestNamedCommand)},
new[] {"-h"}
);
yield return new TestCaseData(
new[] {typeof(TestNamedCommand)},
new[] {"-?"}
);
// Specified a faulty command
yield return new TestCaseData(
new[] {typeof(TestFaultyCommand)},
new[] {"faulty-command", "--help"}
);
yield return new TestCaseData(
new[] {typeof(TestFaultyCommand)},
new[] {"faulty-command", "-h"}
);
yield return new TestCaseData(
new[] {typeof(TestFaultyCommand)},
new[] {"faulty-command", "-?"}
);
}
private static IEnumerable<TestCaseData> GetTestCases_RunAsync_Negative()
{
// No commands defined
yield return new TestCaseData(
new Type[0],
new string[0]
@@ -111,33 +158,6 @@ namespace CliFx.Tests
new[] {"-?"}
);
// Specified a faulty command
yield return new TestCaseData(
new[] {typeof(FaultyCommand)},
new[] {"--version"}
);
yield return new TestCaseData(
new[] {typeof(FaultyCommand)},
new[] {"--help"}
);
yield return new TestCaseData(
new[] {typeof(FaultyCommand)},
new[] {"-h"}
);
yield return new TestCaseData(
new[] {typeof(FaultyCommand)},
new[] {"-?"}
);
}
private static IEnumerable<TestCaseData> GetTestCases_RunAsync_Negative()
{
// Specified command is not defined
yield return new TestCaseData(
new Type[0],
new[] {"command"}
@@ -145,23 +165,35 @@ namespace CliFx.Tests
yield return new TestCaseData(
new Type[0],
new[] {"faulty-command"}
);
// Specified command is not defined
yield return new TestCaseData(
new[] {typeof(TestDefaultCommand)},
new[] {"command"}
);
yield return new TestCaseData(
new[] {typeof(TestDefaultCommand)},
new[] {"command", "--help"}
);
yield return new TestCaseData(
new Type[0],
new[] {typeof(TestDefaultCommand)},
new[] {"command", "-h"}
);
yield return new TestCaseData(
new Type[0],
new[] {typeof(TestDefaultCommand)},
new[] {"command", "-?"}
);
// Specified a faulty command
yield return new TestCaseData(
new[] {typeof(FaultyCommand)},
new[] {typeof(TestFaultyCommand)},
new[] {"faulty-command"}
);
}

View File

@@ -55,13 +55,26 @@ namespace CliFx
var availableCommandSchemas = _commandSchemaResolver.GetCommandSchemas(_commandTypes);
var matchingCommandSchema = availableCommandSchemas.FindByNameOrNull(commandInput.CommandName);
// Fail if there are no commands defined
if (!availableCommandSchemas.Any())
{
stdErr.WriteLine("There are no commands defined in this application.");
return -1;
}
// Fail if specified a command which is not defined
if (commandInput.IsCommandSpecified() && matchingCommandSchema == null)
{
stdErr.WriteLine($"Specified command [{commandInput.CommandName}] doesn't exist.");
stdErr.WriteLine($"Specified command [{commandInput.CommandName}] is not defined.");
return -1;
}
// Use a stub if command was not specified but there is no default command defined
if (matchingCommandSchema == null)
{
matchingCommandSchema = _commandSchemaResolver.GetCommandSchema(typeof(StubDefaultCommand));
}
// Show version if it was requested without specifying a command
if (commandInput.IsVersionRequested() && !commandInput.IsCommandSpecified())
{
@@ -70,12 +83,6 @@ namespace CliFx
return 0;
}
// Use a stub if command was not specified but there is no default command defined
if (matchingCommandSchema == null)
{
matchingCommandSchema = _commandSchemaResolver.GetCommandSchema(typeof(StubDefaultCommand));
}
// Show help if it was requested
if (commandInput.IsHelpRequested())
{