Skip to content

Commit cb82a75

Browse files
committed
Show global HelpText when other commands are present and no args were provided
1 parent ab313a9 commit cb82a75

File tree

3 files changed

+61
-3
lines changed

3 files changed

+61
-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: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,32 @@ public void ListWithRoot()
136136
verifier.Execute(code, args: "--help", expected: """
137137
Usage: [command] [options...] [-h|--help] [--version]
138138
139+
Options:
140+
--x <int> (Required)
141+
--y <int> (Required)
142+
143+
Commands:
144+
a
145+
a b c
146+
ab
147+
148+
""");
149+
}
150+
151+
[Fact]
152+
public void NoArgsShowsGlobalSummary()
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+
verifier.Execute(code, args: "", expected: """
163+
Usage: [command] [options...] [-h|--help] [--version]
164+
139165
Options:
140166
--x <int> (Required)
141167
--y <int> (Required)

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)