Skip to content

Use custom model datatype

Matthias Beerens edited this page Sep 25, 2020 · 2 revisions

Custom resolvers

If you have a custom datatype, you can create your own resolver for that.

NOTE: If your custom datatype contains the Parse and/or TryParse pattern, you don't need to implement your own resolver.

internal class CustomResolver : BaseArgumentResolver<CustomType>
{
    public override bool CanResolve(ArgumentModel model) => true;

    public override CustomType Resolve(ArgumentModel model) 
       => model.HasValue ? new CustomType(model.Value) : null;
}

Now we can register the new resolver using Dependency Injection.

static void Main(string[] args)
{
    var services = new ServiceCollection();

    // add custom type resolver
    services.AddScoped<IArgumentResolver<CustomType>, CustomResolver>();

    var parser = new CommandLineParser<Options>(services);

    // further configuration and parsing..
}
Clone this wiki locally