Skip to content

Commit 219ce6d

Browse files
committed
Show global help text when other commands are present and no arguments were provided
1 parent ab313a9 commit 219ce6d

File tree

3 files changed

+52
-3
lines changed

3 files changed

+52
-3
lines changed

src/ConsoleAppFramework/Emitter.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -595,7 +595,15 @@ void EmitRunBody(ILookup<string, CommandWithId> groupedCommands, int depth, bool
595595
{
596596
ifBlock = sb.BeginBlock($"if (args.Length == {depth})");
597597
}
598-
EmitLeafCommand(leafCommand);
598+
599+
if (ifBlock != null && depth == 0 && leafCommand?.Command.IsRootCommand == true && groupedCommands.Count > 1)
600+
{
601+
sb.AppendLine("ShowHelp(-1);");
602+
}
603+
else
604+
{
605+
EmitLeafCommand(leafCommand);
606+
}
599607
if (ifBlock != null)
600608
{
601609
sb.AppendLine("return;");

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 NoArgsShowsSameHelpTextAsHelp()
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
{

tests/ConsoleAppFramework.GeneratorTests/SubCommandTest.cs

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,19 @@ public void Zeroargs()
2222
builder.Run(args);
2323
""";
2424

25-
verifier.Execute(code, "", "root");
25+
verifier.Execute(code, "", """
26+
Usage: [command] [-h|--help] [--version]
27+
28+
Commands:
29+
a
30+
a b c d e f
31+
a b1
32+
a b2
33+
a b2 c
34+
a b2 d
35+
a b2 d e
36+
37+
""");
2638
verifier.Execute(code, "a", "a");
2739
verifier.Execute(code, "a b1", "a b1");
2840
verifier.Execute(code, "a b2", "a b2");
@@ -78,7 +90,19 @@ public void ZeroargsAsync()
7890
await builder.RunAsync(args);
7991
""";
8092

81-
verifier.Execute(code, "", "root");
93+
verifier.Execute(code, "", """
94+
Usage: [command] [-h|--help] [--version]
95+
96+
Commands:
97+
a
98+
a b c d e f
99+
a b1
100+
a b2
101+
a b2 c
102+
a b2 d
103+
a b2 d e
104+
105+
""");
82106
verifier.Execute(code, "a", "a");
83107
verifier.Execute(code, "a b1", "a b1");
84108
verifier.Execute(code, "a b2", "a b2");

0 commit comments

Comments
 (0)