Skip to content
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

How to use a value parser with subcommands and properties? #186

Closed
vcsjones opened this issue Dec 5, 2018 · 2 comments
Closed

How to use a value parser with subcommands and properties? #186

vcsjones opened this issue Dec 5, 2018 · 2 comments
Labels

Comments

@vcsjones
Copy link

vcsjones commented Dec 5, 2018

I'm attempting to use a IValueParser that was introduced in #59 with my application. Bare bones, here is what I have today.

[Subcommand("sign", typeof(Sign2Command))]
class EntryPoint
{
    static int Main(string[] args) => CommandLineApplication.Execute<EntryPoint>(args);

    public int OnExecute(CommandLineApplication app)
    {
        // Something graceful
        return 1;
    }
}

[Command("sign", Description = "Signs a file.")]
class Sign2Command
{
    [Option("--file-digest", CommandOptionType.SingleValue)]
    [AllowedValues("sha1", "sha256", "sha384", "sha512", IgnoreCase = true)]
    public string FileDigestAlgorithm { get; set; }

    public int OnExecute(CommandLineApplication app)
    {
        Console.WriteLine(FileDigestAlgorithm);
        // Do some signing.
        return 0;
    }
}

I would like to implement an IValueParser for FileDigestAlgorithm to automatically convert it to a HashAlgorithmName (which alas is not an enum, but is a struct with public properties). That is also pretty straight forward.

Where I am struggling is how I can add my new parser to the application. I've tried something like this:

static int Main(string[] args)
{
    var app = new CommandLineApplication<EntryPoint>();
    app.ValueParsers.Add(new HashAlgorithmArgument());
    return app.Execute(args);
}

But then I get

Unrecognized command or argument 'sign'

Which makes sense, because now it wants me to define the Command on the app instance. I'm not sure how to go down that route without ditching properties and attributes entirely.

How can I use IValueParser with a subcommand while continuing to use properties and attributes? #62 looks like what I want, is there something I can do today in the mean time?

@natemcmaster
Copy link
Owner

Let me know if this works:

static int Main(string[] args)
{
    var app = new CommandLineApplication<EntryPoint>();
+   app.Conventions.UseDefaultConventions();
    app.ValueParsers.Add(new HashAlgorithmArgument());
    return app.Execute(args);
}

This is basically what the static CommandLineApplication.Execute method does.

@vcsjones
Copy link
Author

vcsjones commented Dec 6, 2018

That does indeed work. Thank you!

@vcsjones vcsjones closed this as completed Dec 6, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants