Skip to content

Commit 3a2afae

Browse files
committed
fix: update attribute conventions to use TryParse instead of Values.Add
1 parent 6cd1723 commit 3a2afae

File tree

3 files changed

+16
-13
lines changed

3 files changed

+16
-13
lines changed

src/CommandLineUtils/Conventions/ArgumentAttributeConvention.cs

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -121,10 +121,13 @@ private void AddArgument(PropertyInfo prop,
121121
{
122122
if (!ReflectionHelper.IsSpecialValueTupleType(prop.PropertyType, out _))
123123
{
124-
if (getter.Invoke(cmd.GetModel()) is IEnumerable<object> value)
124+
if (getter.Invoke(cmd.GetModel()) is IEnumerable<object> values)
125125
{
126-
argument.Values.AddRange(value.Select(x => x?.ToString()));
127-
argument.DefaultValue = string.Join(", ", value.Select(x => x?.ToString()));
126+
foreach (var value in values)
127+
{
128+
argument.TryParse(value?.ToString());
129+
}
130+
argument.DefaultValue = string.Join(", ", values.Select(x => x?.ToString()));
128131
}
129132
}
130133
}
@@ -145,11 +148,6 @@ private void AddArgument(PropertyInfo prop,
145148
throw new InvalidOperationException(Strings.CannotDetermineParserType(prop));
146149
}
147150

148-
if (argument.Values.Count == 0)
149-
{
150-
return;
151-
}
152-
153151
if (r.SelectedCommand is IModelAccessor cmd)
154152
{
155153
if (argument.Values.Count == 0)
@@ -159,7 +157,7 @@ private void AddArgument(PropertyInfo prop,
159157
var value = getter.Invoke(cmd.GetModel());
160158
if (value != null)
161159
{
162-
argument.Values.Add(value.ToString());
160+
argument.TryParse(value.ToString());
163161
argument.DefaultValue = value.ToString();
164162
}
165163
}

src/CommandLineUtils/Conventions/OptionAttributeConventionBase.cs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ private protected void AddOption(ConventionContext context, CommandOption option
7070
var collectionParser =
7171
CollectionParserProvider.Default.GetParser(prop.PropertyType,
7272
context.Application.ValueParsers);
73+
7374
if (collectionParser == null)
7475
{
7576
throw new InvalidOperationException(Strings.CannotDetermineParserType(prop));
@@ -79,10 +80,13 @@ private protected void AddOption(ConventionContext context, CommandOption option
7980
{
8081
if (!ReflectionHelper.IsSpecialValueTupleType(prop.PropertyType, out var type))
8182
{
82-
if (getter.Invoke(modelAccessor.GetModel()) is IEnumerable<object> value)
83+
if (getter.Invoke(modelAccessor.GetModel()) is IEnumerable<object> values)
8384
{
84-
option.Values.AddRange(value.Select(x => x?.ToString()));
85-
option.DefaultValue = string.Join(", ", value.Select(x => x?.ToString()));
85+
foreach (var value in values)
86+
{
87+
option.TryParse(value?.ToString());
88+
}
89+
option.DefaultValue = string.Join(", ", values.Select(x => x?.ToString()));
8690
}
8791
}
8892
}
@@ -110,7 +114,7 @@ private protected void AddOption(ConventionContext context, CommandOption option
110114

111115
if (value != null)
112116
{
113-
option.Values.Add(value.ToString());
117+
option.TryParse(value.ToString());
114118
option.DefaultValue = value.ToString();
115119
}
116120
}

test/CommandLineUtils.Tests/DefaultHelpTextGeneratorTests.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,7 @@ public void ShowHelpFromAttributes()
174174
var app = new CommandLineApplication<MyApp>() { Name = "test" };
175175
app.Conventions.UseDefaultConventions();
176176
var helpText = GetHelpText(app);
177+
_output.WriteLine(helpText);
177178

178179
Assert.Equal(@"Usage: test [options] <SomeStringArgument> <RestrictedStringArgument> <DefaultValStringArgument> <SomeEnumArgument> <RestrictedEnumArgument> <SomeNullableEnumArgument>
179180

0 commit comments

Comments
 (0)