Skip to content

Commit 813df53

Browse files
committed
Fixed ArgumentMaybe; + tests
1 parent c02cdfa commit 813df53

4 files changed

Lines changed: 67 additions & 16 deletions

File tree

src/FSharp.SystemCommandLine/Inputs.fs

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -87,10 +87,11 @@ type Input =
8787
Option<'T option>(
8888
name,
8989
parseArgument = (fun argResult ->
90-
match argResult.Tokens |> Seq.tryHead with
91-
| Some token -> Parser.parseTokenValue token.Value
92-
| None -> failwith "F# Option can only be used with a single argument."
93-
),
90+
match argResult.Tokens |> Seq.toList with
91+
| [] -> None
92+
| [ token ] -> Parser.parseTokenValue token.Value
93+
| _ :: _ -> failwith "F# Option can only be used with a single argument."
94+
),
9495
description = (description |> Option.defaultValue null)
9596
)
9697
|> HandlerInput.OfOption
@@ -100,10 +101,11 @@ type Input =
100101
Option<'T option>(
101102
aliases |> Seq.toArray,
102103
parseArgument = (fun argResult ->
103-
match argResult.Tokens |> Seq.tryHead with
104-
| Some token -> Parser.parseTokenValue token.Value
105-
| None -> failwith "F# Option can only be used with a single argument."
106-
),
104+
match argResult.Tokens |> Seq.toList with
105+
| [] -> None
106+
| [ token ] -> Parser.parseTokenValue token.Value
107+
| _ :: _ -> failwith "F# Option can only be used with a single argument."
108+
),
107109
description = (description |> Option.defaultValue null)
108110
)
109111
|> HandlerInput.OfOption
@@ -130,11 +132,13 @@ type Input =
130132
Argument<'T option>(
131133
name,
132134
parse = (fun argResult ->
133-
match argResult.Tokens |> Seq.tryHead with
134-
| Some token -> Parser.parseTokenValue token.Value
135-
| None -> failwith "F# Option can only be used with a single argument."
136-
),
137-
description = (description |> Option.defaultValue null)
135+
match argResult.Tokens |> Seq.toList with
136+
| [] -> None
137+
| [ token ] -> Parser.parseTokenValue token.Value
138+
| _ :: _ -> failwith "F# Option can only be used with a single argument."
139+
),
140+
description = (description |> Option.defaultValue null),
141+
isDefault = true
138142
)
139143
|> HandlerInput.OfArgument
140144

src/Tests/ArgumentMaybeTest.fs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
module ArgumentMaybeTest
2+
3+
open NUnit.Framework
4+
open Swensen.Unquote
5+
open FSharp.SystemCommandLine
6+
open Utils
7+
8+
let nameMaybe = Input.ArgumentMaybe<string>("Maybe a name")
9+
10+
let rootCmd argstr (handler: string option -> unit) =
11+
testRootCommand argstr {
12+
description "Maybe displays a name"
13+
inputs (nameMaybe)
14+
setHandler handler
15+
}
16+
|> ignore
17+
18+
[<Test>]
19+
let ``01 Some jdoe`` () =
20+
let mutable handlerCalled = false
21+
rootCmd "jdoe"
22+
(fun (nameMaybe) ->
23+
handlerCalled <- true
24+
nameMaybe =! Some "jdoe"
25+
)
26+
27+
handlerCalled =! true
28+
29+
[<Test>]
30+
let ``02 None`` () =
31+
let mutable handlerCalled = false
32+
rootCmd ""
33+
(fun (nameMaybe) ->
34+
handlerCalled <- true
35+
nameMaybe =! None
36+
)
37+
38+
handlerCalled =! true

src/Tests/SimpleAsyncTest.fs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ open FSharp.SystemCommandLine
66
open Utils
77

88
let words = Input.Option(["--word"; "-w"], Array.empty, "A list of words to be appended")
9-
let separator = Input.Option(["--separator"; "-s"], ",", "A character that will separate the joined words.")
9+
let separator = Input.OptionMaybe(["--separator"; "-s"], "A character that will separate the joined words.")
1010

11-
let rootCmd argstr (handler: string array * string -> unit) =
11+
let rootCmd argstr (handler: string array * string option -> unit) =
1212
task {
1313
testRootCommand argstr {
1414
description "Appends words together"
@@ -23,6 +23,14 @@ let ``01 --word Hello -w World -s *`` () =
2323
rootCmd "--word Hello -w World -s *"
2424
(fun (words, separator) ->
2525
words =! [| "Hello"; "World" |]
26-
separator =! "*"
26+
separator =! Some "*"
27+
)
28+
29+
[<Test>]
30+
let ``02 --word Hello -w World`` () =
31+
rootCmd "--word Hello -w World"
32+
(fun (words, separator) ->
33+
words =! [| "Hello"; "World" |]
34+
separator =! None
2735
)
2836

src/Tests/Tests.fsproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
<ItemGroup>
1111
<Compile Include="Utils.fs" />
1212
<Compile Include="SubCommandAppTest.fs" />
13+
<Compile Include="ArgumentMaybeTest.fs" />
1314
<Compile Include="SimpleAsyncTest.fs" />
1415
<Compile Include="SimpleAppTest.fs" />
1516
<Compile Include="Program.fs" />

0 commit comments

Comments
 (0)