dotnet / dotnet/command-line-api
Binding arguments strings to a complex type?
- Dominant language
- C#
- Stars
- 3.7k
- Forks
- 428
- PR merge metrics
- No merged PRs in 30d
Description
I have a root command with subcommands (verbs).
The subcommands accept 0 .. n strings as arguments -- they're command arguments, not option arguments.
var root = new RootCommand()
{
new Command("foo")
{
new Argument("filter", Array.Empty)
}
};
I can create a handler for that which expects `string[]` as a parameter.
But what I want is:
- The handler should take a more complex user-defined type (named `Filter`), as its parameter
- The command-line parser should use the `string[]` to instantiate that type
I thought that I could do that, using [Model Binding -- More complex types](https://github.com/dotnet/command-line-api/blob/main/docs/model-binding.md#more-complex-types) -- but I tried and failed (with a run-time exception like "Cannot convert string[] to Filter").
- Is this scenario supported, or is that only (as it says) for "binding `Option` arguments" and not for binding `Arguments`?
- Or if it is supported, is there an example or maybe a unit test of its being done?
As a work-around I can convert/instantiate it myself within the command-handler; but I wanted it done automatically because there are many commands which all expect the same kind of arguments type.
Contributor guide
Assessment
This issue has not been assessed yet.