Skip to content

Commit 6d0e218

Browse files
authored
Issue 833: Help Line Wrap (#1604)
* Issue 833: Help Line Wrap - Add support for multiple <para> tags in help text xml documentation - Update command line tests to support no xml, xml without para tags, and xml with para tags * Remove Assertion - Remove assertion causing issues * Fix Line Ending Issue - Replace '\n' with Environment.NewLine to fix platform specific line ending issues - Update test names
1 parent 0c444f0 commit 6d0e218

File tree

3 files changed

+67
-20
lines changed

3 files changed

+67
-20
lines changed

src/System.CommandLine.DragonFruit.Tests/CommandLineTests.cs

Lines changed: 35 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
33

44
using System.CommandLine.Rendering;
5+
using System.ComponentModel;
56
using System.Threading.Tasks;
67
using FluentAssertions;
78
using Xunit;
@@ -24,7 +25,7 @@ public async Task It_executes_method_with_string_option()
2425
{
2526
int exitCode = await CommandLine.InvokeMethodAsync(
2627
new[] { "--name", "Wayne" },
27-
TestProgram.TestMainMethodInfo,
28+
TestProgram.TestMainMethodInfoWithoutPara,
2829
null,
2930
_testProgram,
3031
_terminal);
@@ -37,7 +38,7 @@ public void It_executes_method_synchronously_with_string_option()
3738
{
3839
int exitCode = CommandLine.InvokeMethod(
3940
new[] { "--name", "Wayne" },
40-
TestProgram.TestMainMethodInfo,
41+
TestProgram.TestMainMethodInfoWithoutPara,
4142
null,
4243
_testProgram,
4344
_terminal);
@@ -50,7 +51,7 @@ public async Task It_shows_help_text_based_on_XML_documentation_comments()
5051
{
5152
int exitCode = await CommandLine.InvokeMethodAsync(
5253
new[] { "--help" },
53-
TestProgram.TestMainMethodInfo,
54+
TestProgram.TestMainMethodInfoWithoutPara,
5455
null,
5556
_testProgram,
5657
_terminal);
@@ -60,23 +61,23 @@ public async Task It_shows_help_text_based_on_XML_documentation_comments()
6061
var stdOut = _terminal.Out.ToString();
6162

6263
stdOut.Should()
63-
.Contain("<args> These are arguments")
64-
.And.Contain("Arguments:");
64+
.Contain("<args> These are arguments")
65+
.And.Contain("Arguments:");
6566
stdOut.Should()
66-
.ContainAll("--name <name>", "Specifies the name option")
67-
.And.Contain("Options:");
67+
.ContainAll("--name <name>", "Specifies the name option")
68+
.And.Contain("Options:");
6869
stdOut.Should()
69-
.Contain("Help for the test program");
70+
.Contain($"Description:{Environment.NewLine} Normal summary");
7071
}
71-
72+
7273
[Fact]
73-
public void It_synchronously_shows_help_text_based_on_XML_documentation_comments()
74+
public async Task When_XML_documentation_comment_contains_a_para_tag_then_help_is_written_with_a_newline()
7475
{
75-
int exitCode = CommandLine.InvokeMethod(
76+
int exitCode = await CommandLine.InvokeMethodAsync(
7677
new[] { "--help" },
77-
TestProgram.TestMainMethodInfo,
78+
TestProgram.TestMainMethodInfoWithPara,
7879
null,
79-
_testProgram,
80+
_testProgram,
8081
_terminal);
8182

8283
exitCode.Should().Be(0);
@@ -87,10 +88,29 @@ public void It_synchronously_shows_help_text_based_on_XML_documentation_comments
8788
.Contain("<args> These are arguments")
8889
.And.Contain("Arguments:");
8990
stdOut.Should()
90-
.ContainAll("--name <name>","Specifies the name option")
91+
.ContainAll("--name <name>", "Specifies the name option")
9192
.And.Contain("Options:");
9293
stdOut.Should()
93-
.Contain("Help for the test program");
94+
.Contain($"Description:{Environment.NewLine} Help for the test program{Environment.NewLine} More help for the test program{Environment.NewLine}");
95+
}
96+
97+
[Fact]
98+
public void It_synchronously_shows_help_text_based_on_XML_documentation_comments()
99+
{
100+
int exitCode = CommandLine.InvokeMethod(
101+
new[] { "--help" },
102+
TestProgram.TestMainMethodInfoWithDefault,
103+
null,
104+
_testProgram,
105+
_terminal);
106+
107+
exitCode.Should().Be(0);
108+
109+
var stdOut = _terminal.Out.ToString();
110+
111+
stdOut.Should()
112+
.ContainAll("--name <name>","name [default: Bruce]")
113+
.And.Contain("Options:");
94114
}
95115

96116
[Fact]

src/System.CommandLine.DragonFruit.Tests/TestProgram.cs

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,20 @@ namespace System.CommandLine.DragonFruit.Tests
77
{
88
public class TestProgram
99
{
10-
public static readonly MethodInfo TestMainMethodInfo = typeof(TestProgram).GetMethod(nameof(TestMain));
10+
public static readonly MethodInfo TestMainMethodInfoWithoutPara = typeof(TestProgram).GetMethod(nameof(TestMainWithoutPara));
11+
12+
public static readonly MethodInfo TestMainMethodInfoWithPara = typeof(TestProgram).GetMethod(nameof(TestMainWithPara));
1113

1214
public static readonly MethodInfo TestMainMethodInfoWithDefault = typeof(TestProgram).GetMethod(nameof(TestMainWithDefault));
1315

1416
/// <summary>
15-
/// Help for the test program
17+
/// <para>Help for the test program</para>
18+
/// <para>More help for the test program</para>
1619
/// </summary>
1720
/// <param name="name">Specifies the name option</param>
1821
/// <param name="console"></param>
1922
/// <param name="args">These are arguments</param>
20-
public void TestMain(string name, IConsole console, string[] args = null)
23+
public void TestMainWithPara(string name, IConsole console, string[] args = null)
2124
{
2225
console.Out.Write(name);
2326
if (args != null && args.Length > 0)
@@ -26,6 +29,17 @@ public void TestMain(string name, IConsole console, string[] args = null)
2629
}
2730
}
2831

32+
/// <summary>
33+
/// Normal summary
34+
/// </summary>
35+
/// <param name="name">Specifies the name option</param>
36+
/// <param name="console"></param>
37+
/// <param name="args">These are arguments</param>
38+
public void TestMainWithoutPara(string name, IConsole console, string[] args = null)
39+
{
40+
console.Out.Write(name);
41+
}
42+
2943
public void TestMainWithDefault(string name = "Bruce", IConsole console = null)
3044
{
3145
console?.Out.Write(name);

src/System.CommandLine.DragonFruit/XmlDocReader.cs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,10 +92,23 @@ public bool TryGetMethodDescription(MethodInfo info, out CommandHelpMetadata com
9292
switch (element.Name.ToString())
9393
{
9494
case "summary":
95-
commandHelpMetadata.Description = element.Value?.Trim();
95+
if (element.HasElements)
96+
{
97+
var val = string.Join(string.Empty,
98+
element.Elements().Select(e =>
99+
e.Value + (e.Name.ToString().ToLower() == "para" ? Environment.NewLine : string.Empty)));
100+
commandHelpMetadata.Description = val.TrimEnd(Environment.NewLine.ToCharArray());
101+
}
102+
else
103+
{
104+
commandHelpMetadata.Description = element.Value.Trim();
105+
}
96106
break;
97107
case "param":
98-
commandHelpMetadata.ParameterDescriptions.Add(element.Attribute("name")?.Value, element.Value?.Trim());
108+
var value = element.Attribute("name")?.Value;
109+
if (value != null)
110+
commandHelpMetadata.ParameterDescriptions.Add(value,
111+
element.Value.Trim());
99112
break;
100113
}
101114
}

0 commit comments

Comments
 (0)