Skip to content

Commit 49e11f7

Browse files
committed
trim '-' for match default "help", "list" command
1 parent 4779852 commit 49e11f7

File tree

2 files changed

+16
-28
lines changed

2 files changed

+16
-28
lines changed

sandbox/SingleContainedApp/Program.cs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -102,15 +102,15 @@ public class OverrideCheck : ConsoleAppBase
102102
[Command(new[] { "unescape", "-h" }, "unescape base64url to base64")]
103103
public void Unescape([Option(0)]string input) => Console.WriteLine((input));
104104

105-
[Command(new[] { "help", "-h", "-help", "--help" }, "show help")]
106-
public void Help()
107-
{
108-
Console.WriteLine("Usage: base64urls [-version] [-help] [decode|encode|escape|unescape] [args]");
109-
Console.WriteLine("E.g., run this: base64urls decode QyMgaXMgYXdlc29tZQ==");
110-
Console.WriteLine("E.g., run this: base64urls encode \"C# is awesome.\"");
111-
Console.WriteLine("E.g., run this: base64urls escape \"This+is/goingto+escape==\"");
112-
Console.WriteLine("E.g., run this: base64urls unescape \"This-is_goingto-escape\"");
113-
}
105+
//[Command(new[] { "help", "-h", "-help", "--help" }, "show help")]
106+
//public void Help()
107+
//{
108+
// Console.WriteLine("Usage: base64urls [-version] [-help] [decode|encode|escape|unescape] [args]");
109+
// Console.WriteLine("E.g., run this: base64urls decode QyMgaXMgYXdlc29tZQ==");
110+
// Console.WriteLine("E.g., run this: base64urls encode \"C# is awesome.\"");
111+
// Console.WriteLine("E.g., run this: base64urls escape \"This+is/goingto+escape==\"");
112+
// Console.WriteLine("E.g., run this: base64urls unescape \"This-is_goingto-escape\"");
113+
//}
114114
}
115115

116116
public class ComplexArgTest : ConsoleAppBase
@@ -132,15 +132,15 @@ class Program
132132
{
133133
static async Task Main(string[] args)
134134
{
135-
args = new[] { "-array", "10,20,30", "-person", @"{""Age"":10,""Name"":""foo""}" };
135+
// args = new[] { "-array", "10,20,30", "-person", @"{""Age"":10,""Name"":""foo""}" };
136136

137137
await ConsoleAppHost.CreateDefaultBuilder()
138138
.ConfigureServices((hostContext, services) =>
139139
{
140140
// mapping config json to IOption<MyConfig>
141141
services.Configure<MyConfig>(hostContext.Configuration);
142142
})
143-
.RunConsoleAppEngineAsync<ComplexArgTest>(args);
143+
.RunConsoleAppEngineAsync<OverrideCheck>(args);
144144
}
145145
}
146146
}

src/ConsoleAppFramework/ConsoleAppEngineHostBuilderExtensions.cs

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public static class ConsoleAppEngineHostBuilderExtensions
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].Equals(ListCommand, StringComparison.OrdinalIgnoreCase)))
18+
if (args.Length == 0 || (args.Length == 1 && args[0].Trim('-').Equals(ListCommand, StringComparison.OrdinalIgnoreCase)))
1919
{
2020
ShowMethodList();
2121
hostBuilder.ConfigureServices(services =>
@@ -25,7 +25,7 @@ public static IHostBuilder UseConsoleAppEngine(this IHostBuilder hostBuilder, st
2525
});
2626
return hostBuilder;
2727
}
28-
if (args.Length == 2 && args[0].Equals(HelpCommand, StringComparison.OrdinalIgnoreCase))
28+
if (args.Length == 2 && args[0].Trim('-').Equals(HelpCommand, StringComparison.OrdinalIgnoreCase))
2929
{
3030
var (t, mi) = GetTypeFromAssemblies(args[1]);
3131
if (mi != null)
@@ -87,7 +87,6 @@ public static IHostBuilder UseConsoleAppEngine<T>(this IHostBuilder hostBuilder,
8787
{
8888
var method = typeof(T).GetMethods(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly);
8989
var defaultMethod = method.FirstOrDefault(x => x.GetCustomAttribute<CommandAttribute>() == null);
90-
var hasList = method.Any(x => x.GetCustomAttribute<CommandAttribute>()?.EqualsAny(ListCommand) ?? false);
9190
var hasHelp = method.Any(x => x.GetCustomAttribute<CommandAttribute>()?.EqualsAny(HelpCommand) ?? false);
9291

9392
if (args.Length == 0)
@@ -113,18 +112,7 @@ public static IHostBuilder UseConsoleAppEngine<T>(this IHostBuilder hostBuilder,
113112
}
114113
}
115114

116-
if (!hasList && args.Length == 1 && args[0].Equals(ListCommand, StringComparison.OrdinalIgnoreCase))
117-
{
118-
ShowMethodList();
119-
hostBuilder.ConfigureServices(services =>
120-
{
121-
services.AddOptions<ConsoleLifetimeOptions>().Configure(x => x.SuppressStatusMessages = true);
122-
services.AddSingleton<IHostedService, EmptyHostedService>();
123-
});
124-
return hostBuilder;
125-
}
126-
127-
if (!hasHelp && args.Length == 1 && args[0].Equals(HelpCommand, StringComparison.OrdinalIgnoreCase))
115+
if (!hasHelp && args.Length == 1 && args[0].Trim('-').Equals(HelpCommand, StringComparison.OrdinalIgnoreCase))
128116
{
129117
Console.Write(new CommandHelpBuilder().BuildHelpMessage(method, defaultMethod));
130118

@@ -209,7 +197,7 @@ static List<Type> GetConsoleAppTypes()
209197
{
210198
if (cmdattr.CommandNames.Any(x => arg0.Equals(x, StringComparison.OrdinalIgnoreCase)))
211199
{
212-
if(foundType != null && foundMethod != null)
200+
if (foundType != null && foundMethod != null)
213201
{
214202
throw new InvalidOperationException($"Duplicate ConsoleApp Command name is not allowed, {foundType.FullName}.{foundMethod.Name} and {baseType.FullName}.{method.Name}");
215203
}
@@ -231,7 +219,7 @@ static List<Type> GetConsoleAppTypes()
231219
}
232220
}
233221
}
234-
if(foundType != null && foundMethod != null)
222+
if (foundType != null && foundMethod != null)
235223
{
236224
return (foundType, foundMethod);
237225
}

0 commit comments

Comments
 (0)