Skip to content

GetValue<FileInfo>() in Command validator suppresses AcceptExistingOnly() errors #2786

@moh-hassan

Description

@moh-hassan

Description

Calling result.GetValue<FileInfo>(argument) inside a Command-level validator suppresses errors from AcceptExistingOnly() (and custom argument validators).

Version

System.CommandLine Version="2.0.5"

Repro

var root = new RootCommand();

var demo = new Option<bool>("--demo");
root.Options.Add(demo);

var fileArg = new Argument<FileInfo>("file");
root.Arguments.Add(fileArg);
fileArg.AcceptExistingOnly();

root.Validators.Add(result =>
{
    bool hasDemo = result.GetValue(demo);
    FileInfo? f = result.GetValue(fileArg);   // ← triggers the issue
});

var result = root.Parse("nonexistent.xyz");

result.ShowErrors();                    // nothing shown
// Expected: 1 error, Actual: 0 errors

Expected: File does not exist error
Actual: No errors reported

Workaround

Use result.GetResult(fileArg) instead of GetValue<FileInfo>(fileArg):

var argResult = result.GetResult(fileArg);
bool hasFile = argResult?.Tokens.Count > 0 ?? false;

or

bool hasFile = result.GetResult(fileArg)?.Value != null;

This side effect makes combining Command validators with standard argument validation Breakable.

Demo:

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions