Skip to content

Commit 6d9827e

Browse files
committed
remove list command, add version command, allow help as -help, --help, show help command in single type
1 parent 49e11f7 commit 6d9827e

File tree

3 files changed

+141
-41
lines changed

3 files changed

+141
-41
lines changed

sandbox/SingleContainedApp/Program.cs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,15 @@ public void Foo(int[] array, Person person)
122122
}
123123
}
124124

125+
public class StandardArgTest : ConsoleAppBase
126+
{
127+
public void Run([Option(0, "message of x.")]string x)
128+
{
129+
Console.WriteLine("1." + x);
130+
//Console.WriteLine("2." + y);
131+
}
132+
}
133+
125134
public class Person
126135
{
127136
public int Age { get; set; }
@@ -140,7 +149,7 @@ await ConsoleAppHost.CreateDefaultBuilder()
140149
// mapping config json to IOption<MyConfig>
141150
services.Configure<MyConfig>(hostContext.Configuration);
142151
})
143-
.RunConsoleAppEngineAsync<OverrideCheck>(args);
152+
.RunConsoleAppEngineAsync<StandardArgTest>(args);
144153
}
145154
}
146155
}

src/ConsoleAppFramework/CommandHelpBuilder.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,9 @@ public string BuildHelpMessage(MethodInfo[] methodInfo, MethodInfo? defaultMetho
4343
if (defaultMethod != null)
4444
{
4545
// Display a help messages for default method
46-
sb.Append(BuildHelpMessage(CreateCommandHelpDefinition(defaultMethod), showCommandName:false));
46+
sb.Append(BuildHelpMessage(CreateCommandHelpDefinition(defaultMethod), showCommandName: false));
4747
}
48-
48+
4949
if (methodInfo.Length > 1)
5050
{
5151
// Display sub commands list.
@@ -155,13 +155,13 @@ public string BuildOptionsMessage(CommandHelpDefinition definition)
155155
.Where(x => !x.Index.HasValue)
156156
.Select(x => (Options: string.Join(", ", x.Options) + $" <{x.ValueTypeName}>", x.Description, x.IsRequired, x.DefaultValue))
157157
.ToArray();
158-
158+
159159
if (!optionsFormatted.Any()) return string.Empty;
160-
160+
161161
var maxWidth = optionsFormatted.Max(x => x.Options.Length);
162162

163163
var sb = new StringBuilder();
164-
164+
165165
sb.AppendLine("Options:");
166166
foreach (var opt in optionsFormatted)
167167
{

src/ConsoleAppFramework/ConsoleAppEngineHostBuilderExtensions.cs

Lines changed: 126 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -10,45 +10,73 @@ namespace ConsoleAppFramework
1010
{
1111
public static class ConsoleAppEngineHostBuilderExtensions
1212
{
13-
const string ListCommand = "list";
1413
const string HelpCommand = "help";
14+
const string VersionCommand = "version";
1515

1616
public static IHostBuilder UseConsoleAppEngine(this IHostBuilder hostBuilder, string[] args, IConsoleAppInterceptor? interceptor = null)
1717
{
18-
if (args.Length == 0 || (args.Length == 1 && args[0].Trim('-').Equals(ListCommand, StringComparison.OrdinalIgnoreCase)))
18+
IHostBuilder ConfigureEmptyService()
1919
{
20-
ShowMethodList();
2120
hostBuilder.ConfigureServices(services =>
2221
{
2322
services.AddOptions<ConsoleLifetimeOptions>().Configure(x => x.SuppressStatusMessages = true);
2423
services.AddSingleton<IHostedService, EmptyHostedService>();
2524
});
2625
return hostBuilder;
2726
}
28-
if (args.Length == 2 && args[0].Trim('-').Equals(HelpCommand, StringComparison.OrdinalIgnoreCase))
27+
28+
// () or -help
29+
if (args.Length == 0 || (args.Length == 1 && TrimEquals(args[0], HelpCommand)))
30+
{
31+
ShowMethodList();
32+
ConfigureEmptyService();
33+
return hostBuilder;
34+
}
35+
36+
// -version
37+
if (args.Length == 1 && TrimEquals(args[0], VersionCommand))
38+
{
39+
ShowVersion();
40+
ConfigureEmptyService();
41+
return hostBuilder;
42+
}
43+
44+
if (args.Length == 2)
2945
{
30-
var (t, mi) = GetTypeFromAssemblies(args[1]);
31-
if (mi != null)
46+
int methodIndex = -1;
47+
48+
// help command
49+
if (TrimEquals(args[0], HelpCommand))
3250
{
33-
Console.Write(new CommandHelpBuilder().BuildHelpMessage(mi, showCommandName: true));
51+
methodIndex = 1;
3452
}
35-
else
53+
// command -help
54+
else if (TrimEquals(args[1], HelpCommand))
3655
{
37-
Console.Error.WriteLine("Method not found , please check \"list\" command.");
56+
methodIndex = 0;
3857
}
39-
hostBuilder.ConfigureServices(services =>
58+
59+
if (methodIndex != -1)
4060
{
41-
services.AddOptions<ConsoleLifetimeOptions>().Configure(x => x.SuppressStatusMessages = true);
42-
services.AddSingleton<IHostedService, EmptyHostedService>();
43-
});
44-
return hostBuilder;
61+
var (t, mi) = GetTypeFromAssemblies(args[1], null);
62+
if (mi != null)
63+
{
64+
Console.Write(new CommandHelpBuilder().BuildHelpMessage(mi, showCommandName: true));
65+
}
66+
else
67+
{
68+
Console.Error.WriteLine("Method not found, please check \"help\" command.");
69+
}
70+
ConfigureEmptyService();
71+
return hostBuilder;
72+
}
4573
}
4674

4775
Type? type = null;
4876
MethodInfo? methodInfo = null;
4977
if (args.Length >= 1)
5078
{
51-
(type, methodInfo) = GetTypeFromAssemblies(args[0]);
79+
(type, methodInfo) = GetTypeFromAssemblies(args[0], null);
5280
}
5381

5482
hostBuilder = hostBuilder
@@ -85,23 +113,29 @@ public static Task RunConsoleAppEngineAsync(this IHostBuilder hostBuilder, strin
85113
public static IHostBuilder UseConsoleAppEngine<T>(this IHostBuilder hostBuilder, string[] args, IConsoleAppInterceptor? interceptor = null)
86114
where T : ConsoleAppBase
87115
{
88-
var method = typeof(T).GetMethods(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly);
89-
var defaultMethod = method.FirstOrDefault(x => x.GetCustomAttribute<CommandAttribute>() == null);
90-
var hasHelp = method.Any(x => x.GetCustomAttribute<CommandAttribute>()?.EqualsAny(HelpCommand) ?? false);
116+
IHostBuilder ConfigureEmptyService()
117+
{
118+
hostBuilder.ConfigureServices(services =>
119+
{
120+
services.AddOptions<ConsoleLifetimeOptions>().Configure(x => x.SuppressStatusMessages = true);
121+
services.AddSingleton<IHostedService, EmptyHostedService>();
122+
});
123+
return hostBuilder;
124+
}
125+
126+
var methods = typeof(T).GetMethods(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly);
127+
var defaultMethod = methods.FirstOrDefault(x => x.GetCustomAttribute<CommandAttribute>() == null);
128+
var hasHelp = methods.Any(x => x.GetCustomAttribute<CommandAttribute>()?.EqualsAny(HelpCommand) ?? false);
129+
var hasVersion = methods.Any(x => x.GetCustomAttribute<CommandAttribute>()?.EqualsAny(VersionCommand) ?? false);
91130

92131
if (args.Length == 0)
93132
{
94133
if (defaultMethod == null || (defaultMethod.GetParameters().Length != 0 && !defaultMethod.GetParameters().All(x => x.HasDefaultValue)))
95134
{
96135
if (!hasHelp)
97136
{
98-
Console.Write(new CommandHelpBuilder().BuildHelpMessage(method, defaultMethod));
99-
100-
hostBuilder.ConfigureServices(services =>
101-
{
102-
services.AddOptions<ConsoleLifetimeOptions>().Configure(x => x.SuppressStatusMessages = true);
103-
services.AddSingleton<IHostedService, EmptyHostedService>();
104-
});
137+
Console.Write(new CommandHelpBuilder().BuildHelpMessage(methods, defaultMethod));
138+
ConfigureEmptyService();
105139
return hostBuilder;
106140
}
107141
else
@@ -112,18 +146,47 @@ public static IHostBuilder UseConsoleAppEngine<T>(this IHostBuilder hostBuilder,
112146
}
113147
}
114148

115-
if (!hasHelp && args.Length == 1 && args[0].Trim('-').Equals(HelpCommand, StringComparison.OrdinalIgnoreCase))
149+
if (!hasHelp && args.Length == 1 && TrimEquals(args[0], HelpCommand))
116150
{
117-
Console.Write(new CommandHelpBuilder().BuildHelpMessage(method, defaultMethod));
151+
Console.Write(new CommandHelpBuilder().BuildHelpMessage(methods, defaultMethod));
152+
ConfigureEmptyService();
153+
return hostBuilder;
154+
}
118155

119-
hostBuilder.ConfigureServices(services =>
120-
{
121-
services.AddOptions<ConsoleLifetimeOptions>().Configure(x => x.SuppressStatusMessages = true);
122-
services.AddSingleton<IHostedService, EmptyHostedService>();
123-
});
156+
if (args.Length == 1 && TrimEquals(args[0], VersionCommand))
157+
{
158+
ShowVersion();
159+
ConfigureEmptyService();
124160
return hostBuilder;
125161
}
126162

163+
if (args.Length == 2 && methods.Length != 1)
164+
{
165+
int methodIndex = -1;
166+
167+
// help command
168+
if (TrimEquals(args[0], HelpCommand))
169+
{
170+
methodIndex = 1;
171+
}
172+
// command -help
173+
else if (TrimEquals(args[1], HelpCommand))
174+
{
175+
methodIndex = 0;
176+
}
177+
178+
if (methodIndex != -1)
179+
{
180+
var (_, mi) = GetTypeFromAssemblies(args[methodIndex], typeof(T));
181+
if (mi != null)
182+
{
183+
Console.Write(new CommandHelpBuilder().BuildHelpMessage(mi, showCommandName: true));
184+
ConfigureEmptyService();
185+
return hostBuilder;
186+
}
187+
}
188+
}
189+
127190
hostBuilder = hostBuilder.ConfigureServices(services =>
128191
{
129192
services.AddOptions<ConsoleLifetimeOptions>().Configure(x => x.SuppressStatusMessages = true);
@@ -143,6 +206,31 @@ public static Task RunConsoleAppEngineAsync<T>(this IHostBuilder hostBuilder, st
143206
return UseConsoleAppEngine<T>(hostBuilder, args, interceptor).Build().RunAsync();
144207
}
145208

209+
static bool TrimEquals(string arg, string command)
210+
{
211+
return arg.Trim('-').Equals(command, StringComparison.OrdinalIgnoreCase);
212+
}
213+
214+
static void ShowVersion()
215+
{
216+
var asm = Assembly.GetEntryAssembly();
217+
var version = "1.0.0";
218+
var infoVersion = asm.GetCustomAttribute<AssemblyInformationalVersionAttribute>();
219+
if (infoVersion != null)
220+
{
221+
version = infoVersion.InformationalVersion;
222+
}
223+
else
224+
{
225+
var asmVersion = asm.GetCustomAttribute<AssemblyVersionAttribute>();
226+
if (asmVersion != null)
227+
{
228+
version = asmVersion.Version;
229+
}
230+
}
231+
Console.WriteLine(version);
232+
}
233+
146234
static void ShowMethodList()
147235
{
148236
Console.Write(new CommandHelpBuilder().BuildHelpMessage(GetConsoleAppTypes()));
@@ -178,9 +266,12 @@ static List<Type> GetConsoleAppTypes()
178266
return consoleAppBaseTypes;
179267
}
180268

181-
static (Type?, MethodInfo?) GetTypeFromAssemblies(string arg0)
269+
static (Type?, MethodInfo?) GetTypeFromAssemblies(string arg0, Type? defaultBaseType)
182270
{
183-
var consoleAppBaseTypes = GetConsoleAppTypes();
271+
var consoleAppBaseTypes = (defaultBaseType == null)
272+
? GetConsoleAppTypes()
273+
: new List<Type> { defaultBaseType };
274+
184275
if (consoleAppBaseTypes == null)
185276
{
186277
return (null, null);
@@ -195,7 +286,7 @@ static List<Type> GetConsoleAppTypes()
195286
foreach (var (method, cmdattr) in baseType.GetMethods().
196287
Select(m => (MethodInfo: m, Attr: m.GetCustomAttribute<CommandAttribute>())).Where(x => x.Attr != null))
197288
{
198-
if (cmdattr.CommandNames.Any(x => arg0.Equals(x, StringComparison.OrdinalIgnoreCase)))
289+
if (cmdattr.CommandNames.Any(x => TrimEquals(arg0, x)))
199290
{
200291
if (foundType != null && foundMethod != null)
201292
{

0 commit comments

Comments
 (0)