dotnet / dotnet/command-line-api
CommandHandler.Create delegate does not get correctly bound options
- Dominant language
- C#
- Stars
- 3.7k
- Forks
- 428
- PR merge metrics
- No merged PRs in 30d
Description
Tested publish profiles:
* Target framework: net8.0
* Deployment mode: Self-contained
* Target runtime: win-x64, linux-x64
* Enable ReadyToRun compilation: true, false
* Trim unused code: true
The context in Program.cs
```c#
internal class Options {
public int Timeout { get; set; }
}
RootCommand rootCommand = new("desc")
{
new Option(
[ "--timeout" ],
() => 30,
"Maximum wait time in seconds"),
};
```
In the aforementioned profiles and context, the following delegate behaves weirdly:
```c#
rootCommand.Handler = CommandHandler.Create(options => {
...
});
```
I tested three situations:
* If no arguments are specified in the command line: `options` will be null.
* If `--timeout` is specified in the command line: `options` is not null but the `Timeout` property won't be set.
* If `--timeout` is specified in the command line and the `Options.Timeout` property is mentioned at least once in the delegate: everything works as correctly.
Outside the aforementioned profiles, when the deployment mode is set to Framework-dependent, then everything works as correctly.
Contributor guide
Assessment
This issue has not been assessed yet.