Skip to content
Closed
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
109 changes: 103 additions & 6 deletions src/System.CommandLine.Tests/ParserTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public partial class ParserTests
public void An_option_without_a_long_form_can_be_checked_for_using_a_prefix()
{
var option = new Option("--flag");

var result = option.Parse("--flag");

result.FindResultFor(option).Should().NotBeNull();
Expand Down Expand Up @@ -213,7 +213,7 @@ public void Short_form_options_can_be_specified_using_equals_delimiter()
[Fact]
public void Long_form_options_can_be_specified_using_equals_delimiter()
{
var option =
var option =
new Option("--hello", arity: ArgumentArity.ExactlyOne);

var result = option.Parse("--hello=there");
Expand Down Expand Up @@ -631,7 +631,7 @@ public void When_an_option_is_not_respecified_but_limit_is_reached_then_the_foll
var animalsOption = new Option(new[] { "-a", "--animals" }, arity: ArgumentArity.ZeroOrOne);

var vegetablesOption = new Option(new[] { "-v", "--vegetables" }, arity: ArgumentArity.ZeroOrOne);

var parser = new Parser(
new Command("the-command")
{
Expand Down Expand Up @@ -1031,6 +1031,103 @@ public void Subsequent_occurrences_of_tokens_matching_command_names_are_parsed_a
completeResult.Tokens.Select(t => t.Value).Should().BeEquivalentTo("the-command");
}

[Fact]
public void HasArgument_returns_expected_result_when_argument_provided()
{
var stringArgument = new Argument<string>();

var command = new Command("the-command")
{
new Command("complete")
{
stringArgument,
new Option<int>("--position")
}
};

ParseResult result = command.Parse("the-command",
"complete",
"--position",
"7",
"the-argument");

CommandResult completeResult = result.CommandResult;

completeResult.HasArgument(stringArgument).Should().BeTrue();
}

[Fact]
public void HasArgument_returns_expected_result_when_argument_not_provided()
{
var stringArgument = new Argument<string>();

var command = new Command("the-command")
{
new Command("complete")
{
stringArgument,
new Option<int>("--position")
}
};

ParseResult result = command.Parse("the-command",
"complete",
"--position",
"7");

CommandResult completeResult = result.CommandResult;

completeResult.HasArgument(stringArgument).Should().BeFalse();
}

[Fact]
public void HasOption_returns_expected_result_when_option_provided()
{
var positionOption = new Option<int>("--position");

var command = new Command("the-command")
{
new Command("complete")
{
new Argument<string>(),
positionOption
}
};

ParseResult result = command.Parse("the-command",
"complete",
"--position",
"7",
"the-command");

CommandResult completeResult = result.CommandResult;

completeResult.HasOption(positionOption).Should().BeTrue();
}

[Fact]
public void HasOption_returns_expected_result_when_option_not_provided()
{
var positionOption = new Option<int>("--position");

var command = new Command("the-command")
{
new Command("complete")
{
new Argument<string>(),
positionOption
}
};

ParseResult result = command.Parse("the-command",
"complete",
"the-command");

CommandResult completeResult = result.CommandResult;

completeResult.HasOption(positionOption).Should().BeFalse();
}

[Fact]
public void A_root_command_can_be_omitted_from_the_parsed_args()
{
Expand Down Expand Up @@ -1183,7 +1280,7 @@ public void When_an_option_with_a_default_value_is_not_matched_then_the_option_r
public void When_an_option_with_a_default_value_is_not_matched_then_there_are_no_tokens()
{
var option = new Option<string>(
"-o",
"-o",
() => "the-default");

var command = new Command("command")
Expand All @@ -1203,7 +1300,7 @@ public void When_an_option_with_a_default_value_is_not_matched_then_there_are_no
public void When_an_argument_with_a_default_value_is_not_matched_then_there_are_no_tokens()
{
var argument = new Argument<string>(
"o",
"o",
() => "the-default");

var command = new Command("command")
Expand Down Expand Up @@ -1761,7 +1858,7 @@ public CollectionWithCustomTypeConverter(IEnumerable<string> values)
: base(values)
{ }
}

public class CustomCollectionTypeConverter : TypeConverter
{
public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
Expand Down
24 changes: 24 additions & 0 deletions src/System.CommandLine/Parsing/CommandResultExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,30 @@ namespace System.CommandLine.Parsing
{
public static class CommandResultExtensions
{
public static bool HasOption(
this CommandResult commandResult,
IOption option)
{
if (commandResult is null)
{
throw new ArgumentNullException(nameof(commandResult));
}

return commandResult.FindResultFor(option) is { };
}

public static bool HasArgument(
this CommandResult commandResult,
IArgument argument)
{
if (commandResult is null)
{
throw new ArgumentNullException(nameof(commandResult));
}

return commandResult.FindResultFor(argument) is { };
}

internal static bool TryGetValueForArgument(
this CommandResult commandResult,
IValueDescriptor valueDescriptor,
Expand Down