From 8e8c69a73b7234460bfc9e718eed5939708d332c Mon Sep 17 00:00:00 2001 From: Adam Sitnik Date: Wed, 22 Feb 2023 19:54:06 +0100 Subject: [PATCH] Symbol.Description does not need to be virtual, we can pass the description to ctor when creating every Symbol (#2045) the affected ctors belonged to internal types --- .../Builder/CommandLineBuilderExtensions.cs | 6 +++--- src/System.CommandLine/Help/HelpOption.cs | 18 ++++-------------- src/System.CommandLine/Help/VersionOption.cs | 19 ++++--------------- src/System.CommandLine/Symbol.cs | 2 +- 4 files changed, 12 insertions(+), 33 deletions(-) diff --git a/src/System.CommandLine/Builder/CommandLineBuilderExtensions.cs b/src/System.CommandLine/Builder/CommandLineBuilderExtensions.cs index dbb6d1f69a..42213a556e 100644 --- a/src/System.CommandLine/Builder/CommandLineBuilderExtensions.cs +++ b/src/System.CommandLine/Builder/CommandLineBuilderExtensions.cs @@ -236,7 +236,7 @@ void Default(Exception exception, InvocationContext context) /// The same instance of . public static CommandLineBuilder UseHelp(this CommandLineBuilder builder, int? maxWidth = null) { - return builder.UseHelp(new HelpOption(() => builder.LocalizationResources), maxWidth); + return builder.UseHelp(new HelpOption(builder.LocalizationResources), maxWidth); } /// @@ -250,7 +250,7 @@ public static CommandLineBuilder UseHelp( this CommandLineBuilder builder, params string[] helpAliases) { - return builder.UseHelp(new HelpOption(helpAliases, () => builder.LocalizationResources)); + return builder.UseHelp(new HelpOption(helpAliases, builder.LocalizationResources)); } /// @@ -270,7 +270,7 @@ public static CommandLineBuilder UseHelp( if (builder.HelpOption is null) { - builder.UseHelp(new HelpOption(() => builder.LocalizationResources), maxWidth); + builder.UseHelp(new HelpOption(builder.LocalizationResources), maxWidth); } return builder; diff --git a/src/System.CommandLine/Help/HelpOption.cs b/src/System.CommandLine/Help/HelpOption.cs index d2411dc67e..627c43e238 100644 --- a/src/System.CommandLine/Help/HelpOption.cs +++ b/src/System.CommandLine/Help/HelpOption.cs @@ -8,31 +8,21 @@ namespace System.CommandLine.Help { internal class HelpOption : Option { - private readonly Func _localizationResources; - private string? _description; - - public HelpOption(string[] aliases, Func getLocalizationResources) - : base(aliases, null, new Argument { Arity = ArgumentArity.Zero }) + internal HelpOption(string[] aliases, LocalizationResources localizationResources) + : base(aliases, localizationResources.HelpOptionDescription(), new Argument { Arity = ArgumentArity.Zero }) { - _localizationResources = getLocalizationResources; AppliesToSelfAndChildren = true; } - public HelpOption(Func getLocalizationResources) : this(new[] + internal HelpOption(LocalizationResources localizationResources) : this(new[] { "-h", "/h", "--help", "-?", "/?" - }, getLocalizationResources) - { - } - - public override string? Description + }, localizationResources) { - get => _description ??= _localizationResources().HelpOptionDescription(); - set => _description = value; } internal override bool IsGreedy => false; diff --git a/src/System.CommandLine/Help/VersionOption.cs b/src/System.CommandLine/Help/VersionOption.cs index 04514a9b15..e97e39bb4e 100644 --- a/src/System.CommandLine/Help/VersionOption.cs +++ b/src/System.CommandLine/Help/VersionOption.cs @@ -10,20 +10,15 @@ namespace System.CommandLine.Help { internal class VersionOption : Option { - private readonly CommandLineBuilder _builder; - private string? _description; - - public VersionOption(CommandLineBuilder builder) : base("--version", null, new Argument { Arity = ArgumentArity.Zero }) + internal VersionOption(CommandLineBuilder builder) + : base("--version", builder.LocalizationResources.VersionOptionDescription(), new Argument { Arity = ArgumentArity.Zero }) { - _builder = builder; - AddValidators(); } - public VersionOption(string[] aliases, CommandLineBuilder builder) : base(aliases) + internal VersionOption(string[] aliases, CommandLineBuilder builder) + : base(aliases, builder.LocalizationResources.VersionOptionDescription()) { - _builder = builder; - AddValidators(); } @@ -50,12 +45,6 @@ private static bool IsNotImplicit(SymbolResult symbolResult) }; } - public override string? Description - { - get => _description ??= _builder.LocalizationResources.VersionOptionDescription(); - set => _description = value; - } - internal override bool IsGreedy => false; public override bool Equals(object? obj) => obj is VersionOption; diff --git a/src/System.CommandLine/Symbol.cs b/src/System.CommandLine/Symbol.cs index e43bfb0034..6a93eb4527 100644 --- a/src/System.CommandLine/Symbol.cs +++ b/src/System.CommandLine/Symbol.cs @@ -21,7 +21,7 @@ private protected Symbol() /// /// Gets or sets the description of the symbol. /// - public virtual string? Description { get; set; } + public string? Description { get; set; } /// /// Gets or sets the name of the symbol.