-
Notifications
You must be signed in to change notification settings - Fork 376
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
Can CommandHandler.Create() go more than 7 parameters? #458
Comments
My personal opinion is the |
Another approach that's worth considering: You can do bind complex objects. command.Handler = CommandHandler.Create<MyType>(obj => /* */ ); Constructor injection and property setters are both supported, with the same naming conventions as we use to match parameters. |
Can you say a bit more about this? I have a command with 9 options ... what should I do? |
Let's assume you have a command line with options command.Handler = CommandHandler.Create<MyType>(myType => /* */ ); where public class MyType
{
public string A { get; set; }
public string B { get; set; }
public string C { get; set; }
public string D { get; set; }
public string E { get; set; }
public string F { get; set; }
public string G { get; set; }
public string H { get; set; }
public string I { get; set; }
}
// or...
public class MyType
{
public MyType(string a, string b, string c, string d, string e, string f, string g, string h, string i)
{
A = a;
B = b;
C = c;
D = d;
E = e;
F = f;
G = g;
H = h;
I = i;
}
public string A { get; }
public string B { get; }
public string C { get; }
public string D { get; }
public string E { get; }
public string F { get; }
public string G { get; }
public string H { get; }
public string I { get; }
} |
Thanks, that works nicely. |
I agree. Aka, 16. https://docs.microsoft.com/en-us/dotnet/api/system.func-17?view=netcore-3.1 |
Can you show the my.exe whatIsHere for this? Thanks. |
This would work for the example: > my.exe -a -b -c -d -e -f -g -h -i Name matching is by parameter name for simple types, and by constructor or property names for more complex types. |
I'm confused between what is the "flag", and what is the value. Can you repost with values like valuea valueb (etc, etc) is the below valid for using "MyType" ? my.exe -a valuea -b valueb -c valuec -d valued -e valuee -f valuef -g valueg -h valueh -i valuei |
Sorry, what I posted would be valid for bools. Your example is correct. |
I appreciate your quick responses. I am currently getting: Unrecognized command or argument 'valuea' mycommandone: Usage: Options: ==============================
The code for setup:
|
I tried this as well:
===========
================================= ============================================== 'mytypeone' was not matched. Did you mean 'mytypeone'? << may want to look into this separate from my issue(s) :) Unrecognized command or argument 'mytypeone' mycommandone: Usage: Commands: |
When binding to a complex type you still need to define the options. Here's a full working example: Note that I changed the invoked command to Also note that if you use the |
I appreciate your help. I am still getting issues. I've coded up a clean-sample here: https://github.com/granadacoder/system-command-line-demo/tree/feature/InitialCommit If you get a chance to look, I would be grateful. Thank you. README has enough notes to get going. I coded it to your example...."MyTypeCommandCreator.cs". But still getting: Required command was not provided. |
Ok, My RunJustRootCommandDemo does work... if I send this in. (note the no prefix "--" on the first argument)
But check out my "RunParserDemo" method. Basically, if the RootCommand gets pushed to a Parser, the code fails. This may be intentional..just highlighting it.
All of this code in the github repo above. |
I see that you're using an old version, This probably is not related to any issues you've described, though. |
Gaaaaaaaaaaaa! I updated the nuget package and pushed the code. ( I did not realize I was using an experimental outdated version..Thanks.)
Short Version: This is WORKING !!
Not working is adding it to a "Parser". In case you're interested. Working Output:
|
The root command isn't intended to be supplied on the command line, though the parser does understand it so that you can, for example, parse a complete, un-split string. So, for example, for input like this: > my.exe subcommand1 --option1 argument
But System.CommandLine lets you do any of the following with equivalent results: parser.Parse("subcommand1 --option1 argument");
parser.Parse("my.exe subcommand1 --option1 argument");
parser.Parse(new [] { "subcommand1", "--option1", "argument" } );
parser.Parse(new [] { "my.exe","subcommand1","--option1","argument" } ); |
I finally figured it out. I am able to have multiple Commands off the (vanilla) RootCommand now. I was (errantly) using the Parser when I should have been using the RootCommand. I'll update my sample when I get a chance. Thanks for your quick feedback. |
Ok, I've updated the sample. https://github.com/granadacoder/system-command-line-demo This shows strong binding to a simple POCO (two different Poco's)....with 2 "forks" for the functionality. Basically, this:
I'm "good" now. Thanks for the help. |
You're welcome! |
This works:
subcommand.Handler = CommandHandler.Create((int p1, int p2, int p3, ... int p7) => ...
This doesn't:
subcommand.Handler = CommandHandler.Create((int p1, int p2, int p3, ... int p8) => ...
Is it reasonable to ask to keep going a few more parameters since Func<> and Action<> support more?
The text was updated successfully, but these errors were encountered: