mirror of
https://github.com/spectreconsole/spectre.console.git
synced 2025-10-25 15:19:23 +00:00
Compare commits
9 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
3c2156268c | ||
|
|
6fb81103f0 | ||
|
|
880e83b27c | ||
|
|
0b270e1ccd | ||
|
|
2d9e8069fd | ||
|
|
b551bbd244 | ||
|
|
3a70fbec75 | ||
|
|
c67b3df3ba | ||
|
|
8e474f514c |
4
.github/workflows/ci.yaml
vendored
4
.github/workflows/ci.yaml
vendored
@@ -18,12 +18,12 @@ jobs:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- name: Checkout
|
||||
uses: actions/checkout@v4
|
||||
uses: actions/checkout@v5
|
||||
with:
|
||||
fetch-depth: 0
|
||||
|
||||
- name: Setup .NET SDK
|
||||
uses: actions/setup-dotnet@v4
|
||||
uses: actions/setup-dotnet@v5
|
||||
with:
|
||||
dotnet-version: |
|
||||
8.0.x
|
||||
|
||||
12
.github/workflows/publish.yaml
vendored
12
.github/workflows/publish.yaml
vendored
@@ -24,12 +24,12 @@ jobs:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- name: Checkout
|
||||
uses: actions/checkout@v4
|
||||
uses: actions/checkout@v5
|
||||
with:
|
||||
fetch-depth: 0
|
||||
|
||||
- name: Setup .NET SDK
|
||||
uses: actions/setup-dotnet@v4
|
||||
uses: actions/setup-dotnet@v5
|
||||
with:
|
||||
dotnet-version: |
|
||||
8.0.x
|
||||
@@ -53,17 +53,17 @@ jobs:
|
||||
runs-on: windows-latest
|
||||
steps:
|
||||
- name: Checkout
|
||||
uses: actions/checkout@v4
|
||||
uses: actions/checkout@v5
|
||||
with:
|
||||
fetch-depth: 0
|
||||
|
||||
- name: Setup .NET SDK
|
||||
uses: actions/setup-dotnet@v4
|
||||
uses: actions/setup-dotnet@v5
|
||||
|
||||
- name: Setup Node.js
|
||||
uses: actions/setup-node@v4
|
||||
uses: actions/setup-node@v5
|
||||
with:
|
||||
node-version: '16'
|
||||
node-version: '22'
|
||||
|
||||
- name: Cache dependencies
|
||||
uses: actions/cache@v4
|
||||
|
||||
@@ -38,8 +38,8 @@
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.Playwright" Version="1.52.0" />
|
||||
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
|
||||
<PackageReference Include="Microsoft.Playwright" Version="1.55.0" />
|
||||
<PackageReference Include="Newtonsoft.Json" Version="13.0.4" />
|
||||
<PackageReference Include="Statiq.CodeAnalysis" Version="1.0.0-beta.72" />
|
||||
<PackageReference Include="Statiq.Common" Version="1.0.0-beta.72" />
|
||||
<PackageReference Include="Statiq.Web" Version="1.0.0-beta.60" />
|
||||
|
||||
@@ -1,25 +0,0 @@
|
||||
|
||||
Microsoft Visual Studio Solution File, Format Version 12.00
|
||||
# Visual Studio Version 16
|
||||
VisualStudioVersion = 16.0.30011.22
|
||||
MinimumVisualStudioVersion = 10.0.40219.1
|
||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Docs", "Docs.csproj", "{C337F609-A890-4E52-BDA3-91658039B0E3}"
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
Debug|Any CPU = Debug|Any CPU
|
||||
Release|Any CPU = Release|Any CPU
|
||||
EndGlobalSection
|
||||
GlobalSection(ProjectConfigurationPlatforms) = postSolution
|
||||
{C337F609-A890-4E52-BDA3-91658039B0E3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{C337F609-A890-4E52-BDA3-91658039B0E3}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{C337F609-A890-4E52-BDA3-91658039B0E3}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{C337F609-A890-4E52-BDA3-91658039B0E3}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
EndGlobalSection
|
||||
GlobalSection(SolutionProperties) = preSolution
|
||||
HideSolutionNode = FALSE
|
||||
EndGlobalSection
|
||||
GlobalSection(ExtensibilityGlobals) = postSolution
|
||||
SolutionGuid = {2FB3922B-494A-45EB-A479-FC507B8E107C}
|
||||
EndGlobalSection
|
||||
EndGlobal
|
||||
3
docs/Docs.slnx
Normal file
3
docs/Docs.slnx
Normal file
@@ -0,0 +1,3 @@
|
||||
<Solution>
|
||||
<Project Path="Docs.csproj" />
|
||||
</Solution>
|
||||
@@ -1,6 +0,0 @@
|
||||
{
|
||||
"sdk": {
|
||||
"version": "9.0.202",
|
||||
"rollForward": "latestFeature"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
Title: Spectre.Console 0.51.1 released!
|
||||
Description: Not a substitute for human interaction.
|
||||
Published: 2025-09-07
|
||||
Category: Release Notes
|
||||
Excluded: false
|
||||
---
|
||||
|
||||
Version `0.51.1` of Spectre.Console has been released!
|
||||
|
||||
_Note: Due to an issue discovered after the release of version 0.51.0, that version has now been unlisted. Let’s all pretend it never existed 😅_
|
||||
|
||||
## What's Changed
|
||||
|
||||
* Fix IndexOutOfRangeException in ExceptionFormatter by [@martincostello](https://github.com/martincostello) in [#1800](https://github.com/spectreconsole/spectre.console/pull/1800)
|
||||
* TestConsole can now be configured and accessed in CommandAppTester by [@magiino](https://github.com/magiino) in [#1803](https://github.com/spectreconsole/spectre.console/pull/1803)
|
||||
* Add ShowRowSeparators in Table Widget docs by [@bartoginski](https://github.com/bartoginski) in [#1807](https://github.com/spectreconsole/spectre.console/pull/1807)
|
||||
* Add support for required options by [@patriksvensson](https://github.com/patriksvensson) in [#1825](https://github.com/spectreconsole/spectre.console/pull/1825)
|
||||
* Added documentation for align widget by [@Elementttto](https://github.com/Elementttto) in [#1746](https://github.com/spectreconsole/spectre.console/pull/1746)
|
||||
* Fixed link not displayed in markup in Style.cs and added unit test cases by [@Elementttto](https://github.com/Elementttto) in [#1750](https://github.com/spectreconsole/spectre.console/pull/1750)
|
||||
* Update System.Memory dependency by [@WeihanLi](https://github.com/WeihanLi) in [#1832](https://github.com/spectreconsole/spectre.console/pull/1832)
|
||||
* Reduce memory usage for rune width cache. by [@Pannoniae](https://github.com/Pannoniae) in [#1756](https://github.com/spectreconsole/spectre.console/pull/1756)
|
||||
* Fix resizing of Live views with reduced size. by [@belucha](https://github.com/belucha) in [#1840](https://github.com/spectreconsole/spectre.console/pull/1840)
|
||||
* Corrects comment for optional text prompt by [@aljanabim](https://github.com/aljanabim) in [#1857](https://github.com/spectreconsole/spectre.console/pull/1857)
|
||||
* Update spinners by [@FroggieFrog](https://github.com/FroggieFrog) in [#1873](https://github.com/spectreconsole/spectre.console/pull/1873)
|
||||
* Support J and K for navigating list prompts by [@tobias-tengler](https://github.com/tobias-tengler) in [#1877](https://github.com/spectreconsole/spectre.console/pull/1877)
|
||||
* Fix space triggering selection when items in the selection list have a space. by [@mitchdenny](https://github.com/mitchdenny) in [#1881](https://github.com/spectreconsole/spectre.console/pull/1881)
|
||||
* Fix bug setting Header by [@mattfennerom](https://github.com/mattfennerom) in [#1890](https://github.com/spectreconsole/spectre.console/pull/1890)
|
||||
|
||||
## New Contributors
|
||||
|
||||
* [@magiino](https://github.com/magiino) made their first contribution in [#1803](https://github.com/spectreconsole/spectre.console/pull/1803)
|
||||
* [@bartoginski](https://github.com/bartoginski) made their first contribution in [#1807](https://github.com/spectreconsole/spectre.console/pull/1807)
|
||||
* [@Elementttto](https://github.com/Elementttto) made their first contribution in [#1746](https://github.com/spectreconsole/spectre.console/pull/1746)
|
||||
* [@WeihanLi](https://github.com/WeihanLi) made their first contribution in [#1832](https://github.com/spectreconsole/spectre.console/pull/1832)
|
||||
* [@Pannoniae](https://github.com/Pannoniae) made their first contribution in [#1756](https://github.com/spectreconsole/spectre.console/pull/1756)
|
||||
* [@belucha](https://github.com/belucha) made their first contribution in [#1840](https://github.com/spectreconsole/spectre.console/pull/1840)
|
||||
* [@aljanabim](https://github.com/aljanabim) made their first contribution in [#1857](https://github.com/spectreconsole/spectre.console/pull/1857)
|
||||
* [@FroggieFrog](https://github.com/FroggieFrog) made their first contribution in [#1873](https://github.com/spectreconsole/spectre.console/pull/1873)
|
||||
* [@tobias-tengler](https://github.com/tobias-tengler) made their first contribution in [#1877](https://github.com/spectreconsole/spectre.console/pull/1877)
|
||||
* [@mitchdenny](https://github.com/mitchdenny) made their first contribution in [#1881](https://github.com/spectreconsole/spectre.console/pull/1881)
|
||||
* [@mattfennerom](https://github.com/mattfennerom) made their first contribution in [#1890](https://github.com/spectreconsole/spectre.console/pull/1890)
|
||||
|
||||
**Full Changelog**: [0.50.0...0.51.0](https://github.com/spectreconsole/spectre.console/compare/0.50.0...0.51.1)
|
||||
22
docs/input/cli/opencli.md
Normal file
22
docs/input/cli/opencli.md
Normal file
@@ -0,0 +1,22 @@
|
||||
Title: OpenCLI Integration
|
||||
Order: 15
|
||||
Description: OpenCLI integration
|
||||
Highlights:
|
||||
- Generate OpenCLI descriptions
|
||||
---
|
||||
|
||||
From version `0.52.0` and above, you will be able to generate [OpenCLI](https://opencli.org)
|
||||
descriptions from your `Spectre.Console.Cli` applications.
|
||||
|
||||
Simply add the `--help-dump-opencli` option to your application, and an
|
||||
OpenCLI description will be written to stdout.
|
||||
|
||||
```shell
|
||||
$ ./myapp --help-dump-opencli
|
||||
```
|
||||
|
||||
If you want to save it to disk, pipe it to a file.
|
||||
|
||||
```shell
|
||||
$ ./myapp --help-dump-opencli > myapp.openapi.json
|
||||
```
|
||||
@@ -3,7 +3,7 @@
|
||||
"isRoot": true,
|
||||
"tools": {
|
||||
"cake.tool": {
|
||||
"version": "5.0.0",
|
||||
"version": "5.1.0",
|
||||
"commands": [
|
||||
"dotnet-cake"
|
||||
]
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
{
|
||||
"$schema": "http://json.schemastore.org/global",
|
||||
"sdk": {
|
||||
"version": "9.0.202",
|
||||
"version": "9.0.305",
|
||||
"rollForward": "latestFeature"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -45,9 +45,9 @@
|
||||
<ItemGroup>
|
||||
<PackageReference Include="AngleSharp" Version="1.3.0" />
|
||||
<PackageReference Include="Humanizer.Core" Version="2.14.1" />
|
||||
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
|
||||
<PackageReference Include="Scriban" Version="6.2.1" />
|
||||
<PackageReference Include="Spectre.IO" Version="0.18.0" />
|
||||
<PackageReference Include="Newtonsoft.Json" Version="13.0.4" />
|
||||
<PackageReference Include="Scriban" Version="6.4.0" />
|
||||
<PackageReference Include="Spectre.IO" Version="0.20.0" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
||||
@@ -4,25 +4,23 @@
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<PackageVersion Include="IsExternalInit" Version="1.0.3" />
|
||||
<PackageVersion Include="Microsoft.Extensions.DependencyInjection" Version="9.0.3" />
|
||||
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="17.14.1" />
|
||||
<PackageVersion Include="Microsoft.Extensions.DependencyInjection" Version="9.0.9" />
|
||||
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="18.0.0" />
|
||||
<PackageVersion Include="Microsoft.SourceLink.GitHub" PrivateAssets="All" Version="8.0.0" />
|
||||
<PackageVersion Include="MinVer" PrivateAssets="All" Version="6.0.0" />
|
||||
<PackageVersion Include="OpenCli.Sources" Version="0.5.0" />
|
||||
<PackageVersion Include="PolySharp" Version="1.15.0" />
|
||||
<PackageVersion Include="Roslynator.Analyzers" PrivateAssets="All" Version="4.14.0" />
|
||||
<PackageVersion Include="Roslynator.Analyzers" PrivateAssets="All" Version="4.14.1" />
|
||||
<PackageVersion Include="Shouldly" Version="4.3.0" />
|
||||
<PackageVersion Include="SixLabors.ImageSharp" Version="3.1.11" />
|
||||
<PackageVersion Include="Spectre.Console" Version="0.50.0" />
|
||||
<PackageVersion Include="Spectre.Console.Cli" Version="0.50.0" />
|
||||
<PackageVersion Include="Spectre.Console.Testing" Version="0.50.1-preview.0.20" />
|
||||
<PackageVersion Include="Spectre.Verify.Extensions" Version="28.16.0" />
|
||||
<PackageVersion Include="StyleCop.Analyzers" PrivateAssets="All" Version="1.2.0-beta.556" />
|
||||
<PackageVersion Include="System.Memory" Version="4.6.3" />
|
||||
<PackageVersion Include="TunnelVisionLabs.ReferenceAssemblyAnnotator" Version="1.0.0-alpha.160" />
|
||||
<PackageVersion Include="Verify.Xunit" Version="30.5.0" />
|
||||
<PackageVersion Include="Wcwidth.Sources" Version="2.0.0" />
|
||||
<PackageVersion Include="Verify.Xunit" Version="31.0.0" />
|
||||
<PackageVersion Include="Wcwidth.Sources" Version="3.0.0" />
|
||||
<PackageVersion Include="xunit" Version="2.9.3" />
|
||||
<PackageVersion Include="xunit.runner.visualstudio" Version="3.1.3">
|
||||
<PackageVersion Include="xunit.runner.visualstudio" Version="3.1.5">
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
</PackageVersion>
|
||||
|
||||
@@ -79,6 +79,7 @@ public sealed class CommandApp : ICommandApp
|
||||
cli.AddCommand<VersionCommand>(CliConstants.Commands.Version);
|
||||
cli.AddCommand<XmlDocCommand>(CliConstants.Commands.XmlDoc);
|
||||
cli.AddCommand<ExplainCommand>(CliConstants.Commands.Explain);
|
||||
cli.AddCommand<OpenCliGeneratorCommand>(CliConstants.Commands.OpenCli);
|
||||
});
|
||||
|
||||
_executed = true;
|
||||
|
||||
@@ -68,6 +68,13 @@ internal sealed class CommandExecutor
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// OpenCLI?
|
||||
if (firstArgument.Equals(CliConstants.DumpHelpOpenCliOption, StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
// Replace all arguments with the opencligen command
|
||||
arguments = ["cli", "opencli"];
|
||||
}
|
||||
}
|
||||
|
||||
// Parse and map the model against the arguments.
|
||||
|
||||
@@ -2,7 +2,7 @@ namespace Spectre.Console.Cli;
|
||||
|
||||
[Description("Displays diagnostics about CLI configurations")]
|
||||
[SuppressMessage("Performance", "CA1812: Avoid uninstantiated internal classes")]
|
||||
internal sealed class ExplainCommand : Command<ExplainCommand.Settings>
|
||||
internal sealed class ExplainCommand : Command<ExplainCommand.Settings>, IBuiltInCommand
|
||||
{
|
||||
private readonly CommandModel _commandModel;
|
||||
private readonly IAnsiConsole _writer;
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
namespace Spectre.Console.Cli;
|
||||
|
||||
/// <summary>
|
||||
/// Represents a built-in command.
|
||||
/// Used as a marker interface.
|
||||
/// </summary>
|
||||
internal interface IBuiltInCommand : ICommand
|
||||
{
|
||||
}
|
||||
@@ -0,0 +1,224 @@
|
||||
using OpenCli;
|
||||
|
||||
namespace Spectre.Console.Cli;
|
||||
|
||||
internal sealed class OpenCliGeneratorCommand : Command, IBuiltInCommand
|
||||
{
|
||||
private readonly IConfiguration _configuration;
|
||||
private readonly CommandModel _model;
|
||||
|
||||
public OpenCliGeneratorCommand(IConfiguration configuration, CommandModel model)
|
||||
{
|
||||
_configuration = configuration;
|
||||
_model = model ?? throw new ArgumentNullException(nameof(model));
|
||||
}
|
||||
|
||||
public override int Execute(CommandContext context)
|
||||
{
|
||||
var document = new OpenCliDocument
|
||||
{
|
||||
OpenCli = "0.1-draft",
|
||||
Info = new OpenCliInfo
|
||||
{
|
||||
Title = ((ICommandModel)_model).ApplicationName, Version = _model.ApplicationVersion ?? "1.0",
|
||||
},
|
||||
Commands = CreateCommands(_model.Commands),
|
||||
Arguments = CreateArguments(_model.DefaultCommand?.GetArguments()),
|
||||
Options = CreateOptions(_model.DefaultCommand?.GetOptions()),
|
||||
};
|
||||
|
||||
var writer = _configuration.Settings.Console.GetConsole();
|
||||
writer.WriteLine(document.Write());
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
private List<OpenCliCommand> CreateCommands(IList<CommandInfo> commands)
|
||||
{
|
||||
var result = new List<OpenCliCommand>();
|
||||
|
||||
foreach (var command in commands.OrderBy(o => o.Name, StringComparer.OrdinalIgnoreCase))
|
||||
{
|
||||
if (typeof(IBuiltInCommand).IsAssignableFrom(command.CommandType))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
var openCliCommand = new OpenCliCommand
|
||||
{
|
||||
Name = command.Name,
|
||||
Aliases =
|
||||
[
|
||||
..command.Aliases.OrderBy(str => str)
|
||||
],
|
||||
Commands = CreateCommands(command.Children),
|
||||
Arguments = CreateArguments(command.GetArguments()),
|
||||
Options = CreateOptions(command.GetOptions()),
|
||||
Description = command.Description,
|
||||
Hidden = command.IsHidden,
|
||||
Examples = [..command.Examples.Select(example => string.Join(" ", example))],
|
||||
};
|
||||
|
||||
// Skip branches without commands
|
||||
if (command.IsBranch && openCliCommand.Commands.Count == 0)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
result.Add(openCliCommand);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
private List<OpenCliArgument> CreateArguments(IEnumerable<CommandArgument>? arguments)
|
||||
{
|
||||
var result = new List<OpenCliArgument>();
|
||||
|
||||
if (arguments == null)
|
||||
{
|
||||
return result;
|
||||
}
|
||||
|
||||
foreach (var argument in arguments.OrderBy(x => x.Position))
|
||||
{
|
||||
var metadata = default(List<OpenCliMetadata>);
|
||||
if (argument.ParameterType != typeof(void) &&
|
||||
argument.ParameterType != typeof(bool))
|
||||
{
|
||||
metadata =
|
||||
[
|
||||
new OpenCliMetadata { Name = "ClrType", Value = argument.ParameterType.ToCliTypeString(), },
|
||||
];
|
||||
}
|
||||
|
||||
result.Add(new OpenCliArgument
|
||||
{
|
||||
Name = argument.Value,
|
||||
Required = argument.IsRequired,
|
||||
Arity = new OpenCliArity
|
||||
{
|
||||
// TODO: Look into this
|
||||
Minimum = 1,
|
||||
Maximum = 1,
|
||||
},
|
||||
Description = argument.Description,
|
||||
Hidden = argument.IsHidden,
|
||||
Metadata = metadata,
|
||||
AcceptedValues = null,
|
||||
Group = null,
|
||||
});
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
private List<OpenCliOption> CreateOptions(IEnumerable<CommandOption>? options)
|
||||
{
|
||||
var result = new List<OpenCliOption>();
|
||||
|
||||
if (options == null)
|
||||
{
|
||||
return result;
|
||||
}
|
||||
|
||||
foreach (var option in options.OrderBy(o => o.GetOptionName(), StringComparer.OrdinalIgnoreCase))
|
||||
{
|
||||
var arguments = new List<OpenCliArgument>();
|
||||
if (option.ParameterType != typeof(void) &&
|
||||
option.ParameterType != typeof(bool))
|
||||
{
|
||||
arguments.Add(new OpenCliArgument
|
||||
{
|
||||
Name = option.ValueName ?? "VALUE",
|
||||
Required = !option.ValueIsOptional,
|
||||
Arity = new OpenCliArity
|
||||
{
|
||||
// TODO: Look into this
|
||||
Minimum = option.ValueIsOptional
|
||||
? 0
|
||||
: 1,
|
||||
Maximum = 1,
|
||||
},
|
||||
AcceptedValues = null,
|
||||
Group = null,
|
||||
Hidden = null,
|
||||
Metadata =
|
||||
[
|
||||
new OpenCliMetadata
|
||||
{
|
||||
Name = "ClrType",
|
||||
Value = option.ParameterType.ToCliTypeString(),
|
||||
},
|
||||
],
|
||||
});
|
||||
}
|
||||
|
||||
var optionMetadata = default(List<OpenCliMetadata>);
|
||||
if (arguments.Count == 0 && option.ParameterType != typeof(void) &&
|
||||
option.ParameterType != typeof(bool))
|
||||
{
|
||||
optionMetadata =
|
||||
[
|
||||
new OpenCliMetadata { Name = "ClrType", Value = option.ParameterType.ToCliTypeString(), },
|
||||
];
|
||||
}
|
||||
|
||||
var (optionName, optionAliases) = GetOptionNames(option);
|
||||
result.Add(new OpenCliOption
|
||||
{
|
||||
Name = optionName,
|
||||
Required = option.IsRequired,
|
||||
Aliases = [..optionAliases.OrderBy(str => str)],
|
||||
Arguments = arguments,
|
||||
Description = option.Description,
|
||||
Group = null,
|
||||
Hidden = option.IsHidden,
|
||||
Recursive = option.IsShadowed, // TODO: Is this correct?
|
||||
Metadata = optionMetadata,
|
||||
});
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
private static (string Name, HashSet<string> Aliases) GetOptionNames(CommandOption option)
|
||||
{
|
||||
var name = GetOptionName(option);
|
||||
var aliases = new HashSet<string>();
|
||||
|
||||
if (option.LongNames.Count > 0)
|
||||
{
|
||||
foreach (var alias in option.LongNames.Skip(1))
|
||||
{
|
||||
aliases.Add("--" + alias);
|
||||
}
|
||||
|
||||
foreach (var alias in option.ShortNames)
|
||||
{
|
||||
aliases.Add("-" + alias);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
foreach (var alias in option.LongNames)
|
||||
{
|
||||
aliases.Add("--" + alias);
|
||||
}
|
||||
|
||||
foreach (var alias in option.ShortNames.Skip(1))
|
||||
{
|
||||
aliases.Add("-" + alias);
|
||||
}
|
||||
}
|
||||
|
||||
return (name, aliases);
|
||||
}
|
||||
|
||||
private static string GetOptionName(CommandOption option)
|
||||
{
|
||||
return option.LongNames.Count > 0
|
||||
? "--" + option.LongNames[0]
|
||||
: "-" + option.ShortNames[0];
|
||||
}
|
||||
}
|
||||
@@ -2,7 +2,7 @@ namespace Spectre.Console.Cli;
|
||||
|
||||
[Description("Displays the CLI library version")]
|
||||
[SuppressMessage("Performance", "CA1812: Avoid uninstantiated internal classes")]
|
||||
internal sealed class VersionCommand : Command<VersionCommand.Settings>
|
||||
internal sealed class VersionCommand : Command, IBuiltInCommand
|
||||
{
|
||||
private readonly IAnsiConsole _writer;
|
||||
|
||||
@@ -11,11 +11,7 @@ internal sealed class VersionCommand : Command<VersionCommand.Settings>
|
||||
_writer = configuration.Settings.Console.GetConsole();
|
||||
}
|
||||
|
||||
public sealed class Settings : CommandSettings
|
||||
{
|
||||
}
|
||||
|
||||
public override int Execute(CommandContext context, Settings settings)
|
||||
public override int Execute(CommandContext context)
|
||||
{
|
||||
_writer.MarkupLine(
|
||||
"[yellow]Spectre.Cli[/] version [aqua]{0}[/]",
|
||||
|
||||
@@ -2,7 +2,7 @@ namespace Spectre.Console.Cli;
|
||||
|
||||
[Description("Generates an XML representation of the CLI configuration.")]
|
||||
[SuppressMessage("Performance", "CA1812: Avoid uninstantiated internal classes")]
|
||||
internal sealed class XmlDocCommand : Command<XmlDocCommand.Settings>
|
||||
internal sealed class XmlDocCommand : Command, IBuiltInCommand
|
||||
{
|
||||
private readonly CommandModel _model;
|
||||
private readonly IAnsiConsole _writer;
|
||||
@@ -13,11 +13,7 @@ internal sealed class XmlDocCommand : Command<XmlDocCommand.Settings>
|
||||
_writer = configuration.Settings.Console.GetConsole();
|
||||
}
|
||||
|
||||
public sealed class Settings : CommandSettings
|
||||
{
|
||||
}
|
||||
|
||||
public override int Execute(CommandContext context, Settings settings)
|
||||
public override int Execute(CommandContext context)
|
||||
{
|
||||
_writer.Write(Serialize(_model), Style.Plain);
|
||||
return 0;
|
||||
|
||||
@@ -5,6 +5,7 @@ internal static class CliConstants
|
||||
public const string DefaultCommandName = "__default_command";
|
||||
public const string True = "true";
|
||||
public const string False = "false";
|
||||
public const string DumpHelpOpenCliOption = "--help-dump-opencli";
|
||||
|
||||
public static string[] AcceptedBooleanValues { get; } = new string[]
|
||||
{
|
||||
@@ -18,5 +19,6 @@ internal static class CliConstants
|
||||
public const string Version = "version";
|
||||
public const string XmlDoc = "xmldoc";
|
||||
public const string Explain = "explain";
|
||||
public const string OpenCli = "opencli";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
#if NETSTANDARD2_0
|
||||
namespace System.IO;
|
||||
|
||||
// Polyfills needed for OpenCli.
|
||||
// This can be removed once me migrate over to the Polyfill library.
|
||||
internal static class OpenCliExtensions
|
||||
{
|
||||
public static Task<string> ReadToEndAsync(this StreamReader reader, CancellationToken cancellationToken)
|
||||
{
|
||||
return reader.ReadToEndAsync();
|
||||
}
|
||||
}
|
||||
#endif
|
||||
@@ -16,4 +16,19 @@ internal static class TypeExtensions
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
// Taken from https://github.com/dotnet/sdk/blob/main/src/Cli/Microsoft.DotNet.Cli.Utils/Extensions/TypeExtensions.cs#L15
|
||||
// Licensed under MIT
|
||||
public static string ToCliTypeString(this Type type)
|
||||
{
|
||||
var typeName = type.FullName ?? string.Empty;
|
||||
if (!type.IsGenericType)
|
||||
{
|
||||
return typeName;
|
||||
}
|
||||
|
||||
var genericTypeName = typeName.Substring(0, typeName.IndexOf('`'));
|
||||
var genericTypes = string.Join(", ", type.GenericTypeArguments.Select(generic => generic.ToCliTypeString()));
|
||||
return $"{genericTypeName}<{genericTypes}>";
|
||||
}
|
||||
}
|
||||
@@ -2,6 +2,16 @@ namespace Spectre.Console.Cli;
|
||||
|
||||
internal static class CommandInfoExtensions
|
||||
{
|
||||
public static IEnumerable<CommandArgument>? GetArguments(this CommandInfo? command)
|
||||
{
|
||||
return command?.Parameters.OfType<CommandArgument>();
|
||||
}
|
||||
|
||||
public static IEnumerable<CommandOption>? GetOptions(this CommandInfo? command)
|
||||
{
|
||||
return command?.Parameters.OfType<CommandOption>();
|
||||
}
|
||||
|
||||
public static bool HaveParentWithOption(this CommandInfo command, CommandOption option)
|
||||
{
|
||||
var parent = command?.Parent;
|
||||
|
||||
@@ -38,7 +38,7 @@ internal abstract class CommandParameter : ICommandParameterInfo, ICommandParame
|
||||
DefaultValue = defaultValue;
|
||||
PairDeconstructor = deconstructor;
|
||||
ValueProvider = valueProvider;
|
||||
Validators = new List<ParameterValidationAttribute>(validators ?? Array.Empty<ParameterValidationAttribute>());
|
||||
Validators = new List<ParameterValidationAttribute>(validators ?? []);
|
||||
IsRequired = required;
|
||||
IsHidden = isHidden;
|
||||
}
|
||||
|
||||
@@ -9,6 +9,7 @@ global using System.IO;
|
||||
global using System.Linq;
|
||||
global using System.Reflection;
|
||||
global using System.Text;
|
||||
global using System.Threading;
|
||||
global using System.Threading.Tasks;
|
||||
global using System.Xml;
|
||||
global using Spectre.Console.Cli.Help;
|
||||
|
||||
@@ -3,13 +3,13 @@
|
||||
<PropertyGroup>
|
||||
<TargetFrameworks>net9.0;net8.0;netstandard2.0</TargetFrameworks>
|
||||
<IsPackable>true</IsPackable>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup>
|
||||
<IsAotCompatible Condition="'$(TargetFramework)' != 'netstandard2.0'">false</IsAotCompatible>
|
||||
<IsTrimmable>false</IsTrimmable>
|
||||
<DefineConstants>$(DefineConstants);OPENCLI_VISIBILITY_INTERNAL</DefineConstants>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup Label="REMOVE THIS">
|
||||
<InternalsVisibleTo Include="Spectre.Console.Cli.Tests" />
|
||||
<InternalsVisibleTo Include="Spectre.Console.Cli.Tests"/>
|
||||
</ItemGroup>
|
||||
|
||||
<PropertyGroup Condition="'$(TargetFramework)' == 'netstandard2.0'">
|
||||
@@ -18,17 +18,16 @@
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup Label="Dependencies">
|
||||
<PackageReference Include="OpenCli.Sources" />
|
||||
<PackageReference Include="PolySharp">
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
|
||||
</PackageReference>
|
||||
<PackageReference Include="Spectre.Console" />
|
||||
</ItemGroup>
|
||||
|
||||
|
||||
|
||||
<ItemGroup Condition="'$(TargetFramework)' == 'netstandard2.0'">
|
||||
<PackageReference Include="TunnelVisionLabs.ReferenceAssemblyAnnotator" PrivateAssets="all" />
|
||||
<PackageDownload Include="Microsoft.NETCore.App.Ref" Version="[$(AnnotatedReferenceAssemblyVersion)]" />
|
||||
<PackageReference Include="TunnelVisionLabs.ReferenceAssemblyAnnotator" PrivateAssets="all"/>
|
||||
<PackageDownload Include="Microsoft.NETCore.App.Ref" Version="[$(AnnotatedReferenceAssemblyVersion)]"/>
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
@@ -46,4 +45,8 @@
|
||||
</EmbeddedResource>
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\Spectre.Console\Spectre.Console.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
||||
@@ -28,4 +28,4 @@
|
||||
<Project Path="Spectre.Console.Testing/Spectre.Console.Testing.csproj" />
|
||||
<Project Path="Spectre.Console/Spectre.Console.csproj" />
|
||||
<Project Path="Tests/Spectre.Console.Tests/Spectre.Console.Tests.csproj" />
|
||||
</Solution>
|
||||
</Solution>
|
||||
@@ -3,16 +3,17 @@ namespace Spectre.Console.Tests;
|
||||
public static class Constants
|
||||
{
|
||||
public static string[] VersionCommand { get; } =
|
||||
new[]
|
||||
{
|
||||
CliConstants.Commands.Branch,
|
||||
CliConstants.Commands.Version,
|
||||
};
|
||||
[
|
||||
CliConstants.Commands.Branch,
|
||||
CliConstants.Commands.Version
|
||||
];
|
||||
|
||||
public static string[] XmlDocCommand { get; } =
|
||||
new[]
|
||||
{
|
||||
CliConstants.Commands.Branch,
|
||||
CliConstants.Commands.XmlDoc,
|
||||
};
|
||||
[
|
||||
CliConstants.Commands.Branch,
|
||||
CliConstants.Commands.XmlDoc
|
||||
];
|
||||
|
||||
public static string[] OpenCliOption { get; } =
|
||||
[CliConstants.DumpHelpOpenCliOption];
|
||||
}
|
||||
|
||||
@@ -0,0 +1,190 @@
|
||||
{
|
||||
"opencli": "0.1-draft",
|
||||
"info": {
|
||||
"title": "my-app",
|
||||
"version": "1.2.3"
|
||||
},
|
||||
"commands": [
|
||||
{
|
||||
"name": "animals",
|
||||
"commands": [
|
||||
{
|
||||
"name": "cat",
|
||||
"options": [
|
||||
{
|
||||
"name": "--agility",
|
||||
"required": false,
|
||||
"arguments": [
|
||||
{
|
||||
"name": "VALUE",
|
||||
"required": true,
|
||||
"arity": {
|
||||
"minimum": 1,
|
||||
"maximum": 1
|
||||
},
|
||||
"metadata": [
|
||||
{
|
||||
"name": "ClrType",
|
||||
"value": "System.Int32"
|
||||
}
|
||||
]
|
||||
}
|
||||
],
|
||||
"description": "The agility between 0 and 100.",
|
||||
"recursive": false,
|
||||
"hidden": false
|
||||
},
|
||||
{
|
||||
"name": "--alive",
|
||||
"required": false,
|
||||
"aliases": [
|
||||
"--not-dead",
|
||||
"-a"
|
||||
],
|
||||
"description": "Indicates whether or not the animal is alive.",
|
||||
"recursive": false,
|
||||
"hidden": false
|
||||
},
|
||||
{
|
||||
"name": "--name",
|
||||
"required": false,
|
||||
"aliases": [
|
||||
"--pet-name",
|
||||
"-n",
|
||||
"-p"
|
||||
],
|
||||
"arguments": [
|
||||
{
|
||||
"name": "VALUE",
|
||||
"required": true,
|
||||
"arity": {
|
||||
"minimum": 1,
|
||||
"maximum": 1
|
||||
},
|
||||
"metadata": [
|
||||
{
|
||||
"name": "ClrType",
|
||||
"value": "System.String"
|
||||
}
|
||||
]
|
||||
}
|
||||
],
|
||||
"recursive": false,
|
||||
"hidden": false
|
||||
}
|
||||
],
|
||||
"arguments": [
|
||||
{
|
||||
"name": "LEGS",
|
||||
"required": false,
|
||||
"arity": {
|
||||
"minimum": 1,
|
||||
"maximum": 1
|
||||
},
|
||||
"description": "The number of legs.",
|
||||
"hidden": false,
|
||||
"metadata": [
|
||||
{
|
||||
"name": "ClrType",
|
||||
"value": "System.Int32"
|
||||
}
|
||||
]
|
||||
}
|
||||
],
|
||||
"hidden": false,
|
||||
"examples": []
|
||||
},
|
||||
{
|
||||
"name": "dog",
|
||||
"options": [
|
||||
{
|
||||
"name": "--alive",
|
||||
"required": false,
|
||||
"aliases": [
|
||||
"--not-dead",
|
||||
"-a"
|
||||
],
|
||||
"description": "Indicates whether or not the animal is alive.",
|
||||
"recursive": false,
|
||||
"hidden": false
|
||||
},
|
||||
{
|
||||
"name": "--good-boy",
|
||||
"required": false,
|
||||
"aliases": [
|
||||
"-g"
|
||||
],
|
||||
"recursive": false,
|
||||
"hidden": false
|
||||
},
|
||||
{
|
||||
"name": "--name",
|
||||
"required": false,
|
||||
"aliases": [
|
||||
"--pet-name",
|
||||
"-n",
|
||||
"-p"
|
||||
],
|
||||
"arguments": [
|
||||
{
|
||||
"name": "VALUE",
|
||||
"required": true,
|
||||
"arity": {
|
||||
"minimum": 1,
|
||||
"maximum": 1
|
||||
},
|
||||
"metadata": [
|
||||
{
|
||||
"name": "ClrType",
|
||||
"value": "System.String"
|
||||
}
|
||||
]
|
||||
}
|
||||
],
|
||||
"recursive": false,
|
||||
"hidden": false
|
||||
}
|
||||
],
|
||||
"arguments": [
|
||||
{
|
||||
"name": "LEGS",
|
||||
"required": false,
|
||||
"arity": {
|
||||
"minimum": 1,
|
||||
"maximum": 1
|
||||
},
|
||||
"description": "The number of legs.",
|
||||
"hidden": false,
|
||||
"metadata": [
|
||||
{
|
||||
"name": "ClrType",
|
||||
"value": "System.Int32"
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "AGE",
|
||||
"required": true,
|
||||
"arity": {
|
||||
"minimum": 1,
|
||||
"maximum": 1
|
||||
},
|
||||
"hidden": false,
|
||||
"metadata": [
|
||||
{
|
||||
"name": "ClrType",
|
||||
"value": "System.Int32"
|
||||
}
|
||||
]
|
||||
}
|
||||
],
|
||||
"description": "The dog command.",
|
||||
"hidden": false,
|
||||
"examples": []
|
||||
}
|
||||
],
|
||||
"hidden": false,
|
||||
"examples": []
|
||||
}
|
||||
]
|
||||
}
|
||||
@@ -9,7 +9,6 @@
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" />
|
||||
<PackageReference Include="Microsoft.Extensions.DependencyInjection" />
|
||||
<PackageReference Include="Shouldly" />
|
||||
<PackageReference Include="Spectre.Console.Testing" />
|
||||
<PackageReference Include="Spectre.Verify.Extensions" />
|
||||
<PackageReference Include="Verify.Xunit" />
|
||||
<PackageReference Include="xunit" />
|
||||
@@ -21,6 +20,7 @@
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\..\Spectre.Console.Cli\Spectre.Console.Cli.csproj" />
|
||||
<ProjectReference Include="..\..\Spectre.Console.Testing\Spectre.Console.Testing.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
||||
@@ -0,0 +1,33 @@
|
||||
namespace Spectre.Console.Tests.Unit.Cli;
|
||||
|
||||
public sealed partial class CommandAppTests
|
||||
{
|
||||
[ExpectationPath("OpenCli")]
|
||||
public sealed partial class OpenCli
|
||||
{
|
||||
[Fact]
|
||||
[Expectation("Generate")]
|
||||
public Task Should_Output_OpenCli_Description()
|
||||
{
|
||||
// Given
|
||||
var fixture = new CommandAppTester();
|
||||
fixture.Configure(config =>
|
||||
{
|
||||
config.SetApplicationName("my-app");
|
||||
config.SetApplicationVersion("1.2.3");
|
||||
|
||||
config.AddBranch("animals", animals =>
|
||||
{
|
||||
animals.AddCommand<DogCommand>("dog");
|
||||
animals.AddCommand<CatCommand>("cat");
|
||||
});
|
||||
});
|
||||
|
||||
// When
|
||||
var result = fixture.Run(Constants.OpenCliOption);
|
||||
|
||||
// Then
|
||||
return Verifier.Verify(result.Output);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user