Tyrrrz / CliFx

Class-first framework for building command-line interfaces
MIT License
1.5k stars 61 forks source link

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

Closed Tyrrrz closed 1 year ago

Tyrrrz commented 1 year ago

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