Skip to content

Built-in support for --no-style flag args #1725

Open
@baronfel

Description

@baronfel

Currently flag args are of the form --flag <true|false>. 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:

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

open System.CommandLine
open System.CommandLine.Parsing

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


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

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @jonsequitur@baronfel

        Issue actions

          Built-in support for `--no`-style flag args · Issue #1725 · dotnet/command-line-api