Command-line-api: Support of unnamed arguments?

Created on 29 Sep 2019  路  3Comments  路  Source: dotnet/command-line-api

Hi,

Can I have my program support usage like this:

$git add a.txt

where a.txt is passed to a handler without having a name like git add --file a.txt

If so, is there any documentation/sample for this kind of usage?

Thanks,

question

Most helpful comment

Thanks @jonsequitur, I was in the process of opening a new issue when this suggestion appeared in the github editor... This example should be included in the docs.

All 3 comments

You can do this with a command argument:

var addCommand = new Command("add")
{
    new Argument<FileInfo>("file") //         <-- ARGUMENT NAME...
};
addCommand.Handler = CommandHandler.Create(
    (FileInfo file)) => //                         <-- ...MATCHES HANDLER PARAMETER NAME
    {  
    });

The name isn't passed on the command line, so you can invoke it just like in your example. But the name is used when binding to your command handler, so they should match.

this worked. thanks @jonsequitur !

Thanks @jonsequitur, I was in the process of opening a new issue when this suggestion appeared in the github editor... This example should be included in the docs.

Was this page helpful?
0 / 5 - 0 ratings