Skip to content

Commit 1ab3137

Browse files
authored
Merge pull request #28 from Cysharp/BetterHelp
Display more human-friendly help message.
2 parents 93db30b + 7b24121 commit 1ab3137

File tree

8 files changed

+752
-85
lines changed

8 files changed

+752
-85
lines changed

sandbox/MultiContainedApp/Program.cs

+2-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ await BatchHost.CreateDefaultBuilder()
1717

1818
public class Foo : BatchBase
1919
{
20-
public void Echo(string msg)
20+
[Command("Echo", "Echo message to the logger")]
21+
public void Echo([Option("msg", "Message to send.")]string msg)
2122
{
2223
this.Context.Logger.LogInformation(msg);
2324
}
+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Runtime.CompilerServices;
4+
using System.Text;
5+
6+
[assembly:InternalsVisibleTo("MicroBatchFramework.Tests, PublicKey="+
7+
"002400000480000094000000060200000024000052534131000400000100010089b12412c27f5f"+
8+
"aece3868b659bf311f2550c5cc1b4cbbe032a048ec36fa288872d51e8ddfd77a83e835c6ea3940"+
9+
"c331fe89d1ba9146a12abed588f194cd437cfe81252634d49214acf6b11dc9e97cfc6d0f818082"+
10+
"e5bbafb50e890eed19517c199213075b294d5fa59556cd42186041a1a95f8cff3869575bed4de2"+
11+
"dd8f5dc2")]

src/MicroBatchFramework/BatchEngine.cs

+1-69
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ public async Task RunAsync(Type type, string[] args)
102102
}
103103
else
104104
{
105-
Console.WriteLine(BatchEngine.BuildHelpParameter(methods));
105+
Console.Write(new CommandHelpBuilder().BuildHelpMessage(methods, null));
106106
return;
107107
}
108108
}
@@ -370,74 +370,6 @@ struct OptionParameter
370370
public bool BooleanSwitch;
371371
}
372372

373-
internal static string BuildHelpParameter(MethodInfo[] methods)
374-
{
375-
var sb = new StringBuilder();
376-
foreach (var method in methods.OrderBy(x => x, new CustomSorter()))
377-
{
378-
var command = method.GetCustomAttribute<CommandAttribute>();
379-
if (command != null)
380-
{
381-
sb.AppendLine(string.Join(", ", command.CommandNames) + ": " + command.Description);
382-
}
383-
else
384-
{
385-
sb.AppendLine("argument list:");
386-
}
387-
388-
var parameters = method.GetParameters();
389-
if (parameters.Length == 0)
390-
{
391-
sb.AppendLine("()");
392-
}
393-
394-
foreach (var item in parameters)
395-
{
396-
// -i, -input | [default=foo]...
397-
398-
var option = item.GetCustomAttribute<OptionAttribute>();
399-
400-
if (option != null)
401-
{
402-
if (option.Index != -1)
403-
{
404-
sb.Append("[" + option.Index + "]");
405-
goto WRITE_DESCRIPTION;
406-
}
407-
else
408-
{
409-
// If Index is -1, ShortName is initialized at Constractor.
410-
sb.Append("-" + option.ShortName!.Trim('-') + ", ");
411-
}
412-
}
413-
414-
sb.Append("-" + item.Name);
415-
416-
WRITE_DESCRIPTION:
417-
sb.Append(": ");
418-
419-
if (item.HasDefaultValue)
420-
{
421-
sb.Append("[default=" + (item.DefaultValue?.ToString() ?? "null") + "]");
422-
}
423-
424-
if (option != null && !string.IsNullOrEmpty(option.Description))
425-
{
426-
sb.Append(option.Description);
427-
}
428-
else
429-
{
430-
sb.Append(item.ParameterType.Name);
431-
}
432-
sb.AppendLine();
433-
}
434-
435-
sb.AppendLine();
436-
}
437-
438-
return sb.ToString();
439-
}
440-
441373
class CustomSorter : IComparer<MethodInfo>
442374
{
443375
public int Compare(MethodInfo x, MethodInfo y)

src/MicroBatchFramework/BatchEngineHostBuilderExtensions.cs

+7-13
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,11 @@ public static IHostBuilder UseBatchEngine(this IHostBuilder hostBuilder, string[
3030
var (t, mi) = GetTypeFromAssemblies(args[1]);
3131
if (mi != null)
3232
{
33-
Console.WriteLine(BatchEngine.BuildHelpParameter(new[] { mi }));
33+
Console.Write(new CommandHelpBuilder().BuildHelpMessage(mi, showCommandName: true));
3434
}
3535
else
3636
{
37-
Console.WriteLine("Method not found , please check \"list\" command.");
37+
Console.Error.WriteLine("Method not found , please check \"list\" command.");
3838
}
3939
hostBuilder.ConfigureServices(services =>
4040
{
@@ -96,7 +96,8 @@ public static IHostBuilder UseBatchEngine<T>(this IHostBuilder hostBuilder, stri
9696
{
9797
if (!hasHelp)
9898
{
99-
Console.WriteLine(BatchEngine.BuildHelpParameter(method));
99+
Console.Write(new CommandHelpBuilder().BuildHelpMessage(method, defaultMethod));
100+
100101
hostBuilder.ConfigureServices(services =>
101102
{
102103
services.AddOptions<ConsoleLifetimeOptions>().Configure(x => x.SuppressStatusMessages = true);
@@ -125,7 +126,8 @@ public static IHostBuilder UseBatchEngine<T>(this IHostBuilder hostBuilder, stri
125126

126127
if (!hasHelp && args.Length == 1 && args[0].Equals(HelpCommand, StringComparison.OrdinalIgnoreCase))
127128
{
128-
Console.WriteLine(BatchEngine.BuildHelpParameter(method));
129+
Console.Write(new CommandHelpBuilder().BuildHelpMessage(method, defaultMethod));
130+
129131
hostBuilder.ConfigureServices(services =>
130132
{
131133
services.AddOptions<ConsoleLifetimeOptions>().Configure(x => x.SuppressStatusMessages = true);
@@ -155,15 +157,7 @@ public static Task RunBatchEngineAsync<T>(this IHostBuilder hostBuilder, string[
155157

156158
static void ShowMethodList()
157159
{
158-
Console.WriteLine("list of methods:");
159-
var list = GetBatchTypes();
160-
foreach (var item in list)
161-
{
162-
foreach (var item2 in item.GetMethods(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly))
163-
{
164-
Console.WriteLine(item.Name + "." + item2.Name);
165-
}
166-
}
160+
Console.Write(new CommandHelpBuilder().BuildHelpMessage(GetBatchTypes()));
167161
}
168162

169163
static List<Type> GetBatchTypes()

0 commit comments

Comments
 (0)