command-line-api icon indicating copy to clipboard operation
command-line-api copied to clipboard

Type Binding

Open HazyFish opened this issue 2 years ago • 0 comments

Current Problems

  • We cannot register custom type binding. We have to provide a parseArgument method to every option and/or argument where the same custom type is used.

  • We cannot leverage the existing validation logic for supported types such as Uri to parse custom types, since we are back to dealing with string tokens once we use parseArgument.

Proposed Solution

I would like to have an API that allows custom type binding.

builder.BindArgumentType<IDbClient>(str => new DbClient(str));
// or, to leverage existing validation for `Uri`
builder.BindArgumentType<Uri, IDbClient>(uri => new DbClient(uri));

Then we can make the code much more lean and readable when we use the type binding.

new Option<IDbClient>("--from"),

// the following should work even if `List<IDbClient>` is not registered by the user
new Option<List<IDbClient>>("--to"),

HazyFish avatar Sep 02 '23 15:09 HazyFish