Skip to content

Commit 723cd2c

Browse files
authored
Removes the custom GetHashCode and Equals overrides in HelpOption and VerboseOption (#2159)
* fix #2118 This removes the custom GetHashCode and Equals overrides in HelpOption and VerboseOption. * fix #2116
1 parent 7f19ebf commit 723cd2c

File tree

6 files changed

+18
-31
lines changed

6 files changed

+18
-31
lines changed

src/System.CommandLine.ApiCompatibility.Tests/ApiCompatibilityApprovalTests.System_CommandLine_api_is_not_changed.approved.txt

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -155,8 +155,6 @@ System.CommandLine
155155
.ctor()
156156
.ctor(System.String name, System.String[] aliases)
157157
public CliAction Action { get; set; }
158-
public System.Boolean Equals(System.Object obj)
159-
public System.Int32 GetHashCode()
160158
System.CommandLine.Completions
161159
public class CompletionContext
162160
public static CompletionContext Empty { get; }
@@ -220,8 +218,6 @@ System.CommandLine.Help
220218
.ctor()
221219
.ctor(System.String name, System.String[] aliases)
222220
public System.CommandLine.CliAction Action { get; set; }
223-
public System.Boolean Equals(System.Object obj)
224-
public System.Int32 GetHashCode()
225221
public class TwoColumnHelpRow, System.IEquatable<TwoColumnHelpRow>
226222
.ctor(System.String firstColumnText, System.String secondColumnText)
227223
public System.String FirstColumnText { get; }

src/System.CommandLine.Tests/Help/HelpBuilderTests.Customization.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -405,6 +405,8 @@ public void Help_sections_can_be_supplemented()
405405
Output = new StringWriter(),
406406
};
407407

408+
var defaultHelp = GetDefaultHelp(config.RootCommand);
409+
408410
ParseResult parseResult = config.Parse("-h");
409411

410412
if (parseResult.Action is HelpAction helpAction)
@@ -415,7 +417,6 @@ public void Help_sections_can_be_supplemented()
415417
parseResult.Invoke();
416418

417419
var output = config.Output.ToString();
418-
var defaultHelp = GetDefaultHelp(config.RootCommand);
419420

420421
var expected = $"first{NewLine}{NewLine}{defaultHelp}last{NewLine}{NewLine}";
421422

src/System.CommandLine/Help/HelpBuilder.Default.cs

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -181,16 +181,20 @@ public static Action<HelpContext> SubcommandsSection() =>
181181
public static Action<HelpContext> OptionsSection() =>
182182
ctx =>
183183
{
184-
// by making this logic more complex, we were able to get some nice perf wins elsewhere
185-
List<TwoColumnHelpRow> options = new();
186-
HashSet<CliOption> uniqueOptions = new();
184+
List<TwoColumnHelpRow> optionRows = new();
185+
bool addedHelpOption = false;
186+
187187
if (ctx.Command.HasOptions)
188188
{
189189
foreach (CliOption option in ctx.Command.Options)
190190
{
191-
if (!option.Hidden && uniqueOptions.Add(option))
191+
if (!option.Hidden)
192192
{
193-
options.Add(ctx.HelpBuilder.GetTwoColumnRow(option, ctx));
193+
optionRows.Add(ctx.HelpBuilder.GetTwoColumnRow(option, ctx));
194+
if (option is HelpOption)
195+
{
196+
addedHelpOption = true;
197+
}
194198
}
195199
}
196200
}
@@ -209,9 +213,12 @@ public static Action<HelpContext> OptionsSection() =>
209213
foreach (var option in parentCommand.Options)
210214
{
211215
// global help aliases may be duplicated, we just ignore them
212-
if (option.Recursive && !option.Hidden && uniqueOptions.Add(option))
216+
if (option is { Recursive: true, Hidden: false })
213217
{
214-
options.Add(ctx.HelpBuilder.GetTwoColumnRow(option, ctx));
218+
if (option is not HelpOption || !addedHelpOption)
219+
{
220+
optionRows.Add(ctx.HelpBuilder.GetTwoColumnRow(option, ctx));
221+
}
215222
}
216223
}
217224
}
@@ -223,14 +230,14 @@ public static Action<HelpContext> OptionsSection() =>
223230
current = parentCommand;
224231
}
225232

226-
if (options.Count <= 0)
233+
if (optionRows.Count <= 0)
227234
{
228235
ctx.WasSectionSkipped = true;
229236
return;
230237
}
231238

232239
ctx.HelpBuilder.WriteHeading(LocalizationResources.HelpOptionsTitle(), null, ctx.Output);
233-
ctx.HelpBuilder.WriteColumns(options, ctx);
240+
ctx.HelpBuilder.WriteColumns(optionRows, ctx);
234241
ctx.Output.WriteLine();
235242
};
236243

src/System.CommandLine/Help/HelpBuilder.cs

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,6 @@ public virtual void Write(HelpContext context)
4848
return;
4949
}
5050

51-
if (OnCustomize is {})
52-
{
53-
OnCustomize(context);
54-
}
55-
5651
foreach (var writeSection in GetLayout(context))
5752
{
5853
writeSection(context);
@@ -66,8 +61,6 @@ public virtual void Write(HelpContext context)
6661
context.Output.WriteLine();
6762
}
6863

69-
internal Action<HelpContext>? OnCustomize { get; set; }
70-
7164
/// <summary>
7265
/// Specifies custom help details for a specific symbol.
7366
/// </summary>

src/System.CommandLine/Help/HelpOption.cs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,5 @@ public override CliAction? Action
3737
get => _action ??= new HelpAction();
3838
set => _action = value ?? throw new ArgumentNullException(nameof(value));
3939
}
40-
41-
internal override bool Greedy => false;
42-
43-
public override bool Equals(object? obj) => obj is HelpOption;
44-
45-
public override int GetHashCode() => typeof(HelpOption).GetHashCode();
4640
}
4741
}

src/System.CommandLine/VersionOption.cs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,6 @@ private static bool NotImplicit(SymbolResult symbolResult)
6161

6262
internal override bool Greedy => false;
6363

64-
public override bool Equals(object? obj) => obj is VersionOption;
65-
66-
public override int GetHashCode() => typeof(VersionOption).GetHashCode();
67-
6864
private sealed class VersionOptionAction : CliAction
6965
{
7066
public override int Invoke(ParseResult parseResult)

0 commit comments

Comments
 (0)