Skip to content

Commit 7dd09a7

Browse files
committed
fix #862
1 parent 6c33204 commit 7dd09a7

File tree

4 files changed

+66
-7
lines changed

4 files changed

+66
-7
lines changed

src/System.CommandLine.Tests/ArgumentTests.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
33

44
using System.Collections.Generic;
5-
using System.CommandLine.Builder;
6-
using System.CommandLine.Help;
75
using System.CommandLine.Invocation;
86
using System.CommandLine.Parsing;
97
using System.IO;

src/System.CommandLine.Tests/Binding/TypeConversionTests.cs

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using FluentAssertions;
88
using System.Linq;
99
using Xunit;
10+
using System.CommandLine.Invocation;
1011

1112
namespace System.CommandLine.Tests.Binding
1213
{
@@ -736,6 +737,55 @@ public void Values_can_be_correctly_converted_to_array_of_int_without_the_parser
736737
value.Should().BeEquivalentTo(1, 2, 3);
737738
}
738739

740+
[Theory]
741+
[InlineData(0, int.MaxValue, typeof(string[]))]
742+
[InlineData(0, 3, typeof(string[]))]
743+
[InlineData(0, int.MaxValue, typeof(IEnumerable<string>))]
744+
[InlineData(0, 3, typeof(IEnumerable<string>))]
745+
[InlineData(0, int.MaxValue, typeof(List<string>))]
746+
[InlineData(0, 3, typeof(List<string>))]
747+
[InlineData(0, int.MaxValue, typeof(IList<string>))]
748+
[InlineData(0, 3, typeof(IList<string>))]
749+
[InlineData(0, int.MaxValue, typeof(ICollection<string>))]
750+
[InlineData(0, 3, typeof(ICollection<string>))]
751+
752+
[InlineData(1, int.MaxValue, typeof(string[]))]
753+
[InlineData(1, 3, typeof(string[]))]
754+
[InlineData(1, int.MaxValue, typeof(IEnumerable<string>))]
755+
[InlineData(1, 3, typeof(IEnumerable<string>))]
756+
[InlineData(1, int.MaxValue, typeof(List<string>))]
757+
[InlineData(1, 3, typeof(List<string>))]
758+
[InlineData(1, int.MaxValue, typeof(IList<string>))]
759+
[InlineData(1, 3, typeof(IList<string>))]
760+
[InlineData(1, int.MaxValue, typeof(ICollection<string>))]
761+
[InlineData(1, 3, typeof(ICollection<string>))]
762+
public void Max_arity_greater_than_1_converts_to_enumerable_types(
763+
int minArity,
764+
int maxArity,
765+
Type argumentType)
766+
{
767+
var argument = new Argument
768+
{
769+
ArgumentType = argumentType,
770+
Arity = new ArgumentArity(minArity, maxArity)
771+
};
772+
773+
var option = new Option("--items")
774+
{
775+
Argument = argument
776+
};
777+
778+
var command = new RootCommand
779+
{
780+
option
781+
};
782+
783+
var result = command.Parse("--items one two three");
784+
785+
result.Errors.Should().BeEmpty();
786+
result.FindResultFor(option).GetValueOrDefault().Should().BeAssignableTo(argumentType);
787+
}
788+
739789
[Fact]
740790
public void Values_can_be_correctly_converted_to_List_of_int_without_the_parser_specifying_a_custom_converter()
741791
{

src/System.CommandLine/Binding/ArgumentConverter.cs

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,11 @@ public static ArgumentConversionResult ConvertStrings(
177177

178178
private static Type GetItemTypeIfEnumerable(Type type)
179179
{
180+
if (type.IsArray)
181+
{
182+
return type.GetElementType();
183+
}
184+
180185
var enumerableInterface =
181186
IsEnumerable(type)
182187
? type
@@ -192,10 +197,18 @@ private static Type GetItemTypeIfEnumerable(Type type)
192197
return enumerableInterface.GenericTypeArguments[0];
193198
}
194199

195-
internal static bool IsEnumerable(this Type i)
200+
internal static bool IsEnumerable(this Type type)
196201
{
197-
return i.IsGenericType &&
198-
i.GetGenericTypeDefinition() == typeof(IEnumerable<>);
202+
if (type == typeof(string))
203+
{
204+
return false;
205+
}
206+
207+
return
208+
type.IsArray
209+
||
210+
(type.IsGenericType &&
211+
type.GetGenericTypeDefinition() == typeof(IEnumerable<>));
199212
}
200213

201214
private static bool HasStringTypeConverter(this Type type)

src/System.CommandLine/Builder/CommandLineBuilderExtensions.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -296,8 +296,6 @@ internal static CommandLineBuilder UseHelp(
296296
await next(context);
297297
}
298298
}, MiddlewareOrderInternal.HelpOption);
299-
300-
return builder;
301299
}
302300

303301
return builder;

0 commit comments

Comments
 (0)