mirror of
https://github.com/nsnail/spectre.console.git
synced 2025-04-14 16:02:50 +08:00
Improve XmlDoc output (#1503)
* Add command description and examples in XML Output Closes #1115
This commit is contained in:
parent
43f9ae92ad
commit
1a3249cdae
@ -84,6 +84,13 @@ internal sealed class XmlDocCommand : Command<XmlDocCommand.Settings>
|
||||
|
||||
node.SetNullableAttribute("Settings", command.SettingsType?.FullName);
|
||||
|
||||
if (!string.IsNullOrWhiteSpace(command.Description))
|
||||
{
|
||||
var descriptionNode = doc.CreateElement("Description");
|
||||
descriptionNode.InnerText = command.Description;
|
||||
node.AppendChild(descriptionNode);
|
||||
}
|
||||
|
||||
// Parameters
|
||||
if (command.Parameters.Count > 0)
|
||||
{
|
||||
@ -103,6 +110,27 @@ internal sealed class XmlDocCommand : Command<XmlDocCommand.Settings>
|
||||
node.AppendChild(CreateCommandNode(doc, childCommand));
|
||||
}
|
||||
|
||||
// Examples
|
||||
if (command.Examples.Count > 0)
|
||||
{
|
||||
var exampleRootNode = doc.CreateElement("Examples");
|
||||
foreach (var example in command.Examples.SelectMany(static x => x))
|
||||
{
|
||||
var exampleNode = CreateExampleNode(doc, example);
|
||||
exampleRootNode.AppendChild(exampleNode);
|
||||
}
|
||||
|
||||
node.AppendChild(exampleRootNode);
|
||||
}
|
||||
|
||||
return node;
|
||||
}
|
||||
|
||||
private static XmlNode CreateExampleNode(XmlDocument document, string example)
|
||||
{
|
||||
var node = document.CreateElement("Example");
|
||||
node.SetAttribute("commandLine", example);
|
||||
|
||||
return node;
|
||||
}
|
||||
|
||||
|
@ -21,6 +21,7 @@
|
||||
</Parameters>
|
||||
<!--DOG-->
|
||||
<Command Name="dog" IsBranch="false" ClrType="Spectre.Console.Tests.Data.DogCommand" Settings="Spectre.Console.Tests.Data.DogSettings">
|
||||
<Description>The dog command.</Description>
|
||||
<Parameters>
|
||||
<Argument Name="AGE" Position="0" Required="true" Kind="scalar" ClrType="System.Int32" />
|
||||
<Option Short="g" Long="good-boy" Value="NULL" Required="false" Kind="flag" ClrType="System.Boolean" />
|
||||
@ -28,6 +29,7 @@
|
||||
</Command>
|
||||
<!--HORSE-->
|
||||
<Command Name="horse" IsBranch="false" ClrType="Spectre.Console.Tests.Data.HorseCommand" Settings="Spectre.Console.Tests.Data.HorseSettings">
|
||||
<Description>The horse command.</Description>
|
||||
<Parameters>
|
||||
<Option Short="d" Long="day" Value="MON|TUE" Required="false" Kind="scalar" ClrType="System.DayOfWeek" />
|
||||
<Option Short="" Long="directory" Value="NULL" Required="false" Kind="scalar" ClrType="System.IO.DirectoryInfo" />
|
||||
|
@ -0,0 +1,26 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Model>
|
||||
<!--DOG-->
|
||||
<Command Name="dog" IsBranch="false" ClrType="Spectre.Console.Tests.Data.DogCommand" Settings="Spectre.Console.Tests.Data.DogSettings">
|
||||
<Description>The dog command.</Description>
|
||||
<Parameters>
|
||||
<Argument Name="LEGS" Position="0" Required="false" Kind="scalar" ClrType="System.Int32">
|
||||
<Description>The number of legs.</Description>
|
||||
<Validators>
|
||||
<Validator ClrType="Spectre.Console.Tests.Data.EvenNumberValidatorAttribute" Message="Animals must have an even number of legs." />
|
||||
<Validator ClrType="Spectre.Console.Tests.Data.PositiveNumberValidatorAttribute" Message="Number of legs must be greater than 0." />
|
||||
</Validators>
|
||||
</Argument>
|
||||
<Argument Name="AGE" Position="1" Required="true" Kind="scalar" ClrType="System.Int32" />
|
||||
<Option Short="a" Long="alive,not-dead" Value="NULL" Required="false" Kind="flag" ClrType="System.Boolean">
|
||||
<Description>Indicates whether or not the animal is alive.</Description>
|
||||
</Option>
|
||||
<Option Short="g" Long="good-boy" Value="NULL" Required="false" Kind="flag" ClrType="System.Boolean" />
|
||||
<Option Short="n,p" Long="name,pet-name" Value="VALUE" Required="false" Kind="scalar" ClrType="System.String" />
|
||||
</Parameters>
|
||||
<Examples>
|
||||
<Example commandLine="dog -g" />
|
||||
<Example commandLine="dog --good-boy" />
|
||||
</Examples>
|
||||
</Command>
|
||||
</Model>
|
@ -2,6 +2,7 @@
|
||||
<Model>
|
||||
<!--DOG-->
|
||||
<Command Name="dog" IsBranch="false" ClrType="Spectre.Console.Tests.Data.DogCommand" Settings="Spectre.Console.Tests.Data.DogSettings">
|
||||
<Description>The dog command.</Description>
|
||||
<Parameters>
|
||||
<Argument Name="LEGS" Position="0" Required="false" Kind="scalar" ClrType="System.Int32">
|
||||
<Description>The number of legs.</Description>
|
||||
|
@ -16,6 +16,7 @@
|
||||
</Parameters>
|
||||
<!--DOG-->
|
||||
<Command Name="dog" IsBranch="false" ClrType="Spectre.Console.Tests.Data.DogCommand" Settings="Spectre.Console.Tests.Data.DogSettings">
|
||||
<Description>The dog command.</Description>
|
||||
<Parameters>
|
||||
<Argument Name="AGE" Position="0" Required="true" Kind="scalar" ClrType="System.Int32" />
|
||||
<Option Short="g" Long="good-boy" Value="NULL" Required="false" Kind="flag" ClrType="System.Boolean" />
|
||||
@ -24,6 +25,7 @@
|
||||
</Command>
|
||||
<!--HORSE-->
|
||||
<Command Name="horse" IsBranch="false" ClrType="Spectre.Console.Tests.Data.HorseCommand" Settings="Spectre.Console.Tests.Data.HorseSettings">
|
||||
<Description>The horse command.</Description>
|
||||
<Parameters>
|
||||
<Option Short="d" Long="day" Value="MON|TUE" Required="false" Kind="scalar" ClrType="System.DayOfWeek" />
|
||||
<Option Short="" Long="directory" Value="NULL" Required="false" Kind="scalar" ClrType="System.IO.DirectoryInfo" />
|
||||
|
@ -16,6 +16,7 @@
|
||||
</Parameters>
|
||||
<!--DOG-->
|
||||
<Command Name="dog" IsBranch="false" ClrType="Spectre.Console.Tests.Data.DogCommand" Settings="Spectre.Console.Tests.Data.DogSettings">
|
||||
<Description>The dog command.</Description>
|
||||
<Parameters>
|
||||
<Argument Name="AGE" Position="0" Required="true" Kind="scalar" ClrType="System.Int32" />
|
||||
<Option Short="g" Long="good-boy" Value="NULL" Required="false" Kind="flag" ClrType="System.Boolean" />
|
||||
|
@ -2,6 +2,7 @@
|
||||
<Model>
|
||||
<!--DEFAULT COMMAND-->
|
||||
<Command Name="__default_command" IsBranch="false" IsDefault="true" ClrType="Spectre.Console.Tests.Data.DogCommand" Settings="Spectre.Console.Tests.Data.DogSettings">
|
||||
<Description>The dog command.</Description>
|
||||
<Parameters>
|
||||
<Argument Name="LEGS" Position="0" Required="false" Kind="scalar" ClrType="System.Int32">
|
||||
<Description>The number of legs.</Description>
|
||||
@ -20,6 +21,7 @@
|
||||
</Command>
|
||||
<!--HORSE-->
|
||||
<Command Name="horse" IsBranch="false" ClrType="Spectre.Console.Tests.Data.HorseCommand" Settings="Spectre.Console.Tests.Data.HorseSettings">
|
||||
<Description>The horse command.</Description>
|
||||
<Parameters>
|
||||
<Argument Name="LEGS" Position="0" Required="false" Kind="scalar" ClrType="System.Int32">
|
||||
<Description>The number of legs.</Description>
|
||||
|
@ -21,6 +21,7 @@
|
||||
</Parameters>
|
||||
<!--__DEFAULT_COMMAND-->
|
||||
<Command Name="__default_command" IsBranch="false" ClrType="Spectre.Console.Tests.Data.HorseCommand" Settings="Spectre.Console.Tests.Data.HorseSettings">
|
||||
<Description>The horse command.</Description>
|
||||
<Parameters>
|
||||
<Option Short="d" Long="day" Value="MON|TUE" Required="false" Kind="scalar" ClrType="System.DayOfWeek" />
|
||||
<Option Short="" Long="directory" Value="NULL" Required="false" Kind="scalar" ClrType="System.IO.DirectoryInfo" />
|
||||
|
@ -16,6 +16,7 @@
|
||||
</Parameters>
|
||||
<!--DOG-->
|
||||
<Command Name="dog" IsBranch="false" ClrType="Spectre.Console.Tests.Data.DogCommand" Settings="Spectre.Console.Tests.Data.DogSettings">
|
||||
<Description>The dog command.</Description>
|
||||
<Parameters>
|
||||
<Argument Name="AGE" Position="0" Required="true" Kind="scalar" ClrType="System.Int32" />
|
||||
<Option Short="g" Long="good-boy" Value="NULL" Required="false" Kind="flag" ClrType="System.Boolean" />
|
||||
@ -24,6 +25,7 @@
|
||||
</Command>
|
||||
<!--__DEFAULT_COMMAND-->
|
||||
<Command Name="__default_command" IsBranch="false" ClrType="Spectre.Console.Tests.Data.HorseCommand" Settings="Spectre.Console.Tests.Data.HorseSettings">
|
||||
<Description>The horse command.</Description>
|
||||
<Parameters>
|
||||
<Option Short="d" Long="day" Value="MON|TUE" Required="false" Kind="scalar" ClrType="System.DayOfWeek" />
|
||||
<Option Short="" Long="directory" Value="NULL" Required="false" Kind="scalar" ClrType="System.IO.DirectoryInfo" />
|
||||
|
@ -18,6 +18,7 @@
|
||||
</Parameters>
|
||||
<!--DOG-->
|
||||
<Command Name="dog" IsBranch="false" ClrType="Spectre.Console.Tests.Data.DogCommand" Settings="Spectre.Console.Tests.Data.DogSettings">
|
||||
<Description>The dog command.</Description>
|
||||
<Parameters>
|
||||
<Argument Name="AGE" Position="0" Required="true" Kind="scalar" ClrType="System.Int32" />
|
||||
<Option Short="g" Long="good-boy" Value="NULL" Required="false" Kind="flag" ClrType="System.Boolean" />
|
||||
@ -26,6 +27,7 @@
|
||||
</Command>
|
||||
<!--__DEFAULT_COMMAND-->
|
||||
<Command Name="__default_command" IsBranch="false" ClrType="Spectre.Console.Tests.Data.HorseCommand" Settings="Spectre.Console.Tests.Data.HorseSettings">
|
||||
<Description>The horse command.</Description>
|
||||
<Parameters>
|
||||
<Option Short="d" Long="day" Value="MON|TUE" Required="false" Kind="scalar" ClrType="System.DayOfWeek" />
|
||||
<Option Short="" Long="directory" Value="NULL" Required="false" Kind="scalar" ClrType="System.IO.DirectoryInfo" />
|
||||
|
@ -110,6 +110,26 @@ public sealed partial class CommandAppTests
|
||||
return Verifier.Verify(result.Output);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
[Expectation("Test_10")]
|
||||
public Task Should_Dump_Correct_Model_For_Case_6()
|
||||
{
|
||||
// Given
|
||||
var fixture = new CommandAppTester();
|
||||
fixture.Configure(config =>
|
||||
{
|
||||
config.AddCommand<DogCommand>("dog")
|
||||
.WithExample("dog -g")
|
||||
.WithExample("dog --good-boy");
|
||||
});
|
||||
|
||||
// When
|
||||
var result = fixture.Run(Constants.XmlDocCommand);
|
||||
|
||||
// Then
|
||||
return Verifier.Verify(result.Output);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
[Expectation("Test_6")]
|
||||
public Task Should_Dump_Correct_Model_For_Model_With_Default_Command()
|
||||
|
Loading…
x
Reference in New Issue
Block a user