Skip to content

Commit 7f7c394

Browse files
committed
Added implicit conversions to allow using ArgumentArity or Arity DU.
1 parent e20df56 commit 7f7c394

File tree

2 files changed

+18
-6
lines changed

2 files changed

+18
-6
lines changed

src/FSharp.SystemCommandLine/Inputs.fs

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,8 @@ type Arity =
5454
| ZeroOrMore
5555
| ZeroOrOne
5656

57-
module Arity =
58-
59-
let toArgumentArity (arity: Arity) =
57+
/// Implicitly convert an Arity DU to an ArgumentArity instance.
58+
static member inline op_Implicit(arity: Arity) : ArgumentArity =
6059
match arity with
6160
| ArgumentArity (min, max) -> (min, max)
6261
| ExactlyOne -> (1, 1)
@@ -68,6 +67,18 @@ module Arity =
6867
| ZeroOrOne -> (0, 1)
6968
|> System.CommandLine.ArgumentArity
7069

70+
/// Implicitly convert an ArgumentArity instance to an Arity DU.
71+
static member inline op_Implicit(argumentArity: ArgumentArity) : Arity =
72+
match argumentArity.MinimumNumberOfValues, argumentArity.MaximumNumberOfValues with
73+
| 0, 0 -> Zero
74+
| 0, max when max = 100_000 -> ZeroOrMore
75+
| min, max when min = max -> ExactlyOne
76+
| min, max when min = 1 && max = 100_000 -> OneOrMore
77+
| min, max when min > 0 && max = 100_000 -> MinimumNumberOfValues min
78+
| min, max when min = 0 && max < 100_000 -> MaximumNumberOfValues max
79+
| _ -> ArgumentArity (argumentArity.MinimumNumberOfValues, argumentArity.MaximumNumberOfValues)
80+
81+
7182
module Input =
7283

7384
/// Injects an `ActionContext` into the action which contains the `ParseResult` and a cancellation token.
@@ -164,7 +175,7 @@ module Input =
164175
| [ token ] -> MaybeParser.parseTokenValue token.Value
165176
| _ :: _ -> failwith "F# Option can only be used with a single argument."
166177
)
167-
o.Arity <- ArgumentArity (0, 1) |> Arity.toArgumentArity
178+
o.Arity <- ArgumentArity (0, 1)
168179
o.DefaultValueFactory <- (fun _ -> None)
169180
ActionInput.OfOption<'T option> o
170181

@@ -265,8 +276,8 @@ module Input =
265276
/// Sets the arity of an option or argument.
266277
let arity (arity: Arity) (input: ActionInput<'T>) =
267278
input
268-
|> editOption (fun o -> o.Arity <- arity |> Arity.toArgumentArity)
269-
|> editArgument (fun a -> a.Arity <- arity |> Arity.toArgumentArity)
279+
|> editOption (fun o -> o.Arity <- arity)
280+
|> editArgument (fun a -> a.Arity <- arity)
270281

271282
/// Sets a value that indicates whether multiple arguments are allowed for each option identifier token. (Defaults to 'false'.)
272283
let allowMultipleArgumentsPerToken (input: ActionInput<'T>) =

src/TestConsole/ProgramAppendWords.fs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ let main argv =
1414
option "--word"
1515
|> alias "-w"
1616
|> desc "A list of words to be appended"
17+
|> arity OneOrMore
1718
|> acceptOnlyFromAmong [ "hello" ; "world" ],
1819

1920
optionMaybe "--separator"

0 commit comments

Comments
 (0)