dotnet / dotnet/command-line-api
Initializer alternative to fluent AcceptLegalFilePathsOnly
- Dominant language
- C#
- Stars
- 3.7k
- Forks
- 428
- PR merge metrics
- No merged PRs in 30d
Description
I tried porting an existing app from System.CommandLine 2.0.0-beta4.22272.1 (commit 209b724a3c843253d3071e8348c353b297b0b8b5) to 2.0.0-beta4.23073.1 (commit 6524142f1398ed78eadfb9b802ba6492c6af63c4). Because of , it is no longer possible to initialize a static field like this:
```csharp
private static readonly Option OutputZipFileOption
= new Option(
aliases: new[] { "-o", "--output" })
{
IsRequired = true,
}
.LegalFilePathsOnly();
```
Instead, one can do any of the following:
- Add a `private static Option CreateOutputZipFileOption` method and call that in the initializer.
- Explicitly define a static constructor that initializes the fields. This loses the `beforefieldinit` flag but that seems unlikely to matter for performance because the initialization would have been done during startup anyway.
- Make the fields not static, and initialize them in an instance constructor.
- Replace the fields with local variables, and reference them from a lambda or a local function so the C# compiler lowers them to fields again.
- Wait for .
Could the library instead allow this syntax:
```csharp
private static readonly Option OutputZipFileOption
= new Option(
aliases: new[] { "-o", "--output" })
{
IsRequired = true,
Validators = { OptionValidation.LegalFilePath },
};
```
That would be more convenient to use, but more difficult to discover. The documentation of the `Validators` property could reference it, though. It might even have been possible to guide the Intellisense feature of Visual Studio to it by using the `completionlist` element in the XML documentation comments of `ValidateSymbolResult`, if that type had not been removed in / .
Contributor guide
Assessment
This issue has not been assessed yet.