Skip to content

Custom class with default string constructor requires parser on latest beta #1664

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
oblaise opened this issue Mar 6, 2022 · 3 comments
Closed

Comments

@oblaise
Copy link

oblaise commented Mar 6, 2022

Dear All,

Not sure if this was the case with the beta-2 but with beta-3 if I want to use my own custom class that implements a default public constructor that takes a single string argument the parsing doesn't works without specifying a parser.

If I use the same code with a Uri Type instead of the TestClass it doesn't require a parser for working.

Is it expected to remove the support for custom class with default string constructor since the changes in beta2/3 ?

Following code works only if I use the Argument creation with a parser:

#r "nuget:System.CommandLine,*-*"

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

class TestClass {
    public TestClass(string value)
    {
        Value = value;
    }
    public string Value {get;private set;}
}

static TestClass ParseTestClass(ArgumentResult result)
{
    if (result.Tokens.Count==1)
        return new TestClass(result.Tokens[0].Value);
    result.ErrorMessage = $"Not a unique argument";
    return default;
}

// Not working using default public constructor
var argTestClass = new Argument<TestClass>("TC");

// Working using a parser
// var argTestClass = new Argument<TestClass>("TC", ParseTestClass);

var argString = new Argument<string>("string");
var cmd = new RootCommand("root") { argTestClass , argString };

cmd.SetHandler<TestClass,string>((tc,s)=>{ Console.WriteLine($"TestClass: {tc.Value}, string: {s}");},argTestClass,argString);

var parser = new CommandLineBuilder(cmd).Build();

parser.Invoke("Hello World");
@jonsequitur
Copy link
Contributor

This was a deliberate change in order to reduce the use of reflection and support trimming.

We plan to support this kind of convention via source generators in the future, or you can get the old behavior (via CommandHandler.Create) by referencing System.CommandLine.NamingConventionBinder.

@oblaise
Copy link
Author

oblaise commented Mar 9, 2022

Fair for me to require the parser delegate that's just because I didn't see it documented anywhere in the beta-2 changes - maybe I missed it.

@oblaise oblaise closed this as completed Mar 9, 2022
@jonsequitur
Copy link
Contributor

This change was in made beta3 but I've just realized I didn't document it. I'll update #1613 to mention this.

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