Skip to content

Commit

Permalink
Symbol.Description does not need to be virtual, we can pass the descr…
Browse files Browse the repository at this point in the history
…iption to ctor when creating every Symbol (#2045)

the affected ctors belonged to internal types
  • Loading branch information
adamsitnik authored Feb 22, 2023
1 parent 6462288 commit 8e8c69a
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ void Default(Exception exception, InvocationContext context)
/// <returns>The same instance of <see cref="CommandLineBuilder"/>.</returns>
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);
}

/// <summary>
Expand All @@ -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));
}

/// <summary>
Expand All @@ -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;
Expand Down
18 changes: 4 additions & 14 deletions src/System.CommandLine/Help/HelpOption.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,31 +8,21 @@ namespace System.CommandLine.Help
{
internal class HelpOption : Option<bool>
{
private readonly Func<LocalizationResources> _localizationResources;
private string? _description;

public HelpOption(string[] aliases, Func<LocalizationResources> getLocalizationResources)
: base(aliases, null, new Argument<bool> { Arity = ArgumentArity.Zero })
internal HelpOption(string[] aliases, LocalizationResources localizationResources)
: base(aliases, localizationResources.HelpOptionDescription(), new Argument<bool> { Arity = ArgumentArity.Zero })
{
_localizationResources = getLocalizationResources;
AppliesToSelfAndChildren = true;
}

public HelpOption(Func<LocalizationResources> 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;
Expand Down
19 changes: 4 additions & 15 deletions src/System.CommandLine/Help/VersionOption.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,15 @@ namespace System.CommandLine.Help
{
internal class VersionOption : Option<bool>
{
private readonly CommandLineBuilder _builder;
private string? _description;

public VersionOption(CommandLineBuilder builder) : base("--version", null, new Argument<bool> { Arity = ArgumentArity.Zero })
internal VersionOption(CommandLineBuilder builder)
: base("--version", builder.LocalizationResources.VersionOptionDescription(), new Argument<bool> { 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();
}

Expand All @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/System.CommandLine/Symbol.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ private protected Symbol()
/// <summary>
/// Gets or sets the description of the symbol.
/// </summary>
public virtual string? Description { get; set; }
public string? Description { get; set; }

/// <summary>
/// Gets or sets the name of the symbol.
Expand Down

0 comments on commit 8e8c69a

Please sign in to comment.