Skip to content

Commit 1a3249c

Browse files
Improve XmlDoc output (#1503)
* Add command description and examples in XML Output Closes #1115
1 parent 43f9ae9 commit 1a3249c

File tree

11 files changed

+87
-0
lines changed

11 files changed

+87
-0
lines changed

src/Spectre.Console.Cli/Internal/Commands/XmlDocCommand.cs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,13 @@ private static XmlNode CreateCommandNode(XmlDocument doc, CommandInfo command, b
8484

8585
node.SetNullableAttribute("Settings", command.SettingsType?.FullName);
8686

87+
if (!string.IsNullOrWhiteSpace(command.Description))
88+
{
89+
var descriptionNode = doc.CreateElement("Description");
90+
descriptionNode.InnerText = command.Description;
91+
node.AppendChild(descriptionNode);
92+
}
93+
8794
// Parameters
8895
if (command.Parameters.Count > 0)
8996
{
@@ -103,6 +110,27 @@ private static XmlNode CreateCommandNode(XmlDocument doc, CommandInfo command, b
103110
node.AppendChild(CreateCommandNode(doc, childCommand));
104111
}
105112

113+
// Examples
114+
if (command.Examples.Count > 0)
115+
{
116+
var exampleRootNode = doc.CreateElement("Examples");
117+
foreach (var example in command.Examples.SelectMany(static x => x))
118+
{
119+
var exampleNode = CreateExampleNode(doc, example);
120+
exampleRootNode.AppendChild(exampleNode);
121+
}
122+
123+
node.AppendChild(exampleRootNode);
124+
}
125+
126+
return node;
127+
}
128+
129+
private static XmlNode CreateExampleNode(XmlDocument document, string example)
130+
{
131+
var node = document.CreateElement("Example");
132+
node.SetAttribute("commandLine", example);
133+
106134
return node;
107135
}
108136

test/Spectre.Console.Cli.Tests/Expectations/Xml/Test_1.Output.verified.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,15 @@
2121
</Parameters>
2222
<!--DOG-->
2323
<Command Name="dog" IsBranch="false" ClrType="Spectre.Console.Tests.Data.DogCommand" Settings="Spectre.Console.Tests.Data.DogSettings">
24+
<Description>The dog command.</Description>
2425
<Parameters>
2526
<Argument Name="AGE" Position="0" Required="true" Kind="scalar" ClrType="System.Int32" />
2627
<Option Short="g" Long="good-boy" Value="NULL" Required="false" Kind="flag" ClrType="System.Boolean" />
2728
</Parameters>
2829
</Command>
2930
<!--HORSE-->
3031
<Command Name="horse" IsBranch="false" ClrType="Spectre.Console.Tests.Data.HorseCommand" Settings="Spectre.Console.Tests.Data.HorseSettings">
32+
<Description>The horse command.</Description>
3133
<Parameters>
3234
<Option Short="d" Long="day" Value="MON|TUE" Required="false" Kind="scalar" ClrType="System.DayOfWeek" />
3335
<Option Short="" Long="directory" Value="NULL" Required="false" Kind="scalar" ClrType="System.IO.DirectoryInfo" />
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Model>
3+
<!--DOG-->
4+
<Command Name="dog" IsBranch="false" ClrType="Spectre.Console.Tests.Data.DogCommand" Settings="Spectre.Console.Tests.Data.DogSettings">
5+
<Description>The dog command.</Description>
6+
<Parameters>
7+
<Argument Name="LEGS" Position="0" Required="false" Kind="scalar" ClrType="System.Int32">
8+
<Description>The number of legs.</Description>
9+
<Validators>
10+
<Validator ClrType="Spectre.Console.Tests.Data.EvenNumberValidatorAttribute" Message="Animals must have an even number of legs." />
11+
<Validator ClrType="Spectre.Console.Tests.Data.PositiveNumberValidatorAttribute" Message="Number of legs must be greater than 0." />
12+
</Validators>
13+
</Argument>
14+
<Argument Name="AGE" Position="1" Required="true" Kind="scalar" ClrType="System.Int32" />
15+
<Option Short="a" Long="alive,not-dead" Value="NULL" Required="false" Kind="flag" ClrType="System.Boolean">
16+
<Description>Indicates whether or not the animal is alive.</Description>
17+
</Option>
18+
<Option Short="g" Long="good-boy" Value="NULL" Required="false" Kind="flag" ClrType="System.Boolean" />
19+
<Option Short="n,p" Long="name,pet-name" Value="VALUE" Required="false" Kind="scalar" ClrType="System.String" />
20+
</Parameters>
21+
<Examples>
22+
<Example commandLine="dog -g" />
23+
<Example commandLine="dog --good-boy" />
24+
</Examples>
25+
</Command>
26+
</Model>

test/Spectre.Console.Cli.Tests/Expectations/Xml/Test_2.Output.verified.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
<Model>
33
<!--DOG-->
44
<Command Name="dog" IsBranch="false" ClrType="Spectre.Console.Tests.Data.DogCommand" Settings="Spectre.Console.Tests.Data.DogSettings">
5+
<Description>The dog command.</Description>
56
<Parameters>
67
<Argument Name="LEGS" Position="0" Required="false" Kind="scalar" ClrType="System.Int32">
78
<Description>The number of legs.</Description>

test/Spectre.Console.Cli.Tests/Expectations/Xml/Test_3.Output.verified.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
</Parameters>
1717
<!--DOG-->
1818
<Command Name="dog" IsBranch="false" ClrType="Spectre.Console.Tests.Data.DogCommand" Settings="Spectre.Console.Tests.Data.DogSettings">
19+
<Description>The dog command.</Description>
1920
<Parameters>
2021
<Argument Name="AGE" Position="0" Required="true" Kind="scalar" ClrType="System.Int32" />
2122
<Option Short="g" Long="good-boy" Value="NULL" Required="false" Kind="flag" ClrType="System.Boolean" />
@@ -24,6 +25,7 @@
2425
</Command>
2526
<!--HORSE-->
2627
<Command Name="horse" IsBranch="false" ClrType="Spectre.Console.Tests.Data.HorseCommand" Settings="Spectre.Console.Tests.Data.HorseSettings">
28+
<Description>The horse command.</Description>
2729
<Parameters>
2830
<Option Short="d" Long="day" Value="MON|TUE" Required="false" Kind="scalar" ClrType="System.DayOfWeek" />
2931
<Option Short="" Long="directory" Value="NULL" Required="false" Kind="scalar" ClrType="System.IO.DirectoryInfo" />

test/Spectre.Console.Cli.Tests/Expectations/Xml/Test_4.Output.verified.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
</Parameters>
1717
<!--DOG-->
1818
<Command Name="dog" IsBranch="false" ClrType="Spectre.Console.Tests.Data.DogCommand" Settings="Spectre.Console.Tests.Data.DogSettings">
19+
<Description>The dog command.</Description>
1920
<Parameters>
2021
<Argument Name="AGE" Position="0" Required="true" Kind="scalar" ClrType="System.Int32" />
2122
<Option Short="g" Long="good-boy" Value="NULL" Required="false" Kind="flag" ClrType="System.Boolean" />

test/Spectre.Console.Cli.Tests/Expectations/Xml/Test_6.Output.verified.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
<Model>
33
<!--DEFAULT COMMAND-->
44
<Command Name="__default_command" IsBranch="false" IsDefault="true" ClrType="Spectre.Console.Tests.Data.DogCommand" Settings="Spectre.Console.Tests.Data.DogSettings">
5+
<Description>The dog command.</Description>
56
<Parameters>
67
<Argument Name="LEGS" Position="0" Required="false" Kind="scalar" ClrType="System.Int32">
78
<Description>The number of legs.</Description>
@@ -20,6 +21,7 @@
2021
</Command>
2122
<!--HORSE-->
2223
<Command Name="horse" IsBranch="false" ClrType="Spectre.Console.Tests.Data.HorseCommand" Settings="Spectre.Console.Tests.Data.HorseSettings">
24+
<Description>The horse command.</Description>
2325
<Parameters>
2426
<Argument Name="LEGS" Position="0" Required="false" Kind="scalar" ClrType="System.Int32">
2527
<Description>The number of legs.</Description>

test/Spectre.Console.Cli.Tests/Expectations/Xml/Test_7.Output.verified.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
</Parameters>
2222
<!--__DEFAULT_COMMAND-->
2323
<Command Name="__default_command" IsBranch="false" ClrType="Spectre.Console.Tests.Data.HorseCommand" Settings="Spectre.Console.Tests.Data.HorseSettings">
24+
<Description>The horse command.</Description>
2425
<Parameters>
2526
<Option Short="d" Long="day" Value="MON|TUE" Required="false" Kind="scalar" ClrType="System.DayOfWeek" />
2627
<Option Short="" Long="directory" Value="NULL" Required="false" Kind="scalar" ClrType="System.IO.DirectoryInfo" />

test/Spectre.Console.Cli.Tests/Expectations/Xml/Test_8.Output.verified.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
</Parameters>
1717
<!--DOG-->
1818
<Command Name="dog" IsBranch="false" ClrType="Spectre.Console.Tests.Data.DogCommand" Settings="Spectre.Console.Tests.Data.DogSettings">
19+
<Description>The dog command.</Description>
1920
<Parameters>
2021
<Argument Name="AGE" Position="0" Required="true" Kind="scalar" ClrType="System.Int32" />
2122
<Option Short="g" Long="good-boy" Value="NULL" Required="false" Kind="flag" ClrType="System.Boolean" />
@@ -24,6 +25,7 @@
2425
</Command>
2526
<!--__DEFAULT_COMMAND-->
2627
<Command Name="__default_command" IsBranch="false" ClrType="Spectre.Console.Tests.Data.HorseCommand" Settings="Spectre.Console.Tests.Data.HorseSettings">
28+
<Description>The horse command.</Description>
2729
<Parameters>
2830
<Option Short="d" Long="day" Value="MON|TUE" Required="false" Kind="scalar" ClrType="System.DayOfWeek" />
2931
<Option Short="" Long="directory" Value="NULL" Required="false" Kind="scalar" ClrType="System.IO.DirectoryInfo" />

test/Spectre.Console.Cli.Tests/Expectations/Xml/Test_9.Output.verified.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
</Parameters>
1919
<!--DOG-->
2020
<Command Name="dog" IsBranch="false" ClrType="Spectre.Console.Tests.Data.DogCommand" Settings="Spectre.Console.Tests.Data.DogSettings">
21+
<Description>The dog command.</Description>
2122
<Parameters>
2223
<Argument Name="AGE" Position="0" Required="true" Kind="scalar" ClrType="System.Int32" />
2324
<Option Short="g" Long="good-boy" Value="NULL" Required="false" Kind="flag" ClrType="System.Boolean" />
@@ -26,6 +27,7 @@
2627
</Command>
2728
<!--__DEFAULT_COMMAND-->
2829
<Command Name="__default_command" IsBranch="false" ClrType="Spectre.Console.Tests.Data.HorseCommand" Settings="Spectre.Console.Tests.Data.HorseSettings">
30+
<Description>The horse command.</Description>
2931
<Parameters>
3032
<Option Short="d" Long="day" Value="MON|TUE" Required="false" Kind="scalar" ClrType="System.DayOfWeek" />
3133
<Option Short="" Long="directory" Value="NULL" Required="false" Kind="scalar" ClrType="System.IO.DirectoryInfo" />

0 commit comments

Comments
 (0)