diff --git a/src/ConsoleAppFramework/CommandHelpBuilder.cs b/src/ConsoleAppFramework/CommandHelpBuilder.cs index 667c3093..9329b6e0 100644 --- a/src/ConsoleAppFramework/CommandHelpBuilder.cs +++ b/src/ConsoleAppFramework/CommandHelpBuilder.cs @@ -305,7 +305,7 @@ internal CommandHelpDefinition CreateCommandHelpDefinition(CommandDescriptor des var isFlag = item.ParameterType == typeof(bool); var defaultValue = default(string); - if (item.HasDefaultValue) + if (item.HasDefaultValue()) { if (option?.DefaultValue != null) { @@ -313,16 +313,16 @@ internal CommandHelpDefinition CreateCommandHelpDefinition(CommandDescriptor des } else { - defaultValue = (item.DefaultValue?.ToString() ?? "null"); + defaultValue = (item.DefaultValue()?.ToString() ?? "null"); } if (isFlag) { - if (item.DefaultValue is true) + if (item.DefaultValue() is true) { // bool option with true default value is not flag. isFlag = false; } - else if (item.DefaultValue is false) + else if (item.DefaultValue() is false) { // false default value should be omitted for flag. defaultValue = null; diff --git a/src/ConsoleAppFramework/ConsoleAppEngine.cs b/src/ConsoleAppFramework/ConsoleAppEngine.cs index 0f6b6471..956fe57e 100644 --- a/src/ConsoleAppFramework/ConsoleAppEngine.cs +++ b/src/ConsoleAppFramework/ConsoleAppEngine.cs @@ -106,7 +106,7 @@ public async Task RunAsync() if (commandDescriptor.CommandType == CommandType.DefaultCommand && args.Length == 0) { var p = commandDescriptor.MethodInfo.GetParameters(); - if (p.Any(x => !(x.ParameterType == typeof(ConsoleAppContext) || isService.IsService(x.ParameterType) || x.HasDefaultValue))) + if (p.Any(x => !(x.ParameterType == typeof(ConsoleAppContext) || isService.IsService(x.ParameterType) || x.HasDefaultValue()))) { options.CommandDescriptors.TryGetHelpMethod(out commandDescriptor); } @@ -294,7 +294,7 @@ bool TryGetInvokeArguments(ParameterInfo[] parameters, string?[] args, int argsO { if (optionByIndex.Count <= option.Index) { - if (!item.HasDefaultValue) + if (!item.HasDefaultValue()) { throw new InvalidOperationException($"Required argument {option.Index} was not found in specified arguments."); } @@ -346,13 +346,20 @@ bool TryGetInvokeArguments(ParameterInfo[] parameters, string?[] args, int argsO var elemType = UnwrapCollectionElementType(parameters[i].ParameterType); if (elemType == typeof(string)) { - if (!(v.StartsWith("\"") && v.EndsWith("\""))) - { - v = "[" + string.Join(",", v.Split(' ', ',').Select(x => "\"" + x + "\"")) + "]"; + if (parameters.Length == i + 1) + { + v = "[" + string.Join(",", optionByIndex.Skip(parameters[i].Position).Select(x => "\"" + x.Value + "\"")) + "]"; } else - { - v = "[" + v + "]"; + { + if (!(v.StartsWith("\"") && v.EndsWith("\""))) + { + v = "[" + string.Join(",", v.Split(' ', ',').Select(x => "\"" + x + "\"")) + "]"; + } + else + { + v = "[" + v + "]"; + } } } else @@ -401,9 +408,9 @@ bool TryGetInvokeArguments(ParameterInfo[] parameters, string?[] args, int argsO } } - if (item.HasDefaultValue) + if (item.HasDefaultValue()) { - invokeArgs[i] = item.DefaultValue; + invokeArgs[i] = item.DefaultValue(); } else if (item.ParameterType == typeof(bool)) { diff --git a/src/ConsoleAppFramework/ParameterInfoExtensions.cs b/src/ConsoleAppFramework/ParameterInfoExtensions.cs new file mode 100644 index 00000000..9dbbb402 --- /dev/null +++ b/src/ConsoleAppFramework/ParameterInfoExtensions.cs @@ -0,0 +1,18 @@ +using System; +using System.Linq; +using System.Reflection; + +namespace ConsoleAppFramework +{ + public static class ParameterInfoExtensions + { + public static bool HasDefaultValue(this ParameterInfo pi) + => pi.HasDefaultValue + || pi.CustomAttributes.Any(a => a.AttributeType == typeof(ParamArrayAttribute)); + + public static object? DefaultValue(this ParameterInfo pi) + => pi.HasDefaultValue + ? pi.DefaultValue + : Array.CreateInstance(pi.ParameterType.GetElementType()!, 0); + } +}