dotnet / dotnet/command-line-api

Built-in support for `--no`-style flag args

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

Description

Currently flag args are of the form `--flag `. There's a bit of a convention to allow `--flag` or `--no-flag`. It would be nice to support this in the API.

In the interim, this can be done with a custom ParseArgument delegate:

```fsharp
#r "nuget: System.CommandLine, 2.0.0-beta3.22114.1"

open System.CommandLine
open System.CommandLine.Parsing

let parseFlag = ParseArgument(fun r ->
match r.Parent with
| :? OptionResult as b ->
b.Token.Value = "--force"
| _ ->
r.ErrorMessage <- "unknown thingy"
Unchecked.defaultof
)
let c = Option([| "--force"; "--no-force" |], parseArgument = parseFlag)
c.Arity <- ArgumentArity.Zero

true = c.Parse("--force").GetValueForOption(c)
false = c.Parse("--no-force").GetValueForOption(c)

```

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.