commandline icon indicating copy to clipboard operation
commandline copied to clipboard

Support for commands

Open fleed opened this issue 8 years ago • 1 comments

Is there any existing way to define commands, intended as classes to execute specific options? IF not, any plan to add support? I would expect something like this, removing lot of the ceremony required to configure the option and run the related logic:

Example:

class CommitCommand : Command<CommitOptions>
{
    public int Execute(CommitOptions options)
    {
        // execute
        return 0;
    }

    // alternative
    public int Execute()
    {
        // access options as property in AsyncCommand base
        var file = this.Options.File;

        // execute
        return 0;
    }
}

class UploadCommand : AsyncCommand<UploadOptions>
{
    public async Task<int> ExecuteAsync(CommitOptions options)
    {
        // execute
        return 0;
    }

    // alternative
    public async Task<int> ExecuteAsync()
    {
        // access options as property in AsyncCommand base
        var file = this.Options.File;

        // execute
        return 0;
    }
}

// Program.cs
var parser = new CommandParser();
var parserResult = parser.Parse(args);
var result = parserResult.ExecuteCommand();

In general I'd like the possibility to discover options and commands through reflection, something similar to ManyConsole.

fleed avatar Mar 31 '17 04:03 fleed

There's no autodetection, but do verbs not do what you're asking?

https://github.com/gsscoder/commandline/blob/master/tests/CommandLine.Tests/Unit/ParserResultExtensionsTests.cs#L25

Parser.Default.ParseArguments<Add_Verb, Commit_Verb, Clone_Verb>(
    new[] { "clone", "https://value.org/user/file.git" })
  .WithParsed<Add_Verb>(opts => expected = "wrong1")
  .WithParsed<Commit_Verb>(opts => expected = "wrong2")
  .WithParsed<Clone_Verb>(opts => expected = opts.Urls.First());

nemec avatar Apr 03 '17 18:04 nemec