Skip to content

Design: builder API for parsing args into .NET types #37

Description

@natemcmaster

Problem
Users who prefer the builder API can't take advantage of the automatic type parsing available in the attribute api for int, List<T>, etc. Part of the difficulty is that CommandLineApplication, CommandArgument, and CommandOption only provide string-based properties.

Proposal
Add CommandOption<T> and CommandArgument<T>.

There is acts as an implicit requirement that values can be parsed to typeof(T).

For custom types, users can provide a custom converter to parse string into typeof(T). CLU will provide parsers for most commonly used types like int, double, bool, etc.

Usage

public static int Main(string[] args)
{
    var app = new CommandLineApplication();

    CommandArgument<int> arg = app.Argument<int>("arg", "argument");
    CommandOption<int> option = app.Option<int>("--arg", CommandOptionType.SingleValue)
        .Accepts().Range(0, 100);

    CommandOption<IPAddress> option2 = app.Option<IPAddress>("--addres", CommandOptionType.SingleValue)
        .UseConverter(val => IPAddress.Parse(val));

    app.OnExecute(() =>
    {
        int[] values = option.Values;
    });

    return app.Execute(args);
}

Metadata

Metadata

Assignees

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions