Skip to content

Commit 8807ac9

Browse files
committed
Merge remote-tracking branch 'origin/master'
2 parents 97e7d38 + cc68b96 commit 8807ac9

File tree

3 files changed

+35
-13
lines changed

3 files changed

+35
-13
lines changed

src/ConsoleAppFramework/ConsoleAppGenerator.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,7 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
9999
{
100100
if (node.IsKind(SyntaxKind.InvocationExpression))
101101
{
102-
var invocationExpression = (node as InvocationExpressionSyntax);
103-
if (invocationExpression == null) return false;
102+
if (node is not InvocationExpressionSyntax invocationExpression) return false;
104103

105104
var expr = invocationExpression.Expression as MemberAccessExpressionSyntax;
106105
if ((expr?.Expression as IdentifierNameSyntax)?.Identifier.Text == "ConsoleApp")
@@ -220,7 +219,8 @@ static void EmitConsoleAppRun(SourceProductionContext sourceProductionContext, C
220219
using (sb.BeginBlock("internal static partial class ConsoleApp"))
221220
{
222221
var emitter = new Emitter(null);
223-
var withId = new Emitter.CommandWithId(null, command, -1);
222+
var requiredParsableParameterCount = command.Parameters.Count(p => p.IsParsable && p.RequireCheckArgumentParsed);
223+
var withId = new Emitter.CommandWithId(null, command, -1, requiredParsableParameterCount);
224224
emitter.EmitRun(sb, withId, commandContext.IsAsync, null);
225225
}
226226
sourceProductionContext.AddSource("ConsoleApp.Run.g.cs", sb.ToString().ReplaceLineEndings());
@@ -274,7 +274,9 @@ static void EmitConsoleAppBuilder(SourceProductionContext sourceProductionContex
274274
var command = new Emitter.CommandWithId(
275275
FieldType: x!.BuildDelegateSignature(Emitter.CommandWithId.BuildCustomDelegateTypeName(i), out var delegateDef),
276276
Command: x!,
277-
Id: i
277+
Id: i,
278+
279+
RequiredParsableParameterCount: x!.Parameters.Count(p => p.IsParsable && p.RequireCheckArgumentParsed)
278280
);
279281
if (delegateDef != null)
280282
{

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;");
@@ -1137,7 +1140,7 @@ string Join(string separator, params string?[] args)
11371140
return sb.ToString();
11381141
}
11391142

1140-
internal record CommandWithId(string? FieldType, Command Command, int Id)
1143+
internal record CommandWithId(string? FieldType, Command Command, int Id, int RequiredParsableParameterCount)
11411144
{
11421145
public static string BuildCustomDelegateTypeName(int id)
11431146
{

tests/ConsoleAppFramework.GeneratorTests/HelpTest.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,23 @@ a b c
148148
""");
149149
}
150150

151+
[Fact]
152+
public void NoArgsOnRootShowsSameHelpTextAsHelpWhenParametersAreRequired()
153+
{
154+
var code = """
155+
var app = ConsoleApp.Create();
156+
app.Add("", (int x, int y) => { });
157+
app.Add("a", (int x, int y) => { });
158+
app.Add("ab", (int x, int y) => { });
159+
app.Add("a b c", (int x, int y) => { });
160+
app.Run(args);
161+
""";
162+
var noArgsOutput = verifier.Error(code, "");
163+
var helpOutput = verifier.Error(code, "--help");
164+
165+
noArgsOutput.ShouldBe(helpOutput);
166+
}
167+
151168
[Fact]
152169
public void SelectLeafHelp()
153170
{

0 commit comments

Comments
 (0)