Skip to content

Commit 31da0bc

Browse files
committed
Emit global help text when no input on root command with required parameters
1 parent 16f41f6 commit 31da0bc

File tree

1 file changed

+12
-9
lines changed

1 file changed

+12
-9
lines changed

src/ConsoleAppFramework/Emitter.cs

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
using Microsoft.CodeAnalysis;
2-
using System.Reflection.Metadata;
3-
using System.Text;
1+
using System.Text;
2+
using Microsoft.CodeAnalysis;
43

54
namespace ConsoleAppFramework;
65

@@ -16,7 +15,6 @@ public void EmitRun(SourceBuilder sb, CommandWithId commandWithId, bool isRunAsy
1615
var hasArgument = command.Parameters.Any(x => x.IsArgument);
1716
var hasValidation = command.Parameters.Any(x => x.HasValidation);
1817
var parsableParameterCount = command.Parameters.Count(x => x.IsParsable);
19-
var requiredParsableParameterCount = command.Parameters.Count(x => x.IsParsable && x.RequireCheckArgumentParsed);
2018

2119
if (command.HasFilter)
2220
{
@@ -43,7 +41,7 @@ public void EmitRun(SourceBuilder sb, CommandWithId commandWithId, bool isRunAsy
4341
sb.AppendLine();
4442
}
4543

46-
var cancellationTokenName = (emitForBuilder) ? "cancellationToken" : null;
44+
var cancellationTokenName = emitForBuilder ? "cancellationToken" : null;
4745
var cancellationTokenParameter = cancellationTokenName != null ? "CancellationToken cancellationToken" : null;
4846

4947
if (!emitForBuilder)
@@ -94,13 +92,13 @@ public void EmitRun(SourceBuilder sb, CommandWithId commandWithId, bool isRunAsy
9492
{
9593
noCommandArgsMemory = true; // emit help directly
9694
sb.AppendLine($"ReadOnlySpan<string> commandArgs = args;");
97-
sb.AppendLine($"if (TryShowHelpOrVersion(commandArgs, {requiredParsableParameterCount}, {commandWithId.Id})) return;");
95+
sb.AppendLine($"if (TryShowHelpOrVersion(commandArgs, {commandWithId.RequiredParsableParameterCount}, {commandWithId.Id})) return;");
9896
}
9997
}
10098

10199
if (!noCommandArgsMemory)
102100
{
103-
sb.AppendLine($"if (TryShowHelpOrVersion(commandArgsMemory.Span, {requiredParsableParameterCount}, {commandWithId.Id})) return;");
101+
sb.AppendLine($"if (TryShowHelpOrVersion(commandArgsMemory.Span, {commandWithId.RequiredParsableParameterCount}, {commandWithId.Id})) return;");
104102
}
105103
sb.AppendLine();
106104

@@ -595,7 +593,12 @@ void EmitRunBody(ILookup<string, CommandWithId> groupedCommands, int depth, bool
595593
{
596594
ifBlock = sb.BeginBlock($"if (args.Length == {depth})");
597595
}
598-
EmitLeafCommand(leafCommand);
596+
597+
var shouldShowGlobalHelp =
598+
ifBlock != null &&
599+
leafCommand is { Command.IsRootCommand: true, RequiredParsableParameterCount: > 0 };
600+
601+
EmitLeafCommand(shouldShowGlobalHelp ? null : leafCommand);
599602
if (ifBlock != null)
600603
{
601604
sb.AppendLine("return;");
@@ -1125,7 +1128,7 @@ string Join(string separator, params string?[] args)
11251128
return sb.ToString();
11261129
}
11271130

1128-
internal record CommandWithId(string? FieldType, Command Command, int Id)
1131+
internal record CommandWithId(string? FieldType, Command Command, int Id, int RequiredParsableParameterCount)
11291132
{
11301133
public static string BuildCustomDelegateTypeName(int id)
11311134
{

0 commit comments

Comments
 (0)