-
Notifications
You must be signed in to change notification settings - Fork 416
GetValue<FileInfo>() in Command validator suppresses AcceptExistingOnly() errors #2786
Copy link
Copy link
Open
Labels
Description
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 errorsExpected: 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.
Reactions are currently unavailable