Skip to content

How to use complex type as option/argument? like color, size, rectangle. #998

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
rwecho opened this issue Jul 27, 2020 · 3 comments
Closed

Comments

@rwecho
Copy link

rwecho commented Jul 27, 2020

Now I bind option/argument to color, with below method:

   public class Arguments
    {
        public int ColorA { get; set; }
        public int ColorR{get; set; }
        public int ColorG{get; set; }
        public int ColorB{get; set; }
        public string SizeWidth { get; set; }
        public string SizeHeight { get; set; }
    }

I hope to group parameters to two properties.

   public class Arguments
    {
        public Color Color { get; set; }
        public Size Size { get; set; }
    }

Thanks.

@jonsequitur
Copy link
Contributor

We don't currently support recursive model binding but you can do this with constructor arguments like this:

using System.CommandLine;
using System.CommandLine.Invocation;
using System.CommandLine.Parsing;

public class Size
{
    public string Width { get; set; }
    public string Height { get; set; }
}

public class Color
{
    public int A { get; set; }
    public int R {get; set; }
    public int G {get; set; }
    public int B {get; set; }
}

public class Arguments
{
    public Arguments(
        int a,
        int r,
        int g,
        int b,
        string width,
        string height)
    {
        Color = new Color { 
            A = a,
            R = r,
            G = g,
            B = b,
        };
        Size = new Size
        {
            Width = width,
            Height = height
        };
    }

    public Color Color { get; set; }
    public Size Size { get; set; }
}

var command = new RootCommand
{
    new Option<int>("-a"),
    new Option<int>("-r"),
    new Option<int>("-g"),
    new Option<int>("-b"),
    new Option<string>("--width"),
    new Option<string>("--height"),
};

command.Handler = CommandHandler.Create<Arguments>(args => {
    Console.WriteLine(args.Color.A);
    Console.WriteLine(args.Color.R);
    Console.WriteLine(args.Color.G);
    Console.WriteLine(args.Color.B);
    Console.WriteLine(args.Size.Width);
    Console.WriteLine(args.Size.Height);

});

command.Invoke("-a 1 -r 2 -g 3 -b 4 --width 5 --height 6");

@rwecho
Copy link
Author

rwecho commented Jul 28, 2020

Thanks, that is resolving my issue. Is there any plan to support recursive model binding?

@jonsequitur
Copy link
Contributor

The app model work that @KathleenDollard has started here should address it but the core System.CommandLine model binder is unlikely to due to the complexity of concerns like resolving naming conflicts, etc.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants