cannorin / FSharp.CommandLine

A framework for building command line application in F#
Apache License 2.0
59 stars 1 forks source link

Required options #11

Open imre-turi-cko opened 2 years ago

imre-turi-cko commented 2 years ago

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 commented 2 years ago

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.