Skip to content

Commit cdc2772

Browse files
JordanMarrclaude
authored andcommitted
Tidy mapFromAmong docs and tests after JordanMarr#37
- README example was missing the option name argument - Drop the "tryParse overrides mapFromAmong" note, remarks, and the test asserting that undefined behaviour - Reword doc comments with inline examples Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01PsmkZPCobsju7AauvVH8du
1 parent 5144078 commit cdc2772

3 files changed

Lines changed: 7 additions & 44 deletions

File tree

‎README.md‎

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -796,7 +796,7 @@ type Configuration =
796796
| Release
797797
798798
let config =
799-
Input.option<Configuration>
799+
Input.option<Configuration> "--config"
800800
|> Input.mapFromAmong [
801801
"r", Release; "release", Release
802802
"R", Release; "Release", Release
@@ -808,16 +808,13 @@ let config =
808808
// use `Input.mapFromAmongWith` to pass a custom string comparer!
809809
810810
let config =
811-
Input.option<Configuration>
811+
Input.option<Configuration> "--config"
812812
|> Input.mapFromAmongWith StringComparer.OrdinalIgnoreCase [
813813
"r", Release; "release", Release
814814
"d", Debug; "debug", Debug
815815
]
816816
```
817817

818-
Notes about overriding properties:
819-
* `Input.tryParse` will overwrite the configuration from `.mapFromAmong` if it is used downstream.
820-
821818
</details>
822819

823820
---

‎src/FSharp.SystemCommandLine/Inputs.fs‎

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
namespace FSharp.SystemCommandLine
1+
namespace FSharp.SystemCommandLine
22

33
open System
44
open System.CommandLine
@@ -364,10 +364,8 @@ module Input =
364364
)
365365

366366

367-
/// <summary>
368-
/// Maps an input whose legal values are a known set bound to a typed value using the given <c>StringComparer</c>.
369-
/// <remarks>Caution: avoid overriding downstream with <c>Input.tryParse</c>.</remarks>
370-
/// </summary>
367+
/// Maps an input whose legal values are a known set of strings, each bound to a typed value, using the given `StringComparer`.
368+
/// Example: `option<Env> "--env" |> mapFromAmongWith StringComparer.OrdinalIgnoreCase [ "dev", Dev; "prod", Prod ]`
371369
let mapFromAmongWith (comparer: StringComparer) (choices: seq<string * 'T>) (input: ActionInput<'T>) =
372370
let keys = choices |> Seq.map fst
373371
let legal = keys |> String.concat ", "
@@ -384,8 +382,8 @@ module Input =
384382
| None -> Error $"'%s{token.Value}' is not a valid choice from: %s{legal}"
385383
)
386384

387-
/// <summary>Maps an input whose legal values are a known set bound to a typed value.</summary>
388-
/// <remarks>Caution: avoid overriding downstream with <c>Input.tryParse</c>.</remarks>
385+
/// Maps an input whose legal values are a known set of strings, each bound to a typed value (case-sensitive).
386+
/// Example: `option<Env> "--env" |> mapFromAmong [ "dev", Dev; "prod", Prod ]`
389387
let mapFromAmong (choices: seq<string * 'T>) (input: ActionInput<'T>) =
390388
mapFromAmongWith StringComparer.Ordinal choices input
391389

‎src/Tests/MapFromAmongTest.fs‎

Lines changed: 0 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -93,35 +93,3 @@ let ``03 - mapFromAmongWith returns correct typed DU - case insensitive``() =
9393
// invalid map
9494
shouldFail "--du a" B
9595
shouldFail "--du B" A
96-
97-
[<Test>]
98-
let ``04 - mapFromAmong followed by different tryParse will override``() =
99-
let input = option<DUType> "--du" |> mapFromAmong duChoices |> tryParse (fun _ -> Ok A)
100-
let compareAgainst (shouldSucceed: bool): string -> DUType -> unit = fun cmd v ->
101-
testRootCommand cmd {
102-
description "Test"
103-
inputs input
104-
setAction (fun o ->
105-
if shouldSucceed
106-
then o =! v |> callAction; 0
107-
else o <>! v; 1
108-
)
109-
}
110-
|> if shouldSucceed then (=!) 0 else (<>!) 0
111-
actionCalled =! shouldSucceed
112-
actionCalled <- false
113-
let shouldSucceed = compareAgainst true
114-
let shouldFail = compareAgainst false
115-
shouldSucceed "--du a" A
116-
shouldSucceed "--du A" A
117-
shouldSucceed "--du b" A
118-
shouldSucceed "--du B" A
119-
shouldFail "--du a" B
120-
shouldFail "--du A" B
121-
shouldFail "--du b" B
122-
shouldFail "--du B" B
123-
// invalid input still processes
124-
// completions will still show correctly
125-
// undefined behaviour
126-
shouldSucceed "--du c" A
127-
shouldFail "--du c" B

0 commit comments

Comments
 (0)