Skip to content

Binding arguments strings to a complex type? #1413

Open
@cwellsx

Description

@cwellsx

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<string[]>("filter", Array.Empty<string>)
            }
        };

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 -- 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.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions