-
Notifications
You must be signed in to change notification settings - Fork 395
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
Comments
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");
|
Thanks, that is resolving my issue. Is there any plan to support recursive model binding? |
The app model work that @KathleenDollard has started here should address it but the core |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Now I bind option/argument to color, with below method:
I hope to group parameters to two properties.
Thanks.
The text was updated successfully, but these errors were encountered: