Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,7 @@ System.CommandLine.Invocation
System.CommandLine.Parsing
public class ArgumentResult : SymbolResult
public System.CommandLine.Argument Argument { get; }
public System.Boolean Implicit { get; }
public System.Void AddError(System.String errorMessage)
public T GetValueOrDefault<T>()
public System.Void OnlyTake(System.Int32 numberOfTokens)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -258,8 +258,8 @@ public void Option_can_fallback_to_default_when_customizing(bool conditionA, boo

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

command.Options.Add(new HelpOption
{
Expand Down Expand Up @@ -300,9 +300,9 @@ public void Argument_can_fallback_to_default_when_customizing(

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

command.Options.Add(new HelpOption
{
Expand Down
1 change: 0 additions & 1 deletion src/System.CommandLine.Tests/HelpOptionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using FluentAssertions;
using Microsoft.VisualStudio.TestPlatform.Utilities;
using System.CommandLine.Help;
using System.CommandLine.Invocation;
using System.CommandLine.Tests.Utility;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// Copyright (c) .NET Foundation and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System.CommandLine.Parsing;
using System.CommandLine.Tests.Utility;
using FluentAssertions;
using System.Linq;
Expand Down
1 change: 0 additions & 1 deletion src/System.CommandLine.Tests/ParseErrorReportingTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
using System.CommandLine.Tests.Utility;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Xunit;

Expand Down
92 changes: 64 additions & 28 deletions src/System.CommandLine.Tests/ParserTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,6 @@ namespace System.CommandLine.Tests
{
public partial class ParserTests
{
private T GetValue<T>(ParseResult parseResult, Option<T> option)
=> parseResult.GetValue(option);

private T GetValue<T>(ParseResult parseResult, Argument<T> argument)
=> parseResult.GetValue(argument);

[Fact]
public void An_option_can_be_checked_by_object_instance()
{
Expand Down Expand Up @@ -816,7 +810,7 @@ public void Commands_can_have_default_argument_values()

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

GetValue(result, argument)
result.GetValue(argument)
.Should()
.Be("default");

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

result.GetResult(option).Should().NotBeNull();
GetValue(result, option).Should().Be("the-default");
result.GetValue(option).Should().Be("the-default");
}

[Fact]
Expand Down Expand Up @@ -926,6 +920,48 @@ public void When_an_argument_with_a_default_value_is_not_matched_then_there_are_
.BeEmpty();
}

[Fact]
public void When_an_argument_with_a_default_value_is_matched_then_the_option_result_is_implicit()
{
var argument = new Argument<string>("the-arg")
{
DefaultValueFactory = _ => "the-default"
};

var command = new Command("command")
{
argument
};

var result = command.Parse("command the-explicit-value");

result.GetResult(argument)
.Implicit
.Should()
.BeFalse();
}

[Fact]
public void When_an_argument_with_a_default_value_is_not_matched_then_the_option_result_is_implicit()
{
var argument = new Argument<string>("the-arg")
{
DefaultValueFactory = _ => "the-default"
};

var command = new Command("command")
{
argument
};

var result = command.Parse("command");

result.GetResult(argument)
.Implicit
.Should()
.BeTrue();
}

[Fact]
public void Command_default_argument_value_does_not_override_parsed_value()
{
Expand All @@ -941,7 +977,7 @@ public void Command_default_argument_value_does_not_override_parsed_value()

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

GetValue(result, argument)
result.GetValue(argument)
.Name
.Should()
.Be("the-directory");
Expand Down Expand Up @@ -1125,7 +1161,7 @@ public void Option_arguments_can_start_with_prefixes_that_make_them_look_like_op

var result = command.Parse(input);

GetValue(result, optionX).Should().Be("-y");
result.GetValue(optionX).Should().Be("-y");
}

[Fact]
Expand All @@ -1144,9 +1180,9 @@ public void Option_arguments_can_start_with_prefixes_that_make_them_look_like_bu

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

GetValue(result, optionA).Should().Be("-bc");
GetValue(result, optionB).Should().BeFalse();
GetValue(result, optionC).Should().BeFalse();
result.GetValue(optionA).Should().Be("-bc");
result.GetValue(optionB).Should().BeFalse();
result.GetValue(optionC).Should().BeFalse();
}

[Fact]
Expand All @@ -1161,7 +1197,7 @@ public void Option_arguments_can_match_subcommands()

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

GetValue(result, optionA).Should().Be("subcommand");
result.GetValue(optionA).Should().Be("subcommand");
result.CommandResult.Command.Should().BeSameAs(root);
}

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

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

GetValue(result, argument)
result.GetValue(argument)
.Should()
.BeEquivalentSequenceTo("one", "two", "three", "subcommand", "four");

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

result.Errors.Should().BeEmpty();
GetValue(result, optionX).Should().Be("-y");
result.GetValue(optionX).Should().Be("-y");
}

[Fact]
Expand All @@ -1222,7 +1258,7 @@ public void Single_option_arguments_that_match_option_aliases_are_parsed_correct

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

GetValue(result, optionX).Should().Be("-x");
result.GetValue(optionX).Should().Be("-x");
}

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

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

GetValue(result, optX).Should().BeTrue();
GetValue(result, optY).Should().BeTrue();
result.GetValue(optX).Should().BeTrue();
result.GetValue(optY).Should().BeTrue();
}

[Fact]
Expand All @@ -1267,8 +1303,8 @@ public void Multiple_option_arguments_that_match_multiple_arity_option_aliases_a

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

GetValue(result, optionX).Should().BeEquivalentTo(new[] { "-x", "-y", "-y" });
GetValue(result, optionY).Should().BeEquivalentTo(new[] { "-x", "-y", "-x" });
result.GetValue(optionX).Should().BeEquivalentTo(new[] { "-x", "-y", "-y" });
result.GetValue(optionY).Should().BeEquivalentTo(new[] { "-x", "-y", "-x" });
}

[Fact]
Expand All @@ -1285,7 +1321,7 @@ public void Bundled_option_arguments_that_match_option_aliases_are_parsed_correc

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

GetValue(result, optionX).Should().Be("x");
result.GetValue(optionX).Should().Be("x");
}

[Fact]
Expand All @@ -1302,8 +1338,8 @@ public void Argument_name_is_not_matched_as_a_token()

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

GetValue(result, nameArg).Should().Be("name");
GetValue(result, columnsArg).Should().BeEquivalentTo("one", "two", "three");
result.GetValue(nameArg).Should().Be("name");
result.GetValue(columnsArg).Should().BeEquivalentTo("one", "two", "three");
}

[Fact]
Expand All @@ -1328,7 +1364,7 @@ public void Boolean_options_with_no_argument_specified_do_not_match_subsequent_a

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

GetValue(result, option).Should().BeTrue();
result.GetValue(option).Should().BeTrue();
}

[Fact]
Expand All @@ -1345,8 +1381,8 @@ public void When_a_command_line_has_unmatched_tokens_they_are_not_applied_to_sub

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

GetValue(result, optionX).Should().Be("23");
GetValue(result, optionY).Should().Be("42");
result.GetValue(optionX).Should().Be("23");
result.GetValue(optionY).Should().Be("42");
result.UnmatchedTokens.Should().BeEquivalentTo("unmatched-token");
}

Expand Down Expand Up @@ -1648,7 +1684,7 @@ public void Parsed_value_of_empty_string_arg_is_an_empty_string(string arg1, str

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

GetValue(result, option).Should().BeEmpty();
result.GetValue(option).Should().BeEmpty();
}
}
}
Loading
Loading