dotnet / dotnet/command-line-api

Argument validator did not run because of GetValue within a Command validator

Open
#2,607 6 comments 0 reactions 1 assignee Claimed by @adamsitnik View on GitHub
Area-Documentation
Dominant language
C#
Stars
3.7k
Forks
428
PR merge metrics
No merged PRs in 30d

Description

Not sure if this is a bug, but it's confusing.

I have an `Argument inputArg` which has a validator and this argument is on a command which also has a validator. The Command Validator was calling `commandResult.GetResult(inputArg).GetValue(inputArg)` within. The command line is "command asdf". The argument's validator is expecting one specific string word or 1 or more numbers.

The argument's validator was not being called. I moved through parsing etc and on to validation using the debugger and I noticed that at some point a conversion result was being produced and there's a part where validation is about to be called on that argument, but it doesn't because the code does sth like:
`_conversionResult ??= ValidateAndConvert(..)`.

So the argument's validator doesn't trigger. Afterwards I noticed that _conversionResult is being set when I call GetValue.

If I change the GetValue to `commandResult.GetResult(inputArg).Tokens`, this doesn't happen.

That said, there's not enough documentation on Validators and the only example on the MS docs uses GetValue, so a user will likely default to that, even if there's other examples buried in the Github Issues that uses Tokens instead.

Is this intentional or a bug?

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.