Skip to content

[main] Source code updates from dotnet/command-line-api #1503

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 16, 2025
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 @@ -37,7 +37,7 @@ public async Task When_the_version_option_is_specified_then_invocation_is_short_
{
var wasCalled = false;
var rootCommand = new RootCommand();
rootCommand.SetAction((_) => wasCalled = true);
rootCommand.SetAction(_ => wasCalled = true);

var output = new StringWriter();

Expand All @@ -46,6 +46,21 @@ public async Task When_the_version_option_is_specified_then_invocation_is_short_
wasCalled.Should().BeFalse();
}

[Fact] // https://github.com/dotnet/command-line-api/issues/2628
public void When_the_version_option_is_specified_then_there_are_no_parse_errors_due_to_unspecified_subcommand()
{
Command subcommand = new("subcommand");
RootCommand root = new()
{
subcommand
};
subcommand.SetAction(_ => 0);

var parseResult = root.Parse("--version");

parseResult.Errors.Should().BeEmpty();
}

[Fact]
public async Task Version_option_appears_in_help()
{
Expand Down
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 System.CommandLine.Parsing;
using System.Linq;

namespace System.CommandLine
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,17 +43,10 @@ internal CommandResult(
internal override bool UseDefaultValueFor(ArgumentResult argumentResult)
=> argumentResult.Argument.HasDefaultValue && argumentResult.Tokens.Count == 0;

/// <param name="completeValidation">Only the inner most command goes through complete validation.</param>
internal void Validate(bool completeValidation)
internal void Validate(bool isInnermostCommand)
{
if (completeValidation)
if (isInnermostCommand)
{
if (Command.Action is null && Command.HasSubcommands)
{
SymbolResultTree.InsertFirstError(
new ParseError(LocalizationResources.RequiredCommandWasNotProvided(), this));
}

if (Command.HasValidators)
{
int errorCountBefore = SymbolResultTree.ErrorCount;
Expand All @@ -71,12 +64,12 @@ internal void Validate(bool completeValidation)

if (Command.HasOptions)
{
ValidateOptionsAndAddDefaultResults(completeValidation);
ValidateOptionsAndAddDefaultResults(isInnermostCommand);
}

if (Command.HasArguments)
{
ValidateArgumentsAndAddDefaultResults(completeValidation);
ValidateArgumentsAndAddDefaultResults(isInnermostCommand);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ internal ParseResult Parse()

ValidateAndAddDefaultResults();


if (_isHelpRequested)
{
_symbolResultTree.Errors?.Clear();
Expand Down Expand Up @@ -373,17 +374,24 @@ private void AddCurrentTokenToUnmatched()

private void ValidateAndAddDefaultResults()
{
// Only the inner most command goes through complete validation,
// Only the innermost command goes through complete validation,
// for other commands only a subset of options is checked.
_innermostCommandResult.Validate(completeValidation: true);
_innermostCommandResult.Validate(isInnermostCommand: true);

CommandResult? currentResult = _innermostCommandResult.Parent as CommandResult;
while (currentResult is not null)
{
currentResult.Validate(completeValidation: false);
currentResult.Validate(isInnermostCommand: false);

currentResult = currentResult.Parent as CommandResult;
}

if (_primaryAction is null &&
_innermostCommandResult is { Command: { Action: null, HasSubcommands: true } })
{
_symbolResultTree.InsertFirstError(
new ParseError(LocalizationResources.RequiredCommandWasNotProvided(), _innermostCommandResult));
}
}
}
}
4 changes: 2 additions & 2 deletions src/source-manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@
"commitSha": "95da703c5b2c85bf4c81b92c1ea1994c7de4970f"
},
{
"barId": 274651,
"barId": 275338,
"path": "command-line-api",
"remoteUri": "https://github.com/dotnet/command-line-api",
"commitSha": "12473cd29fac1b9070c71ed8f5a43cada6872905"
"commitSha": "dbc3781d3398b69cd9ffe24f60a7b77110bdadbf"
},
{
"barId": 274234,
Expand Down
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy