dotnet / dotnet/command-line-api

best way to add option validator

Open
#1,553 5 comments 0 reactions 0 assignees View on GitHub
Area-Parser and Binder question
Dominant language
C#
Stars
3.7k
Forks
428
PR merge metrics
No merged PRs in 30d

Description

What is the best way to add a validator for option parse? Is "AddValidator" best here? How is to allow await statement inside validator logic that makes a db call suppose?? It works for sync but not async - maybe delegate needs to be wrapped .. could not get an example, so reaching for help.

```
var coption = new Option("-c");
coption.FromAmong>("-", ".", "_");
coption.AddValidator(symbol => {
var pvalue = symbol.GetValueOrDefault();
//await Task.Delay(2000);
// logic for pvalue check
return null;
});
```

Looking forward for suggestion!

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.