Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Required options #11

Open
imre-turi-cko opened this issue Jan 1, 2022 · 1 comment
Open

Required options #11

imre-turi-cko opened this issue Jan 1, 2022 · 1 comment
Labels
enhancement New feature or request

Comments

@imre-turi-cko
Copy link

Hey,

It's not really an issue, but a question.
Is there a way to set an option required?

Something like:

opt files in fileOption |> CommandOption.requiredAndExactlyOne
@cannorin cannorin added the enhancement New feature or request label Jan 7, 2022
@cannorin
Copy link
Owner

cannorin commented Jan 7, 2022

It's possible but not very straightforward:

open FSharp.CommandLine

let inline exactlyOne (co: #ICommandOption<_>) =
    co |> CommandOption.zeroOrMore 
       |> CommandOption.map (function 
           | x :: [] -> x 
           | _ -> 
            let msg = sprintf "the option '%s' is required and should be provided only once"
                              (co.Summary.NameRepresentations |> List.head)
            OptionParseFailed (co.Summary, msg) |> raise
          )

Maybe worth adding to the library.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants