Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initializer alternative to fluent AcceptLegalFilePathsOnly #2037

Open
KalleOlaviNiemitalo opened this issue Jan 29, 2023 · 1 comment
Open
Labels
Area-API enhancement New feature or request

Comments

@KalleOlaviNiemitalo
Copy link

KalleOlaviNiemitalo commented Jan 29, 2023

I tried porting an existing app from System.CommandLine 2.0.0-beta4.22272.1 (commit 209b724) to 2.0.0-beta4.23073.1 (commit 6524142). Because of #1994, it is no longer possible to initialize a static field like this:

        private static readonly Option<FileInfo> OutputZipFileOption
            = new Option<FileInfo>(
                aliases: new[] { "-o", "--output" })
            {
                IsRequired = true,
            }
            .LegalFilePathsOnly();

Instead, one can do any of the following:

  • Add a private static Option<FileInfo> 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 [Proposal]: Directly invoked anonymous functions csharplang#4748.

Could the library instead allow this syntax:

        private static readonly Option<FileInfo> OutputZipFileOption
            = new Option<FileInfo>(
                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<T>, if that type had not been removed in #1944 / #1946.

@KalleOlaviNiemitalo KalleOlaviNiemitalo changed the title Initializer alternative for fluent AcceptLegalFilePathsOnly Initializer alternative to fluent AcceptLegalFilePathsOnly Jan 29, 2023
@KalleOlaviNiemitalo

This comment was marked as off-topic.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-API enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants