Skip to content

Commit 98c6f3f

Browse files
stevefan1999-personalbuehler
andauthoredAug 28, 2023
fix: Generate good help text instead of stack trace on unrecognized command (#592)
So now it looks like this: ``` (base) PS E:\Git\github.com\stevefan1999-personal\dotnet-operator-sdk\src> dotnet run --project KubeOps.Play --framework net7.0 -- generator abcd Building... Specify --help for a list of available options and commands. Unrecognized command or argument 'abcd' Usage: KubeOps.Play generator [command] [options] Options: -f|--format <FORMAT> Determines the output format for the generator. Allowed values are: Yaml, Json. -o|--out <OUTPUT_PATH> The "root" path for the generator to put files in - if empty, prints to console. -?|-h|--help Show help information. Commands: crd Generates the needed CRD for kubernetes. docker Generates the docker file for building. installer Generates kustomization yaml for the whole installation of the operator. operator Generates the needed yamls to run the operator. rbac Generates the needed rbac roles for the operator. Run 'generator [command] -?|-h|--help' for more information about a command. ``` Instead of this: ``` (base) PS E:\Git\github.com\stevefan1999-personal\dotnet-operator-sdk\src> dotnet run --project KubeOps.Play --framework net7.0 -- generator abcd Building... E:\Git\github.com\stevefan1999-personal\dotnet-operator-sdk\src\KubeOps\Operator\HostExtensions.cs(33,13): warning SA1515: Single-line comment should be preceded by blank line (https://github.com/DotNetAnalyzers/StyleCopAnalyzers/blob/master/documentation/SA1515.md) [E:\Git\github .com\stevefan1999-personal\dotnet-operator-sdk\src\KubeOps\KubeOps.csproj::TargetFramework=net7.0] Specify --help for a list of available options and commands. Unhandled exception. McMaster.Extensions.CommandLineUtils.UnrecognizedCommandParsingException: Unrecognized command or argument 'abcd' at McMaster.Extensions.CommandLineUtils.CommandLineProcessor.ProcessUnexpectedArg(String argTypeName, String argValue) at McMaster.Extensions.CommandLineUtils.CommandLineProcessor.ProcessCommandOrParameter(CommandOrParameterArgument arg) at McMaster.Extensions.CommandLineUtils.CommandLineProcessor.ProcessNext() at McMaster.Extensions.CommandLineUtils.CommandLineProcessor.Process() at McMaster.Extensions.CommandLineUtils.CommandLineApplication.Parse(String[] args) at McMaster.Extensions.CommandLineUtils.CommandLineApplication.ExecuteAsync(String[] args, CancellationToken cancellationToken) at KubeOps.Operator.HostExtensions.RunOperatorAsync(IHost host, String[] args) in E:\Git\github.com\stevefan1999-personal\dotnet-operator-sdk\src\KubeOps\Operator\HostExtensions.cs:line 28 at Program.<Main>$(String[] args) in E:\Git\github.com\stevefan1999-personal\dotnet-operator-sdk\src\KubeOps.Play\Program.cs:line 10 at Program.<Main>(String[] args) ``` The `Specify --help for a list of available options and commands` stuff is unfortunately non-removable. https://github.com/natemcmaster/CommandLineUtils/blob/81199c7ec68367ea9612be55719dc1fe08f658da/src/CommandLineUtils/Internal/CommandLineProcessor.cs#L323 --------- Co-authored-by: Christoph Bühler <buehler@users.noreply.github.com>
1 parent 4495781 commit 98c6f3f

File tree

2 files changed

+13
-4
lines changed

2 files changed

+13
-4
lines changed
 

‎src/KubeOps/Operator/Commands/RunOperator.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
namespace KubeOps.Operator.Commands;
77

8-
[Command(Description = "Runs the operator.")]
8+
[Command(Description = "Runs the operator.", UsePagerForHelpText = true)]
99
[Subcommand(typeof(Generator))]
1010
[Subcommand(typeof(Install))]
1111
[Subcommand(typeof(Uninstall))]

‎src/KubeOps/Operator/HostExtensions.cs

+12-3
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,23 @@ public static class HostExtensions
1616
/// <param name="host">The <see cref="IHost"/>.</param>
1717
/// <param name="args">Program arguments.</param>
1818
/// <returns>Async task with completion result.</returns>
19-
public static Task<int> RunOperatorAsync(this IHost host, string[] args)
19+
public static async Task<int> RunOperatorAsync(this IHost host, string[] args)
2020
{
2121
var app = new CommandLineApplication<RunOperator>();
2222
app
2323
.Conventions
2424
.UseDefaultConventions()
2525
.UseConstructorInjection(host.Services);
26-
27-
return app.ExecuteAsync(args);
26+
try
27+
{
28+
return await app.ExecuteAsync(args);
29+
}
30+
catch (UnrecognizedCommandParsingException ex)
31+
{
32+
Console.WriteLine(ex.Message);
33+
ex.Command.Description = null;
34+
ex.Command.ShowHelp();
35+
return 1;
36+
}
2837
}
2938
}

0 commit comments

Comments
 (0)
Please sign in to comment.