-
Notifications
You must be signed in to change notification settings - Fork 12
Open
Labels
bugSomething isn't workingSomething isn't working
Description
Discussed in spectreconsole/spectre.console#1831
Minimal reproducable example
using Spectre.Console;
using Spectre.Console.Cli;
var app = new CommandApp<DefaultCommand>();
app.Configure(static config =>
{
config.PropagateExceptions();
config.ValidateExamples();
config.AddBranch<UserSettings>("user", static user =>
{
user.AddCommand<ListUsers>("list");
});
});
return await app.RunAsync(args);
public class AppSettings : CommandSettings
{
[CommandOption("-d|--database-path")]
public string? DatabasePath { get; set; }
}
public abstract class AppCommand<TSettings> : AsyncCommand<TSettings> where TSettings : AppSettings
{
public override async Task<int> ExecuteAsync(CommandContext context, TSettings settings)
{
AnsiConsole.WriteLine($"Database: {settings.DatabasePath ?? "N/A"}");
return await ExecuteCommandAsync(context, settings);
}
protected abstract Task<int> ExecuteCommandAsync(CommandContext context, TSettings settings);
}
public sealed class DefaultCommand : AppCommand<AppSettings>
{
protected override Task<int> ExecuteCommandAsync(
CommandContext context,
AppSettings settings)
{
return Task.FromResult(0);
}
}
public abstract class UserSettings : AppSettings
{
}
public sealed class ListUsersSettings : UserSettings
{
}
public sealed class ListUsers : AppCommand<ListUsersSettings>
{
protected override async Task<int> ExecuteCommandAsync(
CommandContext context,
ListUsersSettings settings)
{
AnsiConsole.MarkupLine("Hello [green]World[/]");
return await Task.FromResult(0);
}
}Output
10:25:30 ❯ dotnet run -- --help
USAGE:
SpectreInh.dll [OPTIONS] [COMMAND]
OPTIONS:
-h, --help Prints help information
-d, --database-path
COMMANDS:
user
10:25:44 ❯ dotnet run -- user --help
DESCRIPTION:
Manage users in the database
USAGE:
SpectreInh.dll user [OPTIONS] <COMMAND>
OPTIONS:
-h, --help Prints help information
-d, --database-path
COMMANDS:
list
10:25:57 ❯ dotnet run -- user list --help
USAGE:
SpectreInh.dll user list [OPTIONS]
OPTIONS:
-h, --help Prints help information
10:26:34 ❯ dotnet run -- -d foo
Database: foo
10:27:01 ❯ dotnet run -- user list -d foo
Database: N/A
Hello World
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
bugSomething isn't workingSomething isn't working