Skip to content

Commit 12473cd

Browse files
authored
fix #2622: add ArgumentResult.Implicit property (#2625)
* fix #2622: add ArgumentResult.Implicit property * some code cleanup
1 parent 0c37e9e commit 12473cd

File tree

8 files changed

+103
-72
lines changed

8 files changed

+103
-72
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
@@ -200,6 +200,7 @@ System.CommandLine.Invocation
200200
System.CommandLine.Parsing
201201
public class ArgumentResult : SymbolResult
202202
public System.CommandLine.Argument Argument { get; }
203+
public System.Boolean Implicit { get; }
203204
public System.Void AddError(System.String errorMessage)
204205
public T GetValueOrDefault<T>()
205206
public System.Void OnlyTake(System.Int32 numberOfTokens)

src/System.CommandLine.Tests/Help/HelpBuilderTests.Customization.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -258,8 +258,8 @@ public void Option_can_fallback_to_default_when_customizing(bool conditionA, boo
258258

259259
var helpBuilder = new HelpBuilder(LargeMaxWidth);
260260
helpBuilder.CustomizeSymbol(option,
261-
firstColumnText: ctx => conditionA ? "custom 1st" : HelpBuilder.Default.GetOptionUsageLabel(option),
262-
secondColumnText: ctx => conditionB ? "custom 2nd" : option.Description ?? string.Empty);
261+
firstColumnText: _ => conditionA ? "custom 1st" : HelpBuilder.Default.GetOptionUsageLabel(option),
262+
secondColumnText: _ => conditionB ? "custom 2nd" : option.Description ?? string.Empty);
263263

264264
command.Options.Add(new HelpOption
265265
{
@@ -300,9 +300,9 @@ public void Argument_can_fallback_to_default_when_customizing(
300300

301301
var helpBuilder = new HelpBuilder(LargeMaxWidth);
302302
helpBuilder.CustomizeSymbol(argument,
303-
firstColumnText: ctx => conditionA ? "custom 1st" : HelpBuilder.Default.GetArgumentUsageLabel(argument),
304-
secondColumnText: ctx => conditionB ? "custom 2nd" : HelpBuilder.Default.GetArgumentDescription(argument),
305-
defaultValue: ctx => conditionC ? "custom def" : HelpBuilder.Default.GetArgumentDefaultValue(argument));
303+
firstColumnText: _ => conditionA ? "custom 1st" : HelpBuilder.Default.GetArgumentUsageLabel(argument),
304+
secondColumnText: _ => conditionB ? "custom 2nd" : HelpBuilder.Default.GetArgumentDescription(argument),
305+
defaultValue: _ => conditionC ? "custom def" : HelpBuilder.Default.GetArgumentDefaultValue(argument));
306306

307307
command.Options.Add(new HelpOption
308308
{

src/System.CommandLine.Tests/HelpOptionTests.cs

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

44
using FluentAssertions;
5-
using Microsoft.VisualStudio.TestPlatform.Utilities;
65
using System.CommandLine.Help;
76
using System.CommandLine.Invocation;
87
using System.CommandLine.Tests.Utility;

src/System.CommandLine.Tests/OptionTests.MultipleArgumentsPerToken.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
// Copyright (c) .NET Foundation and contributors. All rights reserved.
22
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
33

4-
using System.CommandLine.Parsing;
54
using System.CommandLine.Tests.Utility;
65
using FluentAssertions;
76
using System.Linq;

src/System.CommandLine.Tests/ParseErrorReportingTests.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
using System.CommandLine.Tests.Utility;
88
using System.IO;
99
using System.Linq;
10-
using System.Threading;
1110
using System.Threading.Tasks;
1211
using Xunit;
1312

src/System.CommandLine.Tests/ParserTests.cs

Lines changed: 64 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,6 @@ namespace System.CommandLine.Tests
1515
{
1616
public partial class ParserTests
1717
{
18-
private T GetValue<T>(ParseResult parseResult, Option<T> option)
19-
=> parseResult.GetValue(option);
20-
21-
private T GetValue<T>(ParseResult parseResult, Argument<T> argument)
22-
=> parseResult.GetValue(argument);
23-
2418
[Fact]
2519
public void An_option_can_be_checked_by_object_instance()
2620
{
@@ -816,7 +810,7 @@ public void Commands_can_have_default_argument_values()
816810

817811
ParseResult result = command.Parse("command");
818812

819-
GetValue(result, argument)
813+
result.GetValue(argument)
820814
.Should()
821815
.Be("default");
822816

@@ -861,7 +855,7 @@ public void When_an_option_with_a_default_value_is_not_matched_then_the_option_c
861855
ParseResult result = command.Parse("command");
862856

863857
result.GetResult(option).Should().NotBeNull();
864-
GetValue(result, option).Should().Be("the-default");
858+
result.GetValue(option).Should().Be("the-default");
865859
}
866860

867861
[Fact]
@@ -926,6 +920,48 @@ public void When_an_argument_with_a_default_value_is_not_matched_then_there_are_
926920
.BeEmpty();
927921
}
928922

923+
[Fact]
924+
public void When_an_argument_with_a_default_value_is_matched_then_the_option_result_is_implicit()
925+
{
926+
var argument = new Argument<string>("the-arg")
927+
{
928+
DefaultValueFactory = _ => "the-default"
929+
};
930+
931+
var command = new Command("command")
932+
{
933+
argument
934+
};
935+
936+
var result = command.Parse("command the-explicit-value");
937+
938+
result.GetResult(argument)
939+
.Implicit
940+
.Should()
941+
.BeFalse();
942+
}
943+
944+
[Fact]
945+
public void When_an_argument_with_a_default_value_is_not_matched_then_the_option_result_is_implicit()
946+
{
947+
var argument = new Argument<string>("the-arg")
948+
{
949+
DefaultValueFactory = _ => "the-default"
950+
};
951+
952+
var command = new Command("command")
953+
{
954+
argument
955+
};
956+
957+
var result = command.Parse("command");
958+
959+
result.GetResult(argument)
960+
.Implicit
961+
.Should()
962+
.BeTrue();
963+
}
964+
929965
[Fact]
930966
public void Command_default_argument_value_does_not_override_parsed_value()
931967
{
@@ -941,7 +977,7 @@ public void Command_default_argument_value_does_not_override_parsed_value()
941977

942978
var result = command.Parse("the-directory");
943979

944-
GetValue(result, argument)
980+
result.GetValue(argument)
945981
.Name
946982
.Should()
947983
.Be("the-directory");
@@ -1125,7 +1161,7 @@ public void Option_arguments_can_start_with_prefixes_that_make_them_look_like_op
11251161

11261162
var result = command.Parse(input);
11271163

1128-
GetValue(result, optionX).Should().Be("-y");
1164+
result.GetValue(optionX).Should().Be("-y");
11291165
}
11301166

11311167
[Fact]
@@ -1144,9 +1180,9 @@ public void Option_arguments_can_start_with_prefixes_that_make_them_look_like_bu
11441180

11451181
var result = command.Parse("-a -bc");
11461182

1147-
GetValue(result, optionA).Should().Be("-bc");
1148-
GetValue(result, optionB).Should().BeFalse();
1149-
GetValue(result, optionC).Should().BeFalse();
1183+
result.GetValue(optionA).Should().Be("-bc");
1184+
result.GetValue(optionB).Should().BeFalse();
1185+
result.GetValue(optionC).Should().BeFalse();
11501186
}
11511187

11521188
[Fact]
@@ -1161,7 +1197,7 @@ public void Option_arguments_can_match_subcommands()
11611197

11621198
var result = root.Parse("-a subcommand");
11631199

1164-
GetValue(result, optionA).Should().Be("subcommand");
1200+
result.GetValue(optionA).Should().Be("subcommand");
11651201
result.CommandResult.Command.Should().BeSameAs(root);
11661202
}
11671203

@@ -1182,7 +1218,7 @@ public void Arguments_can_match_subcommands()
11821218

11831219
result.CommandResult.Command.Should().BeSameAs(subcommand);
11841220

1185-
GetValue(result, argument)
1221+
result.GetValue(argument)
11861222
.Should()
11871223
.BeEquivalentSequenceTo("one", "two", "three", "subcommand", "four");
11881224

@@ -1207,7 +1243,7 @@ public void Option_arguments_can_match_the_aliases_of_sibling_options_when_non_s
12071243
var result = command.Parse(input);
12081244

12091245
result.Errors.Should().BeEmpty();
1210-
GetValue(result, optionX).Should().Be("-y");
1246+
result.GetValue(optionX).Should().Be("-y");
12111247
}
12121248

12131249
[Fact]
@@ -1222,7 +1258,7 @@ public void Single_option_arguments_that_match_option_aliases_are_parsed_correct
12221258

12231259
var result = command.Parse("-x -x");
12241260

1225-
GetValue(result, optionX).Should().Be("-x");
1261+
result.GetValue(optionX).Should().Be("-x");
12261262
}
12271263

12281264
[Theory]
@@ -1249,8 +1285,8 @@ public void Boolean_options_are_not_greedy(string commandLine)
12491285

12501286
result.Errors.Should().BeEmpty();
12511287

1252-
GetValue(result, optX).Should().BeTrue();
1253-
GetValue(result, optY).Should().BeTrue();
1288+
result.GetValue(optX).Should().BeTrue();
1289+
result.GetValue(optY).Should().BeTrue();
12541290
}
12551291

12561292
[Fact]
@@ -1267,8 +1303,8 @@ public void Multiple_option_arguments_that_match_multiple_arity_option_aliases_a
12671303

12681304
var result = command.Parse("-x -x -x -y -y -x -y -y -y -x -x -y");
12691305

1270-
GetValue(result, optionX).Should().BeEquivalentTo(new[] { "-x", "-y", "-y" });
1271-
GetValue(result, optionY).Should().BeEquivalentTo(new[] { "-x", "-y", "-x" });
1306+
result.GetValue(optionX).Should().BeEquivalentTo(new[] { "-x", "-y", "-y" });
1307+
result.GetValue(optionY).Should().BeEquivalentTo(new[] { "-x", "-y", "-x" });
12721308
}
12731309

12741310
[Fact]
@@ -1285,7 +1321,7 @@ public void Bundled_option_arguments_that_match_option_aliases_are_parsed_correc
12851321

12861322
var result = command.Parse("-yxx");
12871323

1288-
GetValue(result, optionX).Should().Be("x");
1324+
result.GetValue(optionX).Should().Be("x");
12891325
}
12901326

12911327
[Fact]
@@ -1302,8 +1338,8 @@ public void Argument_name_is_not_matched_as_a_token()
13021338

13031339
var result = command.Parse("name one two three");
13041340

1305-
GetValue(result, nameArg).Should().Be("name");
1306-
GetValue(result, columnsArg).Should().BeEquivalentTo("one", "two", "three");
1341+
result.GetValue(nameArg).Should().Be("name");
1342+
result.GetValue(columnsArg).Should().BeEquivalentTo("one", "two", "three");
13071343
}
13081344

13091345
[Fact]
@@ -1328,7 +1364,7 @@ public void Boolean_options_with_no_argument_specified_do_not_match_subsequent_a
13281364

13291365
var result = command.Parse("-v an-argument");
13301366

1331-
GetValue(result, option).Should().BeTrue();
1367+
result.GetValue(option).Should().BeTrue();
13321368
}
13331369

13341370
[Fact]
@@ -1345,8 +1381,8 @@ public void When_a_command_line_has_unmatched_tokens_they_are_not_applied_to_sub
13451381

13461382
var result = command.Parse("-x 23 unmatched-token -y 42");
13471383

1348-
GetValue(result, optionX).Should().Be("23");
1349-
GetValue(result, optionY).Should().Be("42");
1384+
result.GetValue(optionX).Should().Be("23");
1385+
result.GetValue(optionY).Should().Be("42");
13501386
result.UnmatchedTokens.Should().BeEquivalentTo("unmatched-token");
13511387
}
13521388

@@ -1648,7 +1684,7 @@ public void Parsed_value_of_empty_string_arg_is_an_empty_string(string arg1, str
16481684

16491685
var result = rootCommand.Parse(new[] { arg1, arg2 });
16501686

1651-
GetValue(result, option).Should().BeEmpty();
1687+
result.GetValue(option).Should().BeEmpty();
16521688
}
16531689
}
16541690
}

0 commit comments

Comments
 (0)