Tyrrrz/CliFx

Default `IsRequired` for options based on whether the `required` modifier is used on the property

Tyrrrz opened this issue · 0 comments

C#11 added the required keyword for properties:

[Command]
public class AuthCommand : ICommand
{
    [CommandOption("token", IsRequired = true, EnvironmentVariable = "AUTH_TOKEN")]
    public required string AuthToken { get; init; }

    public ValueTask ExecuteAsync(IConsole console)
    {
        console.Output.WriteLine(AuthToken);

        return default;
    }
}

We should default the value of the IsRequired property on the CommandOption attribute to true (instead of false) if the required keyword is used.

This keyword can be detected through reflection by looking for the RequiredMember attribute:

image