Skip to content

Commit 5caabed

Browse files
committed
Fixed OptionMaybe<bool> to work with opt flag w/ no argument
1 parent b876228 commit 5caabed

1 file changed

Lines changed: 14 additions & 25 deletions

File tree

src/FSharp.SystemCommandLine/Inputs.fs

Lines changed: 14 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -65,15 +65,6 @@ type Input =
6565
)
6666
|> HandlerInput.OfOption
6767

68-
/// Creates a CLI option of type 'T that is required.
69-
static member OptionRequired<'T>(name: string, ?description: string) =
70-
Option<'T>(
71-
name,
72-
description = (description |> Option.defaultValue null),
73-
IsRequired = true
74-
)
75-
|> HandlerInput.OfOption
76-
7768
/// Creates a CLI option of type 'T that is required.
7869
static member OptionRequired<'T>(aliases: string seq, ?description: string) =
7970
Option<'T>(
@@ -82,35 +73,33 @@ type Input =
8273
IsRequired = true
8374
)
8475
|> HandlerInput.OfOption
85-
86-
/// Creates a CLI option of type 'T option.
87-
static member OptionMaybe<'T>(name: string, ?description: string) =
88-
Option<'T option>(
89-
name,
90-
parseArgument = (fun argResult ->
91-
match argResult.Tokens |> Seq.toList with
92-
| [] -> None
93-
| [ token ] -> MaybeParser.parseTokenValue token.Value
94-
| _ :: _ -> failwith "F# Option can only be used with a single argument."
95-
),
96-
description = (description |> Option.defaultValue null)
97-
)
98-
|> HandlerInput.OfOption
76+
77+
/// Creates a CLI option of type 'T that is required.
78+
static member OptionRequired<'T>(name: string, ?description: string) =
79+
Input.OptionRequired<'T>([| name |], description |> Option.defaultValue null)
9980

10081
/// Creates a CLI option of type 'T option.
10182
static member OptionMaybe<'T>(aliases: string seq, ?description: string) =
83+
let isBool = typeof<'T> = typeof<bool>
10284
Option<'T option>(
10385
aliases |> Seq.toArray,
10486
parseArgument = (fun argResult ->
10587
match argResult.Tokens |> Seq.toList with
88+
| [] when isBool -> true |> unbox<'T> |> Some
10689
| [] -> None
10790
| [ token ] -> MaybeParser.parseTokenValue token.Value
10891
| _ :: _ -> failwith "F# Option can only be used with a single argument."
10992
),
110-
description = (description |> Option.defaultValue null)
93+
description = (description |> Option.defaultValue null),
94+
Arity = ArgumentArity(0, 1)
11195
)
96+
|> fun o -> o.SetDefaultValue(None); o
11297
|> HandlerInput.OfOption
11398

99+
/// Creates a CLI option of type 'T option.
100+
static member OptionMaybe<'T>(name: string, ?description: string) =
101+
Input.OptionMaybe<'T>([| name |], description |> Option.defaultValue null)
102+
114103
/// Creates a CLI argument of type 'T.
115104
static member Argument<'T>(name: string, ?description: string) =
116105
Argument<'T>(
@@ -140,7 +129,7 @@ type Input =
140129
),
141130
description = (description |> Option.defaultValue null),
142131
isDefault = true
143-
)
132+
)
144133
|> HandlerInput.OfArgument
145134

146135
/// Creates an injected dependency input.

0 commit comments

Comments
 (0)