Skip to content

Commit 1f9483c

Browse files
authored
Command arguments should default to empty collections (#1235)
1 parent 64cc95b commit 1f9483c

File tree

2 files changed

+139
-3
lines changed

2 files changed

+139
-3
lines changed

src/System.CommandLine.Tests/CommandTests.cs

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

44
using FluentAssertions;
5+
using System.Collections.Generic;
56
using System.CommandLine.Parsing;
67
using System.Linq;
78
using Xunit;
@@ -305,7 +306,6 @@ public void When_multiple_options_are_configured_then_they_must_differ_by_name()
305306
.Be("Alias '--same' is already in use.");
306307
}
307308

308-
309309
[Fact]
310310
public void When_Name_is_set_to_its_current_value_then_it_is_not_removed_from_aliases()
311311
{
@@ -318,6 +318,142 @@ public void When_Name_is_set_to_its_current_value_then_it_is_not_removed_from_al
318318
command.Aliases.Should().Contain("name");
319319
}
320320

321+
[Fact]
322+
public void Command_argument_of_string_defaults_to_empty_when_not_specified()
323+
{
324+
var argument = new Argument<string>();
325+
var command = new Command("mycommand")
326+
{
327+
argument
328+
};
329+
330+
var result = command.Parse("mycommand");
331+
result.ValueForArgument(argument)
332+
.Should()
333+
.BeEmpty();
334+
}
335+
336+
[Fact]
337+
public void Command_argument_of_IEnumerable_of_T_defaults_to_empty_when_not_specified()
338+
{
339+
var argument = new Argument<IEnumerable<string>>();
340+
var command = new Command("mycommand")
341+
{
342+
argument
343+
};
344+
345+
var result = command.Parse("mycommand");
346+
result.ValueForArgument(argument)
347+
.Should()
348+
.BeEmpty();
349+
}
350+
351+
[Fact]
352+
public void Command_argument_of_Array_defaults_to_empty_when_not_specified()
353+
{
354+
var argument = new Argument<string[]>();
355+
var command = new Command("mycommand")
356+
{
357+
argument
358+
};
359+
360+
var result = command.Parse("mycommand");
361+
result.ValueForArgument(argument)
362+
.Should()
363+
.BeEmpty();
364+
}
365+
366+
[Fact]
367+
public void Command_argument_of_List_defaults_to_empty_when_not_specified()
368+
{
369+
var argument = new Argument<List<string>>();
370+
var command = new Command("mycommand")
371+
{
372+
argument
373+
};
374+
375+
var result = command.Parse("mycommand");
376+
result.ValueForArgument(argument)
377+
.Should()
378+
.BeEmpty();
379+
}
380+
381+
[Fact]
382+
public void Command_argument_of_IList_of_T_defaults_to_empty_when_not_specified()
383+
{
384+
var argument = new Argument<IList<string>>();
385+
var command = new Command("mycommand")
386+
{
387+
argument
388+
};
389+
390+
var result = command.Parse("mycommand");
391+
result.ValueForArgument(argument)
392+
.Should()
393+
.BeEmpty();
394+
}
395+
396+
[Fact]
397+
public void Command_argument_of_IList_defaults_to_empty_when_not_specified()
398+
{
399+
var argument = new Argument<System.Collections.IList>();
400+
var command = new Command("mycommand")
401+
{
402+
argument
403+
};
404+
405+
var result = command.Parse("mycommand");
406+
result.ValueForArgument(argument)
407+
.Should()
408+
.BeEmpty();
409+
}
410+
411+
[Fact]
412+
public void Command_argument_of_ICollection_defaults_to_empty_when_not_specified()
413+
{
414+
var argument = new Argument<System.Collections.ICollection>();
415+
var command = new Command("mycommand")
416+
{
417+
argument
418+
};
419+
420+
var result = command.Parse("mycommand");
421+
result.ValueForArgument(argument)
422+
.Should()
423+
.BeEmpty();
424+
}
425+
426+
[Fact]
427+
public void Command_argument_of_IEnumerable_defaults_to_empty_when_not_specified()
428+
{
429+
var argument = new Argument<System.Collections.IEnumerable>();
430+
var command = new Command("mycommand")
431+
{
432+
argument
433+
};
434+
435+
var result = command.Parse("mycommand");
436+
result.ValueForArgument(argument)
437+
.Should()
438+
.BeEmpty();
439+
}
440+
441+
[Fact]
442+
public void Command_argument_of_ICollection_of_T_defaults_to_empty_when_not_specified()
443+
{
444+
var argument = new Argument<ICollection<string>>();
445+
var command = new Command("mycommand")
446+
{
447+
argument
448+
};
449+
450+
var result = command.Parse("mycommand");
451+
result.ValueForArgument(argument)
452+
.Should()
453+
.BeEmpty();
454+
}
455+
456+
321457
protected override Symbol CreateSymbol(string name) => new Command(name);
322458
}
323459
}

src/System.CommandLine/Parsing/ParseResult.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ public T ValueForArgument<T>(Argument<T> argument)
105105
return t;
106106
}
107107

108-
return default!;
108+
return (T)Binder.GetDefaultValue(argument.ArgumentType);
109109
}
110110

111111
[return: MaybeNull]
@@ -117,7 +117,7 @@ public T ValueForArgument<T>(Argument argument)
117117
return t;
118118
}
119119

120-
return default;
120+
return (T)Binder.GetDefaultValue(argument.ArgumentType);
121121
}
122122

123123
[return: MaybeNull]

0 commit comments

Comments
 (0)