Skip to content

Commit fdda0a8

Browse files
committed
support command alias
1 parent 5b1bdbe commit fdda0a8

5 files changed

Lines changed: 97 additions & 10 deletions

File tree

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,23 @@
1-
using ConsoleAppFramework;
1+
using ConsoleAppFramework;
22

3-
args = "9999 --x 1000".Split(' ');
3+
// args = ["foo", "--help"];
44

5-
ConsoleApp.Run(args, (int x, [Argument] int y) =>
5+
var app = ConsoleApp.Create();
6+
7+
app.Add("build|b", () => { });
8+
app.Add("test|t", () => { });
9+
app.Add("keyvault|kv", () => { });
10+
app.Add<Commands>();
11+
12+
app.Run(args);
13+
14+
public class Commands
615
{
7-
Console.WriteLine((x, y));
8-
});
16+
/// <summary>Analyze the current package and report errors, but don't build object files.</summary>
17+
[Command("check|c")]
18+
public void Check() { }
19+
20+
/// <summary>Build this packages's and its dependencies' documenation.</summary>
21+
[Command("doc|d")]
22+
public void Doc() { }
23+
}

src/ConsoleAppFramework/CommandHelpBuilder.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using Microsoft.CodeAnalysis;
1+
using Microsoft.CodeAnalysis;
22
using System.Text;
33

44
namespace ConsoleAppFramework;
@@ -222,7 +222,7 @@ static string BuildMethodListMessage(IEnumerable<Command> commands, out int maxW
222222
.Where(x => !x.IsHidden)
223223
.Select(x =>
224224
{
225-
return (Command: x.Name, x.Description);
225+
return (Command: string.Join(", ", x.Name.Split('|')), x.Description);
226226
})
227227
.ToArray();
228228
maxWidth = formatted.Max(x => x.Command.Length);

src/ConsoleAppFramework/Emitter.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -465,8 +465,10 @@ public void EmitRun(SourceBuilder sb, CommandWithId commandWithId, bool isRunAsy
465465

466466
public void EmitBuilder(SourceBuilder sb, CommandWithId[] commandIds, bool emitSync, bool emitAsync)
467467
{
468-
// grouped by path
469-
var commandGroup = commandIds.ToLookup(x => x.Command.Name.Split(' ')[0]);
468+
// split command-alias and grouped by path
469+
var commandGroup = commandIds
470+
.SelectMany(x => x.Command.Name.Split('|'), (command, name) => command with { Command = command.Command with { Name = name } })
471+
.ToLookup(x => x.Command.Name.Split(' ')[0]);
470472
var hasRootCommand = commandIds.Any(x => x.Command.IsRootCommand);
471473

472474
using (sb.BeginBlock("partial class ConsoleAppBuilder"))
@@ -939,7 +941,7 @@ public void EmitConfigure(SourceBuilder sb, DllReference dllReference)
939941
{
940942
if (dllReference.HasDependencyInjection)
941943
{
942-
using(sb.BeginBlock("if (!isRequireCallBuildAndSetServiceProvider)"))
944+
using (sb.BeginBlock("if (!isRequireCallBuildAndSetServiceProvider)"))
943945
{
944946
sb.AppendLine("return;");
945947
}

tests/ConsoleAppFramework.GeneratorTests/ConsoleAppBuilderTest.cs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,4 +278,38 @@ public override Task InvokeAsync(ConsoleAppContext context,CancellationToken can
278278

279279
verifier.Execute(code, "nomunomu", "filter1-filter2-command");
280280
}
281+
282+
[Fact]
283+
public void CommandAlias()
284+
{
285+
var code = """
286+
var app = ConsoleApp.Create();
287+
288+
app.Add("build|b", () => { Console.Write("build ok"); });
289+
app.Add("test|t", () => { Console.Write("test ok"); });
290+
app.Add<Commands>();
291+
292+
app.Run(args);
293+
294+
public class Commands
295+
{
296+
/// <summary>Analyze the current package and report errors, but don't build object files.</summary>
297+
[Command("check|c")]
298+
public void Check() { Console.Write("check ok"); }
299+
300+
/// <summary>Build this packages's and its dependencies' documenation.</summary>
301+
[Command("doc|d")]
302+
public void Doc() { Console.Write("doc ok"); }
303+
}
304+
""";
305+
306+
verifier.Execute(code, "b", "build ok");
307+
verifier.Execute(code, "build", "build ok");
308+
verifier.Execute(code, "t", "test ok");
309+
verifier.Execute(code, "test", "test ok");
310+
verifier.Execute(code, "c", "check ok");
311+
verifier.Execute(code, "check", "check ok");
312+
verifier.Execute(code, "d", "doc ok");
313+
verifier.Execute(code, "doc", "doc ok");
314+
}
281315
}

tests/ConsoleAppFramework.GeneratorTests/HelpTest.cs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -397,4 +397,40 @@ private static string GetEntryAssemblyVersion()
397397

398398
return version;
399399
}
400+
401+
[Fact]
402+
public void CommandAlias()
403+
{
404+
var code = """
405+
var app = ConsoleApp.Create();
406+
407+
app.Add("build|b", () => { Console.Write("build ok"); });
408+
app.Add("test|t", () => { Console.Write("test ok"); });
409+
app.Add<Commands>();
410+
411+
app.Run(args);
412+
413+
public class Commands
414+
{
415+
/// <summary>Analyze the current package and report errors, but don't build object files.</summary>
416+
[Command("check|c")]
417+
public void Check() { Console.Write("check ok"); }
418+
419+
/// <summary>Build this packages's and its dependencies' documenation.</summary>
420+
[Command("doc|d")]
421+
public void Doc() { Console.Write("doc ok"); }
422+
}
423+
""";
424+
425+
verifier.Execute(code, "--help", """
426+
Usage: [command] [-h|--help] [--version]
427+
428+
Commands:
429+
build, b
430+
check, c Analyze the current package and report errors, but don't build object files.
431+
doc, d Build this packages's and its dependencies' documenation.
432+
test, t
433+
434+
""");
435+
}
400436
}

0 commit comments

Comments
 (0)