Skip to content

Commit 44cba4d

Browse files
adamsitnikjonsequitur
authored andcommitted
add CliOption.HasDefaultValue
1 parent ebb192b commit 44cba4d

File tree

3 files changed

+24
-2
lines changed

3 files changed

+24
-2
lines changed

src/System.CommandLine.ApiCompatibility.Tests/ApiCompatibilityApprovalTests.System_CommandLine_api_is_not_changed.approved.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ System.CommandLine
8585
public System.Boolean AllowMultipleArgumentsPerToken { get; set; }
8686
public ArgumentArity Arity { get; set; }
8787
public System.Collections.Generic.List<System.Func<System.CommandLine.Completions.CompletionContext,System.Collections.Generic.IEnumerable<System.CommandLine.Completions.CompletionItem>>> CompletionSources { get; }
88+
public System.Boolean HasDefaultValue { get; }
8889
public System.String HelpName { get; set; }
8990
public System.Boolean Recursive { get; set; }
9091
public System.Boolean Required { get; set; }

src/System.CommandLine.Tests/OptionTests.cs

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,30 @@
44
using System.CommandLine.Parsing;
55
using FluentAssertions;
66
using System.Linq;
7-
using System.Threading;
8-
using System.Threading.Tasks;
97
using Xunit;
108

119
namespace System.CommandLine.Tests
1210
{
1311
public partial class OptionTests
1412
{
13+
[Fact]
14+
public void By_default_there_is_no_default_value()
15+
{
16+
CliOption<string> option = new("name");
17+
18+
option.HasDefaultValue.Should().BeFalse();
19+
}
20+
21+
[Fact]
22+
public void When_default_value_factory_is_set_then_HasDefaultValue_is_true()
23+
{
24+
CliOption<string[]> option = new("name");
25+
26+
option.DefaultValueFactory = (_) => Array.Empty<string>();
27+
28+
option.HasDefaultValue.Should().BeTrue();
29+
}
30+
1531
[Fact]
1632
public void When_an_option_has_only_name_then_it_has_no_aliases()
1733
{

src/System.CommandLine/CliOption.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,11 @@ private protected CliOption(string name, string[] aliases) : base(name)
3434
/// </summary>
3535
internal abstract CliArgument Argument { get; }
3636

37+
/// <summary>
38+
/// Specifies if a default value is defined for the option.
39+
/// </summary>
40+
public bool HasDefaultValue => Argument.HasDefaultValue;
41+
3742
/// <summary>
3843
/// Gets or sets the name of the Option when displayed in help.
3944
/// </summary>

0 commit comments

Comments
 (0)