mirror of
https://github.com/nsnail/spectre.console.git
synced 2025-04-16 00:42:51 +08:00

* Adding a dark mode * Adding reference for types to summary pages * Adding API Reference * Adding modifiers to methods/fields/etc * Minimizing files input * Caching a lot of the output pages * Cache only for each execution * Adding API references to existing docs
2.3 KiB
2.3 KiB
Title: Creating Commands
Order: 6
Description: "How to create commands for Spectre.Console.Cli"
Reference:
- T:Spectre.Console.Cli.AsyncCommand1 - T:Spectre.Console.Cli.Command
1
Commands in Spectre.Console.Cli
are defined by creating a class that inherits from either Command<TSettings>
or AsyncCommand<TSettings>
. Command<TSettings>
must implement an Execute
method that returns an int where as AsyncCommand<TSettings>
must implement ExecuteAsync
returning Task<int>
.
public class HelloCommand : Command<HelloCommand.Settings>
{
public class Settings : CommandSettings
{
[CommandArgument(0, "[Name]")]
public string Name { get; set; }
}
public override int Execute(CommandContext context, Settings settings)
{
AnsiConsole.MarkupLine($"Hello, [blue]{settings.Name}[/]");
return 0;
}
}
Configuring
Commands are configured via the CommandApp
's Configure
method.
var app = new CommandApp();
app.Configure(config =>
{
config.AddCommand<HelloCommand>("hello")
.WithAlias("hola")
.WithDescription("Say hello")
.WithExample(new []{"hello", "Phil"})
.WithExample(new []{"hello", "Phil", "--count", "4"});
});
WithAlias
allows commands to have multiple names.WithDescription
is used by the help renderer to give commands a description when displaying help.WithExample
is used by the help renderer to provide examples to the user for running the commands. The parameters is a string array that matches the values passed inMain(string[] args)
.
Dependency Injection
Constructor injection is supported on commands. See the CommandApp
documentation for further information on configuring Spectre.Console
for your container.
Validation
While the settings can validate themselves, the command also provides a validation. For example, IFileSystem
might be injected into the command which we want to use to validate that a path passed in exists.
public override ValidationResult Validate(CommandContext context, Settings settings)
{
if (_fileSystem.IO.File.Exists(settings.Path))
{
return ValidationResult.Error($"Path not found - {settings.Path}");
}
return base.Validate(context, settings);
}