dotnet / dotnet/command-line-api
Command argument injection stopped working in `2.0.0-beta1.21308.1`
- Dominant language
- C#
- Stars
- 3.7k
- Forks
- 428
- PR merge metrics
- No merged PRs in 30d
Description
Hi,
I have an application that is set up like this. My command:
```C#
public class DeleteCommand : Command
{
[UsedImplicitly(ImplicitUseTargetFlags.WithMembers)]
public class Input
{
public string Repository { get; set; }
public string ObjectId { get; set; }
}
public DeleteCommand() : base("delete", "Deletes a file from the storage.")
{
AddArgument(new Argument("repository", "The repository to delete the file from"));
AddArgument(new Argument("objectId", "The ID of the object to delete"));
}
}
```
My handler (`HandlerBase` is irrelevant for this issue):
```C#
public class DeleteHandler : HandlerBase
{
[UsedImplicitly]
public Commands.DeleteCommand.Input Input { get; set; }
public DeleteHandler(IDataStorageClient dataStorageClient, ILogger logger)
: base(dataStorageClient, null, logger)
{
}
protected override async Task PerformActionAsync()
{
Logger.LogDebug(
$"Deleting from repository '{Input.Repository}' with object ID '{Input.ObjectId}'...");
await DataStorageClient.DeleteObjectAsync(Input.Repository, Input.ObjectId);
Logger.LogDebug("Delete successful");
}
```
And `Program.cs` (I've omitted some details for easier readability):
```C#
var parser = new CommandLineBuilder(Commands.BuildRootCommand())
.UseHelp()
.UseTypoCorrections()
.UseParseErrorReporting()
.UseVersionOption()
.UseHost(_ => Host.CreateDefaultBuilder(args), hostBuilder =>
{
.ConfigureServices(serviceCollection =>
{
serviceCollection
.AddSingleton(new FileSystem())
.AddSingleton(serviceProvider =>
{
// configure global args...
});
})
.UseCommandHandler();
})
.Build();
```
This works great in `2.0.0-beta1.21216.1` - the `Input` class is injected into `DeleteHandler` without an issue. However if I bump to version `2.0.0-beta1.21308.1`, it doesn't work anymore. `Input` is not injected anymore.
I tried comparing the two versions but I couldn't find the tag for the newer version.
Is this a bug, or intended functionality? If the latter, then how can I easily get the command inputs in my handler class?
Contributor guide
Assessment
This issue has not been assessed yet.