Skip to content

Commit 9763f0c

Browse files
JordanMarrclaude
andcommitted
v2.2.0 - Add input { } CE for composing inputs; document in README
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01FrmC1EQmyB68Tqsn3EG7UA
1 parent 516e304 commit 9763f0c

2 files changed

Lines changed: 72 additions & 1 deletion

File tree

‎README.md‎

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ _[Click here to view the old beta 4 README](README-beta4.md)_
1313
* `Input.context` helper allows you to pass the `ActionContext` to your action function which is necessary for some operations.
1414
* `Input.inject` helper allows you to inject pre-resolved dependencies (e.g., loggers, services) into your action function alongside parsed CLI inputs.
1515
* `Input.validate` helper allows you to validate against parsed value using the F# `Result` type.
16+
* `input { }` computation expression composes any number of inputs into a single named value — no more 8-input tuple limit or positional matching.
1617

1718
## Example
1819

@@ -63,6 +64,76 @@ let main argv =
6364
_Notice that mismatches between the `setAction` and the `inputs` are caught as a compile time error:_
6465
![fs scl demo](https://user-images.githubusercontent.com/1030435/164288239-e0ff595d-cdb2-47f8-9381-50c89aedd481.gif)
6566

67+
## Composing Inputs with `input { }`
68+
69+
As an alternative to the `inputs` tuple, the `input { }` computation expression composes any number of inputs into a *single* composite input using applicative `let!`/`and!` bindings. Each value is named at its binding site, and the action receives whatever shape you `return` — typically an anonymous record:
70+
71+
```F#
72+
[<EntryPoint>]
73+
let main argv =
74+
rootCommand argv {
75+
description "Appends words together"
76+
input {
77+
let! words = option<string[]> "--word" |> alias "-w" |> desc "A list of words to be appended"
78+
and! separator = optionMaybe "--separator" |> alias "-s" |> desc "A character that will separate the joined words."
79+
return {| Words = words; Separator = separator |}
80+
}
81+
setAction (fun io ->
82+
let separator = defaultArg io.Separator ", "
83+
io.Words |> String.concat separator |> printfn "%s"
84+
)
85+
}
86+
```
87+
88+
This removes the 8-input tuple limit (bind as many inputs as you like with `and!`) and the need to keep the `inputs` tuple and the `setAction` parameters in positional sync.
89+
90+
The composite input is a normal `ActionInput<'T>`, so it is first-class — all of these work:
91+
92+
```F#
93+
// Declared separately and passed to `inputs`:
94+
let io = input {
95+
let! words = option<string[]> "--word"
96+
and! separator = option "--separator" |> def ", "
97+
return {| Words = words; Separator = separator |}
98+
}
99+
100+
rootCommand argv {
101+
inputs io
102+
setAction (fun io -> ...)
103+
}
104+
105+
// Mixed into an `inputs` tuple alongside plain inputs:
106+
rootCommand argv {
107+
inputs (io, option<bool> "--verbose")
108+
setAction (fun (io, verbose) -> ...)
109+
}
110+
111+
// Nested inside another `input { }` — great for sharing common option groups
112+
// between commands (shared options are registered only once per command):
113+
let common = input {
114+
let! verbose = option<bool> "--verbose"
115+
and! config = option "--configuration" |> def "Release"
116+
return {| Verbose = verbose; Config = config |}
117+
}
118+
119+
let buildCmd = command "build" {
120+
input {
121+
let! common = common
122+
and! quick = option<bool> "--quick"
123+
return {| Common = common; Quick = quick |}
124+
}
125+
setAction (fun io -> ...)
126+
}
127+
```
128+
129+
A few rules to be aware of:
130+
131+
* Bind all sources in one `let!`/`and!` group. A second sequential `let!` is a compile error (`Bind` is deliberately not defined), because an input cannot depend on another input's parsed value — the full input set must be known before parsing.
132+
* Declare one `input { }` block per command, before `setAction`.
133+
* `configureParser` / `configureInvocation` must appear *before* an inline `input { }` block.
134+
135+
136+
66137
## Input API
67138
The new `Input` module contains functions for the underlying System.CommandLine `Option` and `Argument` properties.
68139
### Inputs

‎src/FSharp.SystemCommandLine/FSharp.SystemCommandLine.fsproj‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<PropertyGroup>
44
<TargetFrameworks>netstandard2.0;net6.0</TargetFrameworks>
55
<GenerateDocumentationFile>true</GenerateDocumentationFile>
6-
<Version>2.2.0-alpha.1</Version>
6+
<Version>2.2.0</Version>
77
<Description>F# computation expressions for working with the System.CommandLine API.</Description>
88
<Authors>Jordan Marr</Authors>
99
<PackageTags>F# fsharp System.CommandLine cli</PackageTags>

0 commit comments

Comments
 (0)