Skip to content

Commit

Permalink
Merge pull request #4 from nd1012/dev
Browse files Browse the repository at this point in the history
+ Added `CliApiContext.ForceDisplayHelp`
+ Default CLI API helper won't diplay help on API method execution error (only display the exception)
+ `CliHelpApi` won't display help on API method execution error
+ Added `AboutApi`
- Fixed wrong error display, when the exit code was not zero
  • Loading branch information
nd1012 authored Apr 12, 2024
2 parents c647447 + e17fcd4 commit 5e82567
Show file tree
Hide file tree
Showing 28 changed files with 1,023 additions and 25 deletions.
8 changes: 3 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -295,11 +295,9 @@ to an ANSI console. The default colors used can be customized in the static
`CliApiInfo` properties.

All help output can be localized. For a full localization, you can parse the
`wan24-CLI` source code with POEdit, for example, too. Parser should look for
these phrases:

- `_("...")` (single underscore)
- `__("...")` (double underscore)
`wan24-CLI` source code with
[wan24-I8NTools](https://github.com/nd1012/wan24-I8NTools) to get a common PO
file.

For intercepting errors there are multiple ways:

Expand Down
6 changes: 3 additions & 3 deletions src/wan24-CLI Tests/wan24-CLI Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.9.0" />
<PackageReference Include="MSTest.TestAdapter" Version="3.2.2" />
<PackageReference Include="MSTest.TestFramework" Version="3.2.2" />
<PackageReference Include="coverlet.collector" Version="6.0.1">
<PackageReference Include="MSTest.TestAdapter" Version="3.3.1" />
<PackageReference Include="MSTest.TestFramework" Version="3.3.1" />
<PackageReference Include="coverlet.collector" Version="6.0.2">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
Expand Down
5 changes: 0 additions & 5 deletions src/wan24-CLI.sln
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "wan24-CLI", "wan24-CLI\wan2
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "wan24-CLI Tests", "wan24-CLI Tests\wan24-CLI Tests.csproj", "{4F4F8E45-85C3-4BF8-BF3E-5F941D7289EE}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "wan24-CLI Docs", "wan24-CLI Docs\wan24-CLI Docs.csproj", "{EB50E228-D3DA-4F5E-99A9-56EFE413E5CB}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ObjectValidation", "..\..\ObjectValidation\src\ObjectValidation\ObjectValidation.csproj", "{1A023CA2-4FB0-421D-827B-4EC017B50AB7}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Wan24-Core", "..\..\wan24-Core\src\Wan24-Core\Wan24-Core.csproj", "{E7C5B32A-2097-4056-ABDB-D798F2E907D6}"
Expand All @@ -34,9 +32,6 @@ Global
{4F4F8E45-85C3-4BF8-BF3E-5F941D7289EE}.Release|Any CPU.Build.0 = Release|Any CPU
{4F4F8E45-85C3-4BF8-BF3E-5F941D7289EE}.Trunk|Any CPU.ActiveCfg = Trunk|Any CPU
{4F4F8E45-85C3-4BF8-BF3E-5F941D7289EE}.Trunk|Any CPU.Build.0 = Trunk|Any CPU
{EB50E228-D3DA-4F5E-99A9-56EFE413E5CB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{EB50E228-D3DA-4F5E-99A9-56EFE413E5CB}.Release|Any CPU.ActiveCfg = Release|Any CPU
{EB50E228-D3DA-4F5E-99A9-56EFE413E5CB}.Trunk|Any CPU.ActiveCfg = Trunk|Any CPU
{1A023CA2-4FB0-421D-827B-4EC017B50AB7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{1A023CA2-4FB0-421D-827B-4EC017B50AB7}.Release|Any CPU.ActiveCfg = Release|Any CPU
{1A023CA2-4FB0-421D-827B-4EC017B50AB7}.Trunk|Any CPU.ActiveCfg = Debug|Any CPU
Expand Down
58 changes: 58 additions & 0 deletions src/wan24-CLI/AboutApi.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
using Spectre.Console;
using System.ComponentModel;
using System.Reflection;
using wan24.Core;

namespace wan24.CLI
{
/// <summary>
/// About CLI API
/// </summary>
[CliApi("about")]
[DisplayText("About")]
[Description("Display informations about this CLI app")]
public sealed class AboutApi
{
/// <summary>
/// Constructor
/// </summary>
public AboutApi() { }

/// <summary>
/// App title
/// </summary>
public static string Title { get; set; } = Settings.AppId;

/// <summary>
/// App informations to display (Spectre.Console markup is supported)
/// </summary>
public static string? Info { get; set; }

/// <summary>
/// App version
/// </summary>
public static Version Version { get; set; } = new("1.0.0");

/// <summary>
/// Display the app informations
/// </summary>
[CliApi("info", IsDefault = true)]
[DisplayText("Informations")]
[Description("Display detailed app informations")]
public static void DisplayInfo()
{
DisplayVersion();
if (Info is null) return;
Console.WriteLine();
AnsiConsole.WriteLine(Info);
}

/// <summary>
/// Display the app version
/// </summary>
[CliApi("version")]
[DisplayText("Version")]
[Description("Display app version informations")]
public static void DisplayVersion() => Console.WriteLine($"{Title} version {Version} ({(ENV.IsDebug ? "Debug build" : "Release build")})");
}
}
4 changes: 2 additions & 2 deletions src/wan24-CLI/CliApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ public static async Task<int> RunMultiAsync(string[]? args = null, CancellationT
if (Trace) WriteTrace($"API call returned with exit code {exitCode}");
if (exitCode != 0)
{
AnsiConsole.MarkupLine($"[{CliApiInfo.ApiNameColor}{(CliApiInfo.ApiNameColor.Contains(" on ") ? string.Empty : $" {CliApiInfo.BackGroundColor}")}]{_("Break in arguments chunk ${0} with exit code %{1}", (i + 1).ToString(), exitCode.ToString())}[/]");
AnsiConsole.MarkupLine($"[{CliApiInfo.ApiNameColor}{(CliApiInfo.ApiNameColor.Contains(" on ") ? string.Empty : $" {CliApiInfo.BackGroundColor}")}]{_("Break in arguments chunk %{0} with exit code %{1}", (i + 1).ToString(), exitCode.ToString())}[/]");
return exitCode;
}
startArg = endArg + 1;
Expand Down Expand Up @@ -546,7 +546,7 @@ public static async Task<int> DisplayHelpAsync(
)
{
if (Trace) WriteTrace("Display context API help");
DisplayHelpHeader();
if (context.Parameters is null || context.Exception is null || context.ForceDisplayHelp) DisplayHelpHeader();
if (useApi)
{
// Try the API type error handler / help provider
Expand Down
5 changes: 5 additions & 0 deletions src/wan24-CLI/CliApiContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ internal CliApiContext() { }
/// </summary>
public Exception? Exception { get; set; }

/// <summary>
/// Force displaying help on API method execution error?
/// </summary>
public bool ForceDisplayHelp { get; set; }

/// <summary>
/// Get the exported API names
/// </summary>
Expand Down
4 changes: 3 additions & 1 deletion src/wan24-CLI/CliApiHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ public CliApiHelper() { }
public virtual int DisplayHelp(CliApiContext context)
{
Contract.Assume(CliApi.ExportedApis is not null);
CliApi.DisplayHelpHeader();
bool displayHelp = context.Parameters is null || context.Exception is null || context.ForceDisplayHelp;
if (displayHelp) CliApi.DisplayHelpHeader();
CliArgException? argException = context.Exception as CliArgException;
if (context.Exception is not null)
{
Expand All @@ -33,6 +34,7 @@ public virtual int DisplayHelp(CliApiContext context)
: $"[white on red]{_("An exception has been catched")}: {(CliApi.DisplayFullExceptions ? context.Exception.ToString().EscapeMarkup() : context.Exception.Message.EscapeMarkup())}[/]");
CliApi.StdErr.WriteLine();
}
if (!displayHelp) return 1;
CliHelpApi help = CliApi.ExportedApis.Values.Where(a => typeof(CliHelpApi).IsAssignableFrom(a.Type)).FirstOrDefault()?.Type is Type apiHelpType
? apiHelpType.ConstructAuto() as CliHelpApi ?? throw new InvalidProgramException($"Failed to instance API help from {apiHelpType}")
: new();
Expand Down
1 change: 1 addition & 0 deletions src/wan24-CLI/CliHelpApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ public CliHelpApi() { }
public virtual int Help()
{
Contract.Assert(CliApi.CurrentContext is not null && CliApi.ExportedApis is not null);
if (CliApi.CurrentContext.Parameters is not null && CliApi.CurrentContext.Exception is not null && !CliApi.CurrentContext.ForceDisplayHelp) return 1;
CliApi.DisplayHelpHeader();
CliApiInfo? apiInfo = ApiName is null
? null
Expand Down
8 changes: 3 additions & 5 deletions src/wan24-CLI/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -295,11 +295,9 @@ to an ANSI console. The default colors used can be customized in the static
`CliApiInfo` properties.

All help output can be localized. For a full localization, you can parse the
`wan24-CLI` source code with POEdit, for example, too. Parser should look for
these phrases:

- `_("...")` (single underscore)
- `__("...")` (double underscore)
`wan24-CLI` source code with
[wan24-I8NTools](https://github.com/nd1012/wan24-I8NTools) to get a common PO
file.

For intercepting errors there are multiple ways:

Expand Down
3 changes: 1 addition & 2 deletions src/wan24-CLI/StringExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Reflection;
using System.Text;
using System.Text;

namespace wan24.CLI
{
Expand Down
21 changes: 21 additions & 0 deletions src/wan24-CLI/wan24-CLI licenses/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2023 Andreas Zimmermann, wan24.de

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@

<!DOCTYPE html>

<html lang="en">
<head>
<link rel="stylesheet" href="/Content/Site.css" />
<title>&#39;MIT&#39; reference</title>
</head>
<body>
<div id="main-content">



<h1>MIT License</h1>

<h2>SPDX identifier</h2>
<div id="license-expression">MIT</div>

<h2>License text</h2>

<div class="optional-license-text">
<p>MIT License</p>

</div>
<div class="replaceable-license-text">
<p>Copyright (c) &lt;year&gt; &lt;copyright holders&gt;
</p>

</div>

<p>Permission is hereby granted, free of charge, to any person obtaining a copy of <var class="replaceable-license-text"> this software and
associated documentation files</var> (the &quot;Software&quot;), to deal in the Software without restriction,
including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:</p>

<p>The above copyright notice and this permission notice
<var class="optional-license-text"> (including the next paragraph)</var>
shall be included in all copies or substantial
portions of the Software.</p>

<p>THE SOFTWARE IS PROVIDED &quot;AS IS&quot;, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
NO EVENT SHALL <var class="replaceable-license-text"> THE AUTHORS OR COPYRIGHT HOLDERS</var> BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.</p>






<h2>SPDX web page</h2>
<ul>
<li><a href="https://spdx.org/licenses/MIT.html">https://spdx.org/licenses/MIT.html</a></li>
</ul>

<h2>Notice</h2>
<p>This license content is provided by the <a href="https://spdx.dev/">SPDX project</a>. For more information about <b>licenses.nuget.org</b>, see <a href="https://aka.ms/licenses.nuget.org">our documentation</a>.

<p><i>Data pulled from <a href="https://github.com/spdx/license-list-data">spdx/license-list-data</a> on February 9, 2023.</i></p>
</div>
</body>
</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@

<!DOCTYPE html>

<html lang="en">
<head>
<link rel="stylesheet" href="/Content/Site.css" />
<title>&#39;MIT&#39; reference</title>
</head>
<body>
<div id="main-content">



<h1>MIT License</h1>

<h2>SPDX identifier</h2>
<div id="license-expression">MIT</div>

<h2>License text</h2>

<div class="optional-license-text">
<p>MIT License</p>

</div>
<div class="replaceable-license-text">
<p>Copyright (c) &lt;year&gt; &lt;copyright holders&gt;
</p>

</div>

<p>Permission is hereby granted, free of charge, to any person obtaining a copy of <var class="replaceable-license-text"> this software and
associated documentation files</var> (the &quot;Software&quot;), to deal in the Software without restriction,
including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:</p>

<p>The above copyright notice and this permission notice
<var class="optional-license-text"> (including the next paragraph)</var>
shall be included in all copies or substantial
portions of the Software.</p>

<p>THE SOFTWARE IS PROVIDED &quot;AS IS&quot;, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
NO EVENT SHALL <var class="replaceable-license-text"> THE AUTHORS OR COPYRIGHT HOLDERS</var> BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.</p>






<h2>SPDX web page</h2>
<ul>
<li><a href="https://spdx.org/licenses/MIT.html">https://spdx.org/licenses/MIT.html</a></li>
</ul>

<h2>Notice</h2>
<p>This license content is provided by the <a href="https://spdx.dev/">SPDX project</a>. For more information about <b>licenses.nuget.org</b>, see <a href="https://aka.ms/licenses.nuget.org">our documentation</a>.

<p><i>Data pulled from <a href="https://github.com/spdx/license-list-data">spdx/license-list-data</a> on February 9, 2023.</i></p>
</div>
</body>
</html>
Loading

0 comments on commit 5e82567

Please sign in to comment.