Skip to content

Commit

Permalink
Merge pull request #8 from EAVFW/tst/option-list
Browse files Browse the repository at this point in the history
feat: Add AllowMultipleArgumentsPerToken to true for IEnumerable options
  • Loading branch information
thygesteffensen authored Nov 29, 2023
2 parents 8cb42b1 + 3838c05 commit ca5a015
Showing 1 changed file with 14 additions and 7 deletions.
21 changes: 14 additions & 7 deletions src/EAVFW.Extensions.CommandLine/Extensions.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Microsoft.Extensions.DependencyInjection;
using System.Collections;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using System.Collections.Generic;
using System.CommandLine.Invocation;
Expand Down Expand Up @@ -58,21 +59,21 @@ public static Dictionary<string, Argument> AddArguments(this Command command)
typeof(COmmandExtensions).GetMethod(nameof(CreateArgument), 1, new[] { typeof(string) })
?.MakeGenericMethod(prop.PropertyType).Invoke(null,
new object[]
{
prop.GetCustomAttribute<DescriptionAttribute>()?.Description
});
{
prop.GetCustomAttribute<DescriptionAttribute>()?.Description
});


if (argument is not Argument a) continue;

output.Add(prop.Name, a);
command.Add(a);
}
}

return output;
}

public static Dictionary<string, Option> AddOptions(this Command command)
{
var o = new Dictionary<string, Option>();
Expand Down Expand Up @@ -105,6 +106,11 @@ public static Dictionary<string, Option> AddOptions(this Command command)
op.AddAlias(a.Alias);
}

if (typeof(IEnumerable).IsAssignableFrom(prop.PropertyType))
{
op.AllowMultipleArgumentsPerToken = true;
}

o[prop.Name] = op;

command.Add(op);
Expand Down Expand Up @@ -139,6 +145,7 @@ Task<int> Run(ParseResult parsed, IConsole console)
{
cmd.GetType().GetProperty(o.Key).SetValue(cmd, parsed.GetValueForOption(o.Value));
}

foreach (var (key, argument) in arguments)
{
cmd.GetType().GetProperty(key)!.SetValue(cmd, parsed.GetValueForArgument(argument));
Expand Down Expand Up @@ -219,4 +226,4 @@ public static async Task<int> RunConsoleApp<TApp>(this IHost host) where TApp :
return app.Result;
}
}
}
}

0 comments on commit ca5a015

Please sign in to comment.