Skip to content

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

Description

@vcsjones

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?

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions