Skip to content

Commit 1b57429

Browse files
committed
adjust HelpBuilder.Approval test
1 parent fe5d68b commit 1b57429

File tree

3 files changed

+36
-24
lines changed

3 files changed

+36
-24
lines changed

src/System.CommandLine.Tests/ApprovalTests/Help/Approvals/HelpBuilderTests.Help_describes_default_values_for_complex_root_command_scenario.approved.txt

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,20 @@ the-root-command:
22
Test description
33

44
Usage:
5-
the-root-command [options] <the-root-arg-no-default> [<the-root-arg> [<the-root-arg-enum-default>]]
5+
the-root-command [options] <the-root-arg-no-description-no-default> [<the-root-arg-no-description-default> <the-root-arg-no-default> [<the-root-arg> [<the-root-arg-enum-default>]]]
66

77
Arguments:
8-
<the-root-arg-no-default> the-root-arg-no-default-description
9-
<the-root-arg> the-root-arg-description [default: the-root-arg-one-value]
10-
<Read|ReadWrite|Write> the-root-arg-enum-default-description [default: Read]
8+
<the-root-arg-no-description-no-default>
9+
<the-root-arg-no-description-default> [default: the-root-arg-no-description-default-value]
10+
<the-root-arg-no-default> the-root-arg-no-default-description
11+
<the-root-arg> the-root-arg-description [default: the-root-arg-one-value]
12+
<Read|ReadWrite|Write> the-root-arg-enum-default-description [default: Read]
1113

1214
Options:
13-
-trna, --the-root-option-no-arg (REQUIRED) the-root-option-no-arg-description
14-
-tronda, --the-root-option-no-default-arg <the-root-option-arg-no-default-arg> (REQUIRED) the-root-option-no-default-description
15-
-troda, --the-root-option-default-arg <the-root-option-arg> the-root-option-default-arg-description [default: the-root-option-arg-value]
16-
-troea, --the-root-option-enum-arg <Read|ReadWrite|Write> the-root-option-description [default: Read]
17-
-trorea, --the-root-option-required-enum-arg <Read|ReadWrite|Write> (REQUIRED) the-root-option-description [default: Read]
15+
-trna, --the-root-option-no-arg (REQUIRED) the-root-option-no-arg-description
16+
-trondda, --the-root-option-no-description-default-arg <the-root-option-no-description-default-arg> [default: the-root-option--no-description-default-arg-value]
17+
-tronda, --the-root-option-no-default-arg <the-root-option-arg-no-default-arg> (REQUIRED) the-root-option-no-default-description
18+
-troda, --the-root-option-default-arg <the-root-option-arg> the-root-option-default-arg-description [default: the-root-option-arg-value]
19+
-troea, --the-root-option-enum-arg <Read|ReadWrite|Write> the-root-option-description [default: Read]
20+
-trorea, --the-root-option-required-enum-arg <Read|ReadWrite|Write> (REQUIRED) the-root-option-description [default: Read]
1821

src/System.CommandLine.Tests/ApprovalTests/Help/HelpBuilderTests.Approval.cs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,22 @@
55
using System.CommandLine.Help;
66
using System.IO;
77
using ApprovalTests;
8-
8+
using ApprovalTests.Reporters;
99

1010
namespace System.CommandLine.Tests.Help
1111
{
1212
public partial class HelpBuilderTests
1313
{
14+
[UseReporter(typeof(DiffReporter))]
1415
[Fact]
1516
public void Help_describes_default_values_for_complex_root_command_scenario()
1617
{
1718
var command = new RootCommand(description: "Test description")
1819
{
20+
new Argument<string>("the-root-arg-no-description-no-default"),
21+
new Argument<string>("the-root-arg-no-description-default",
22+
argResult => "the-root-arg-no-description-default-value",
23+
isDefault: true),
1924
new Argument<string>("the-root-arg-no-default")
2025
{
2126
Description = "the-root-arg-no-default-description",
@@ -33,6 +38,11 @@ public void Help_describes_default_values_for_complex_root_command_scenario()
3338
Description = "the-root-option-no-arg-description",
3439
Required = true
3540
},
41+
new Option<string>(
42+
aliases: new string[] {"--the-root-option-no-description-default-arg", "-trondda"},
43+
parseArgument: _ => "the-root-option--no-description-default-arg-value",
44+
isDefault: true
45+
),
3646
new Option(aliases: new string[] {"--the-root-option-no-default-arg", "-tronda"}) {
3747
Description = "the-root-option-no-default-description",
3848
Argument = new Argument<string>("the-root-option-arg-no-default-arg")
@@ -53,15 +63,13 @@ public void Help_describes_default_values_for_complex_root_command_scenario()
5363
Argument = new Argument<FileAccess>("the-root-option-arg", () => FileAccess.Read)
5464
{
5565
Description = "the-root-option-arg-description",
56-
ArgumentType = typeof(FileAccess),
5766
},
5867
},
5968
new Option(aliases: new string[] {"--the-root-option-required-enum-arg", "-trorea"}) {
6069
Description = "the-root-option-description",
6170
Argument = new Argument<FileAccess>("the-root-option-arg", () => FileAccess.Read)
6271
{
6372
Description = "the-root-option-arg-description",
64-
ArgumentType = typeof(FileAccess),
6573
},
6674
Required = true
6775
}

src/System.CommandLine/Help/HelpBuilder.cs

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,11 @@ public class HelpBuilder : IHelpBuilder
2020

2121
protected IConsole Console { get; }
2222

23-
public int ColumnGutter { get; }
23+
public int ColumnGutter { get; }
2424

25-
public int IndentationSize { get; }
25+
public int IndentationSize { get; }
2626

27-
public int MaxWidth { get; }
27+
public int MaxWidth { get; }
2828

2929
/// <summary>
3030
/// Brokers the generation and output of help text of <see cref="Symbol"/>
@@ -215,10 +215,11 @@ protected void AppendHelpItem(HelpItem helpItem, int maxInvocationWidth)
215215
var offset = maxInvocationWidth + ColumnGutter - helpItem.Invocation.Length;
216216
var availableWidth = GetAvailableWidth();
217217
var maxDescriptionWidth = availableWidth - maxInvocationWidth - ColumnGutter;
218-
var descriptionColumn = helpItem.Description;
218+
var postfix = string.IsNullOrEmpty(helpItem.Description) ? string.Empty : " ";
219+
var descriptionColumn = helpItem.Description + postfix;
219220
if (helpItem.HasDefaultValueHint)
220221
{
221-
descriptionColumn += " " + helpItem.DefaultValueHint;
222+
descriptionColumn += helpItem.DefaultValueHint;
222223
}
223224
var descriptionLines = SplitText(descriptionColumn, maxDescriptionWidth);
224225
var lineCount = descriptionLines.Count;
@@ -257,7 +258,7 @@ protected virtual IReadOnlyCollection<string> SplitText(string text, int maxLeng
257258

258259
if (string.IsNullOrWhiteSpace(cleanText) || textLength < maxLength)
259260
{
260-
return new[] {cleanText};
261+
return new[] { cleanText };
261262
}
262263

263264
var lines = new List<string>();
@@ -298,7 +299,7 @@ private IEnumerable<HelpItem> GetArgumentHelpItems(ISymbol symbol)
298299
{
299300
foreach (var argument in symbol.Arguments())
300301
{
301-
if(ShouldShowHelp(argument))
302+
if (ShouldShowHelp(argument))
302303
{
303304
var argumentDescriptor = ArgumentDescriptor(argument);
304305

@@ -323,7 +324,7 @@ private IEnumerable<HelpItem> GetArgumentHelpItems(ISymbol symbol)
323324

324325
protected virtual string ArgumentDescriptor(IArgument argument)
325326
{
326-
if (argument.ValueType == typeof(bool) || argument.ValueType == typeof(bool?) )
327+
if (argument.ValueType == typeof(bool) || argument.ValueType == typeof(bool?))
327328
{
328329
return "";
329330
}
@@ -459,7 +460,7 @@ protected virtual void AddUsage(ICommand command)
459460
{
460461
usage.Add(Usage.Options);
461462
}
462-
463+
463464
usage.Add(FormatArgumentUsage(command.Arguments.ToArray()));
464465

465466
var hasCommandHelp = command.Children
@@ -609,14 +610,14 @@ private bool ShouldDisplayArgumentHelp(ICommand? command)
609610

610611
private int GetConsoleWindowWidth()
611612
{
612-
try
613+
try
613614
{
614615
return System.Console.WindowWidth;
615616
}
616617
catch (System.IO.IOException)
617618
{
618619
return int.MaxValue;
619-
}
620+
}
620621
}
621622

622623
protected class HelpItem
@@ -640,7 +641,7 @@ public HelpItem(
640641
protected bool Equals(HelpItem other) =>
641642
(Invocation, Description) == (other.Invocation, other.Description);
642643

643-
public override bool Equals(object obj) => Equals((HelpItem) obj);
644+
public override bool Equals(object obj) => Equals((HelpItem)obj);
644645

645646
public override int GetHashCode() => (Invocation, Description).GetHashCode();
646647

0 commit comments

Comments
 (0)