Skip to content

Commit ca5a015

Browse files
Merge pull request #8 from EAVFW/tst/option-list
feat: Add AllowMultipleArgumentsPerToken to true for IEnumerable options
2 parents 8cb42b1 + 3838c05 commit ca5a015

File tree

1 file changed

+14
-7
lines changed

1 file changed

+14
-7
lines changed

src/EAVFW.Extensions.CommandLine/Extensions.cs

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using Microsoft.Extensions.DependencyInjection;
1+
using System.Collections;
2+
using Microsoft.Extensions.DependencyInjection;
23
using Microsoft.Extensions.Hosting;
34
using System.Collections.Generic;
45
using System.CommandLine.Invocation;
@@ -58,21 +59,21 @@ public static Dictionary<string, Argument> AddArguments(this Command command)
5859
typeof(COmmandExtensions).GetMethod(nameof(CreateArgument), 1, new[] { typeof(string) })
5960
?.MakeGenericMethod(prop.PropertyType).Invoke(null,
6061
new object[]
61-
{
62-
prop.GetCustomAttribute<DescriptionAttribute>()?.Description
63-
});
62+
{
63+
prop.GetCustomAttribute<DescriptionAttribute>()?.Description
64+
});
6465

6566

6667
if (argument is not Argument a) continue;
67-
68+
6869
output.Add(prop.Name, a);
6970
command.Add(a);
7071
}
7172
}
7273

7374
return output;
7475
}
75-
76+
7677
public static Dictionary<string, Option> AddOptions(this Command command)
7778
{
7879
var o = new Dictionary<string, Option>();
@@ -105,6 +106,11 @@ public static Dictionary<string, Option> AddOptions(this Command command)
105106
op.AddAlias(a.Alias);
106107
}
107108

109+
if (typeof(IEnumerable).IsAssignableFrom(prop.PropertyType))
110+
{
111+
op.AllowMultipleArgumentsPerToken = true;
112+
}
113+
108114
o[prop.Name] = op;
109115

110116
command.Add(op);
@@ -139,6 +145,7 @@ Task<int> Run(ParseResult parsed, IConsole console)
139145
{
140146
cmd.GetType().GetProperty(o.Key).SetValue(cmd, parsed.GetValueForOption(o.Value));
141147
}
148+
142149
foreach (var (key, argument) in arguments)
143150
{
144151
cmd.GetType().GetProperty(key)!.SetValue(cmd, parsed.GetValueForArgument(argument));
@@ -219,4 +226,4 @@ public static async Task<int> RunConsoleApp<TApp>(this IHost host) where TApp :
219226
return app.Result;
220227
}
221228
}
222-
}
229+
}

0 commit comments

Comments
 (0)