Skip to content

How can I customise help for an option within a command? #1593

Open
@ashscodes

Description

@ashscodes

I was reading the release for the notes about Customizing help for a single option or argument and all the examples use an instance of the CommandLineBuilder. Is it possible to customise the help when declaring a new Option or in the constructor for a class. At the moment the project I am developing basically looks like this:

// Commands are added as a Singleton to IServiceCollection and then added to the root command.
internal static Parser Build(ServiceProvider serviceProvider)
{
    RootCommand rootCommand = new RootCommand(description: "Repository management tool.");
    foreach (Command command in serviceProvider.GetServices<Command>())
    {
        rootCommand.AddCommand(command);
    }

    // Program.cs invokes the result of this async with the args.
    return new CommandLineBuilder(rootCommand).UseDefaults().Build();
}

public class ConfigCommand : Command
{
    private readonly string[] Force = new string[] { "-f", "--force" };

    private readonly string[] Generate = new string[] { "-g", "--generate" };

    public ConfigCommand() : base(
        name: "config",
        description: "Options for creating, getting or setting app configuration.")
    {
        Option forceOption = new Option<bool>(
                aliases: this.Force,
                description: "Specifies that an existing config file should be overwritten.");

        Option generateOption = new Option<bool>(
                aliases: this.Generate,
                description: "Creates a new config file in the current directory.");

        this.AddOption(generateOption);
        this.AddOption(forceOption);

        this.SetHandler(
            (bool generate, bool force) =>
            {
                ConfigHandler.Invoke(generate, force);
            },
            generateOption,
            forceOption);
    }
}

Unless I am missing something, this is not currently possible unless the CommandLineBuilder is used, and I assume I shouldn't be using that for anything other than a single command which includes any subcommands, in this case the RootCommand. It would be cool to be able to apply a customisation object directly to the option constructor.

I have seen the example in the tests here, but this I do not think I can apply it to an unknown number of commands being passed via the ServiceProvider.

Apologies if anything is unclear, there's a learning curve involved for me here that extends outside of System.CommandLine :)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions