Cocona icon indicating copy to clipboard operation
Cocona copied to clipboard

Feature request: enum classes

Open dahlia opened this issue 5 years ago • 2 comments

Some arguments/options has only limited choices. The easiest way to implement them using Cocona at this moment would be maintaining a static readonly set of string or string-keyed dictionary. However, still we need to implement validation & errors by hand. There is a limitation as well that such arg/option can't show all available choices in its help message — without code duplication.

IMHO, such args/options can easily be modeled on enum classes, so I wish Cocona to automatically deal with enum classes. For example, where we have an enum class:

public enum EmploymentState { FullTime, PartTime, Retired }

and an option:

public void ListEmployees(
    [Option('s', Description = "Filter by employment state.")]
    EmploymentState? stateFilter = null
)

I wish Cocona translates --state-filter full-time to stateFilter = EmploymentState.FullTime or -s retired to stateFilter = EmploymentState.Retired. Also it would be great if its help message shows all available choices:

Options:
  -s, --state-filter <EmploymentState>    Filter by employment state.
                                          Available choices: full-time, part-time, retired.

dahlia avatar Oct 06 '20 07:10 dahlia

Thanks for the feedback! The current Cocona implementation should be able to handle enums.

public void ListEmployees(
    [Option('s', Description = "Filter by employment state.")]
    EmploymentState stateFilter = EmploymentState.None
)
{
}
public enum EmploymentState
{
    FullTime, PartTime, Retired
}
C:\>dotnet run -- --help
Usage: ConsoleApp1 [--state-filter <EmploymentState>] [--completion] [--help] [--version]

ConsoleApp1

Options:
  -s, --state-filter <EmploymentState>    Filter by employment state. (Required) (Allowed values: FullTime, PartTime, Retired)
  --completion                            Generate a shell completion code
  -h, --help                              Show help message
  --version                               Show version

C:\>dotnet run -- --state-filter retired
Retired

However, in the case of nullable, as in your example, the help output is inappropriate. I'd like to fix the nullable description, but the current workaround is to use a default value such as None.

public void ListEmployees(
    [Option('s', Description = "Filter by employment state.")]
    EmploymentState stateFilter = EmploymentState.None
)
{
    Console.WriteLine(stateFilter);
}

public enum EmploymentState
{
    None, FullTime, PartTime, Retired
}
C:\>dotnet run -- --help
Usage: ConsoleApp1 [--state-filter <EmploymentState>] [--completion] [--help] [--version]

ConsoleApp1

Options:
  -s, --state-filter <EmploymentState>    Filter by employment state. (Default: None) (Allowed values: None, FullTime, PartTime, Retired)
  --completion                            Generate a shell completion code
  -h, --help                              Show help message
  --version                               Show version

C:\>dotnet run --
None

mayuki avatar Oct 07 '20 16:10 mayuki

Oh, I wasn't aware that! Thanks for your kind answer. 🙏🏼

dahlia avatar Oct 08 '20 04:10 dahlia