dotnet / dotnet/command-line-api

CommandHandler.Create delegate does not get correctly bound options

Open
#2,297 0 comments 0 reactions 0 assignees View on GitHub
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

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.