8000 Wrap `HelpItem`s to table. (#629) · jonsequitur/command-line-api@96da131 · GitHub
[go: up one dir, main page]

Skip to content

Commit 96da131

Browse files
apogeeoakjonsequitur
authored andcommitted
Wrap HelpItems to table. (dotnet#629)
1 parent 357065e commit 96da131

File tree

3 files changed

+304
-129
lines changed

3 files changed

+304
-129
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ Usage:
55
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-description-no-default>
8+
<the-root-arg-no-description-no-default>
99
<the-root-arg-no-description-default> [default: the-root-arg-no-description-default-value]
1010
<the-root-arg-no-default> the-root-arg-no-default-description
1111
<the-root-arg> the-root-arg-description [default: the-root-arg-one-value]

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

Lines changed: 112 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -111,9 +111,9 @@ public void Synopsis_section_properly_wraps_description()
111111
public void Command_name_in_synopsis_can_be_specified()
112112
{
113113
var command = new RootCommand
114-
{
115-
Name = "custom-name"
116-
};
114+
{
115+
Name = "custom-name"
116+
};
117117

118118
var helpBuilder = GetHelpBuilder(SmallMaxWidth);
119119
helpBuilder.Write(command);
@@ -138,10 +138,10 @@ public void Usage_section_shows_arguments_if_there_are_arguments_for_command_whe
138138
string expectedDescriptor)
139139
{
140140
var argument = new Argument
141-
{
142-
Name = "the-args",
143-
Arity = new ArgumentArity(minArity, maxArity)
144-
};
141+
{
142+
Name = "the-args",
143+
Arity = new ArgumentArity(minArity, maxArity)
144+
};
145145
var command = new Command("the-command", "command help")
146146
{
147147
argument,
@@ -178,18 +178,18 @@ public void Usage_section_shows_arguments_if_there_are_arguments_for_command_whe
178178
string expectedDescriptor)
179179
{
180180
var arg1 = new Argument
181-
{
182-
Name = "arg1",
183-
Arity = new ArgumentArity(
184-
minArityForArg1,
185-
maxArityForArg1)
186-
};
181+
{
182+
Name = "arg1",
183+
Arity = new ArgumentArity(
184+
minArityForArg1,
185+
maxArityForArg1)
186+
};
187187
var arg2 = new Argument
188-
{
189-
Name = "arg2",
190-
Arity = new ArgumentArity(
191-
minArityForArg2,
192-
maxArityForArg2)
188+
{
189+
Name = "arg2",
190+
Arity = new ArgumentArity(
191+
minArityForArg2,
192+
maxArityForArg2)
193193
};
194194
var command = new Command("the-command", "command help")
195195
{
@@ -426,7 +426,7 @@ public void Usage_section_does_not_contain_hidden_argument()
426426
var command = new Command(commandName, "Does things");
427427
var hiddenArg = new Argument<int>
428428
{
429-
Name = "hidden",
429+
Name = "hidden",
430430
IsHidden = true
431431
};
432432
var visibleArg = new Argument<int>
@@ -442,7 +442,7 @@ public void Usage_section_does_not_contain_hidden_argument()
442442
var expected =
443443
$"Usage:{NewLine}" +
444444
$"{_indentation}{commandName} <{visibleArgName}>{NewLine}{NewLine}";
445-
445+
446446
_console.Out.ToString().Should().Contain(expected);
447447
}
448448

@@ -465,7 +465,7 @@ public void Arguments_section_is_not_included_if_there_are_commands_but_no_argum
465465

466466
_helpBuilder.Write(command);
467467
_console.Out.ToString().Should().NotContain("Arguments:");
468-
468+
469469
_helpBuilder.Write(command);
470470
_console.Out.ToString().Should().NotContain("Arguments:");
471471
}
@@ -534,7 +534,7 @@ public void Arguments_section_includes_configured_argument_aliases()
534534
Description = "Sets the verbosity."
535535
}
536536
};
537-
537+
538538
_helpBuilder.Write(command);
539539

540540
var help = _console.Out.ToString();
@@ -589,14 +589,14 @@ public void Arguments_section_does_not_contain_hidden_argument()
589589

590590
var expected =
591591
$"Arguments:{NewLine}" +
592-
$"{_indentation}<{visibleArgName}>{_columnPadding}{visibleDesc}{NewLine}{NewLine}";
592+
$"{_indentation}<{visibleArgName}>{_columnPadding}{visibleDesc}{NewLine}{NewLine}";
593593

594594
_helpBuilder.Write(command);
595595
var help = _console.Out.ToString();
596596

597597
help.Should().Contain(expected);
598598
help.Should().NotContain(hiddenArgName);
599-
help.Should().NotContain(hiddenDesc);
599+
help.Should().NotContain(hiddenDesc);
600600
}
601601

602602
[Fact]
@@ -736,6 +736,34 @@ public void Arguments_section_properly_wraps_description()
736736
_console.Out.ToString().Should().Contain(expected);
737737
}
738738

739+
[Fact]
740+
public void Arguments_section_properly_wraps()
741+
{
742+
var name = "argument-name-for-a-command-that-is-long-enough-to-wrap-to-a-new-line";
743+
var description = "Argument description for a command with line breaks that is long enough to wrap to a new line.";
744+
745+
var command = new RootCommand()
746+
{
747+
new Argument
748+
{
749+
Name = name,
750+
Description = description
751+
}
752+
};
753+
754+
HelpBuilder helpBuilder = GetHelpBuilder(SmallMaxWidth);
755+
helpBuilder.Write(command);
756+
757+
var expected =
758+
$"Arguments:{NewLine}" +
759+
$"{_indentation}<argument-name-for-a-command-th{_columnPadding}Argument description for a{NewLine}" +
760+
$"{_indentation}at-is-long-enough-to-wrap-to-a-{_columnPadding}command with line breaks that{NewLine}" +
761+
$"{_indentation}new-line> {_columnPadding}is long enough to wrap to a new{NewLine}" +
762+
$"{_indentation} {_columnPadding}line.{NewLine}{NewLine}";
763+
764+
_console.Out.ToString().Should().Contain(expected);
765+
}
766+
739767
[Theory]
740768
[InlineData(typeof(bool))]
741769
[InlineData(typeof(bool?))]
@@ -813,10 +841,10 @@ public void Option_argument_descriptor_is_empty_for_boolean_values(Type type)
813841
HelpBuilder helpBuilder = GetHelpBuilder(SmallMaxWidth);
814842

815843
helpBuilder.Write(command);
816-
844+
817845
_console.Out.ToString().Should().Contain($"--opt{_columnPadding}{description}");
818846
}
819-
847+
820848
[Theory]
821849
[InlineData(typeof(FileAccess))]
822850
[InlineData(typeof(FileAccess?))]
@@ -947,7 +975,7 @@ public void Options_section_does_not_contain_option_with_HelpDefinition_that_IsH
947975
{
948976
IsHidden = false
949977
});
950-
978+
951979

952980
_helpBuilder.Write(command);
953981

@@ -1084,6 +1112,29 @@ public void Options_section_properly_wraps_description()
10841112
_console.Out.ToString().Should().Contain(expected);
10851113
}
10861114

1115+
[Fact]
1116+
public void Options_section_properly_wraps()
1117+
{
1118+
var alias = "--option-alias-for-a-command-that-is-long-enough-to-wrap-to-a-new-line";
1119+
var description = "Option description that is long enough to wrap.";
1120+
1121+
var command = new RootCommand()
1122+
{
1123+
new Option(alias, description)
1124+
};
1125+
1126+
HelpBuilder helpBuilder = GetHelpBuilder(SmallMaxWidth);
1127+
helpBuilder.Write(command);
1128+
1129+
var expected =
1130+
$"Options:{NewLine}" +
1131+
$"{_indentation}--option-alias-for-a-command-th{_columnPadding}Option description that is long{NewLine}" +
1132+
$"{_indentation}at-is-long-enough-to-wrap-to-a-{_columnPadding}enough to wrap.{NewLine}" +
1133+
$"{_indentation}new-line{NewLine}{NewLine}";
1134+
1135+
_console.Out.ToString().Should().Contain(expected);
1136+
}
1137+
10871138
[Fact]
10881139
public void Options_section_does_not_contain_hidden_argument()
10891140
{
@@ -1126,13 +1177,13 @@ public void Required_options_are_indicated()
11261177
};
11271178

11281179
_helpBuilder.Write(command);
1129-
1180+
11301181
var help = _console.Out.ToString();
11311182

11321183
help.Should()
11331184
.Contain("--required (REQUIRED)");
11341185
}
1135-
1186+
11361187
[Fact]
11371188
public void Required_options_are_indicated_when_argument_is_named()
11381189
{
@@ -1146,7 +1197,7 @@ public void Required_options_are_indicated_when_argument_is_named()
11461197
};
11471198

11481199
_helpBuilder.Write(command);
1149-
1200+
11501201
var help = _console.Out.ToString();
11511202

11521203
help.Should()
@@ -1177,12 +1228,12 @@ public void Options_aliases_differing_only_by_prefix_are_deduplicated_favoring_d
11771228
};
11781229

11791230
_helpBuilder.Write(command);
1180-
1231+
11811232
var help = _console.Out.ToString();
11821233

11831234
help.Should().NotContain("/x");
11841235
}
1185-
1236+
11861237
[Fact]
11871238
public void Options_aliases_differing_only_by_prefix_are_deduplicated_favoring_double_dashed_prefixes()
11881239
{
@@ -1192,7 +1243,7 @@ public void Options_aliases_differing_only_by_prefix_are_deduplicated_favoring_d
11921243
};
11931244

11941245
_helpBuilder.Write(command);
1195-
1246+
11961247
var help = _console.Out.ToString();
11971248

11981249
help.Should().NotContain("/long");
@@ -1265,7 +1316,7 @@ public void Help_describes_default_value_for_option_with_argument_having_default
12651316
}
12661317

12671318
[Fact]
1268-
public void Help_should_not_contain_default_value_for_hidden_argument_defined_for_option ()
1319+
public void Help_should_not_contain_default_value_for_hidden_argument_defined_for_option()
12691320
{
12701321
var argument = new Argument
12711322
{
@@ -1427,7 +1478,7 @@ public void Subcommands_properly_wraps_description()
14271478
}
14281479
};
14291480

1430-
helpBuilder.Write(command);
1481+
helpBuilder.Write(command);
14311482

14321483
var expected =
14331484
$"Commands:{NewLine}" +
@@ -1437,6 +1488,29 @@ public void Subcommands_properly_wraps_description()
14371488
_console.Out.ToString().Should().Contain(expected);
14381489
}
14391490

1491+
[Fact]
1492+
public void Subcommands_section_properly_wraps()
1493+
{
1494+
var name = "subcommand-name-that-is-long-enough-to-wrap-to-a-new-line";
1495+
var description = "Subcommand description that is really long. So long that it caused the line to wrap.";
1496+
1497+
var command = new RootCommand()
1498+
{
1499+
new Command(name, description)
1500+
};
1501+
1502+
var helpBuilder = GetHelpBuilder(SmallMaxWidth);
1503+
helpBuilder.Write(command);
1504+
1505+
var expected =
1506+
$"Commands:{NewLine}" +
1507+
$"{_indentation}subcommand-name-that-is-long-en{_columnPadding}Subcommand description that is{NewLine}" +
1508+
$"{_indentation}ough-to-wrap-to-a-new-line {_columnPadding}really long. So long that it{NewLine}" +
1509+
$"{_indentation} {_columnPadding}caused the line to wrap.{NewLine}{NewLine}";
1510+
1511+
_console.Out.ToString().Should().Contain(expected);
1512+
}
1513+
14401514
[Fact]
14411515
public void Subcommand_help_contains_command_with_empty_description()
14421516
{
@@ -1479,12 +1553,12 @@ public void Subcommand_help_does_not_contain_hidden_argument()
14791553
var subCommand = new Command("the-subcommand");
14801554
var hidden = new Argument<int>()
14811555
{
1482-
Name = "the-hidden",
1556+
Name = "the-hidden",
14831557
IsHidden = true
14841558
};
14851559
var visible = new Argument<int>()
14861560
{
1487-
Name = "the-visible",
1561+
Name = "the-visible",
14881562
IsHidden = false
14891563
};
14901564
subCommand.AddArgument(hidden);
@@ -1557,7 +1631,7 @@ public void Help_describes_default_value_for_subcommand_with_arguments_and_only_
15571631
}
15581632

15591633
[Fact]
1560-
public void Help_describes_default_values_for_subcommand_with_multiple_defaultable_arguments ()
1634+
public void Help_describes_default_values_for_subcommand_with_multiple_defaultable_arguments()
15611635
{
15621636
var argument = new Argument
15631637
{

0 commit comments

Comments
 (0)
0