Skip to content

Commit 4a19ccb

Browse files
authored
Merge pull request #90 from yatagarasu25/params_arguments_patch
Enable (param string[] something) to accept arbitrary number of parameters.
2 parents 9bb46be + 0f6c5b9 commit 4a19ccb

File tree

3 files changed

+38
-13
lines changed

3 files changed

+38
-13
lines changed

src/ConsoleAppFramework/CommandHelpBuilder.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -305,24 +305,24 @@ internal CommandHelpDefinition CreateCommandHelpDefinition(CommandDescriptor des
305305
var isFlag = item.ParameterType == typeof(bool);
306306

307307
var defaultValue = default(string);
308-
if (item.HasDefaultValue)
308+
if (item.HasDefaultValue())
309309
{
310310
if (option?.DefaultValue != null)
311311
{
312312
defaultValue = option.DefaultValue;
313313
}
314314
else
315315
{
316-
defaultValue = (item.DefaultValue?.ToString() ?? "null");
316+
defaultValue = (item.DefaultValue()?.ToString() ?? "null");
317317
}
318318
if (isFlag)
319319
{
320-
if (item.DefaultValue is true)
320+
if (item.DefaultValue() is true)
321321
{
322322
// bool option with true default value is not flag.
323323
isFlag = false;
324324
}
325-
else if (item.DefaultValue is false)
325+
else if (item.DefaultValue() is false)
326326
{
327327
// false default value should be omitted for flag.
328328
defaultValue = null;

src/ConsoleAppFramework/ConsoleAppEngine.cs

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ public async Task RunAsync()
106106
if (commandDescriptor.CommandType == CommandType.DefaultCommand && args.Length == 0)
107107
{
108108
var p = commandDescriptor.MethodInfo.GetParameters();
109-
if (p.Any(x => !(x.ParameterType == typeof(ConsoleAppContext) || isService.IsService(x.ParameterType) || x.HasDefaultValue)))
109+
if (p.Any(x => !(x.ParameterType == typeof(ConsoleAppContext) || isService.IsService(x.ParameterType) || x.HasDefaultValue())))
110110
{
111111
options.CommandDescriptors.TryGetHelpMethod(out commandDescriptor);
112112
}
@@ -294,7 +294,7 @@ bool TryGetInvokeArguments(ParameterInfo[] parameters, string?[] args, int argsO
294294
{
295295
if (optionByIndex.Count <= option.Index)
296296
{
297-
if (!item.HasDefaultValue)
297+
if (!item.HasDefaultValue())
298298
{
299299
throw new InvalidOperationException($"Required argument {option.Index} was not found in specified arguments.");
300300
}
@@ -346,13 +346,20 @@ bool TryGetInvokeArguments(ParameterInfo[] parameters, string?[] args, int argsO
346346
var elemType = UnwrapCollectionElementType(parameters[i].ParameterType);
347347
if (elemType == typeof(string))
348348
{
349-
if (!(v.StartsWith("\"") && v.EndsWith("\"")))
350-
{
351-
v = "[" + string.Join(",", v.Split(' ', ',').Select(x => "\"" + x + "\"")) + "]";
349+
if (parameters.Length == i + 1)
350+
{
351+
v = "[" + string.Join(",", optionByIndex.Skip(parameters[i].Position).Select(x => "\"" + x.Value + "\"")) + "]";
352352
}
353353
else
354-
{
355-
v = "[" + v + "]";
354+
{
355+
if (!(v.StartsWith("\"") && v.EndsWith("\"")))
356+
{
357+
v = "[" + string.Join(",", v.Split(' ', ',').Select(x => "\"" + x + "\"")) + "]";
358+
}
359+
else
360+
{
361+
v = "[" + v + "]";
362+
}
356363
}
357364
}
358365
else
@@ -401,9 +408,9 @@ bool TryGetInvokeArguments(ParameterInfo[] parameters, string?[] args, int argsO
401408
}
402409
}
403410

404-
if (item.HasDefaultValue)
411+
if (item.HasDefaultValue())
405412
{
406-
invokeArgs[i] = item.DefaultValue;
413+
invokeArgs[i] = item.DefaultValue();
407414
}
408415
else if (item.ParameterType == typeof(bool))
409416
{
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
using System;
2+
using System.Linq;
3+
using System.Reflection;
4+
5+
namespace ConsoleAppFramework
6+
{
7+
public static class ParameterInfoExtensions
8+
{
9+
public static bool HasDefaultValue(this ParameterInfo pi)
10+
=> pi.HasDefaultValue
11+
|| pi.CustomAttributes.Any(a => a.AttributeType == typeof(ParamArrayAttribute));
12+
13+
public static object? DefaultValue(this ParameterInfo pi)
14+
=> pi.HasDefaultValue
15+
? pi.DefaultValue
16+
: Array.CreateInstance(pi.ParameterType.GetElementType()!, 0);
17+
}
18+
}

0 commit comments

Comments
 (0)