Skip to content

Commit fa19f2c

Browse files
committed
Updated to System.CommandLine v2.0.0 and fixed broken test in optionMaybe<'T> due to changed behavior in SCL.
1 parent 79d9389 commit fa19f2c

File tree

3 files changed

+43
-8
lines changed

3 files changed

+43
-8
lines changed

.claude/settings.local.json

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"permissions": {
3+
"allow": [
4+
"Bash(dotnet build:*)",
5+
"WebFetch(domain:github.com)",
6+
"WebSearch",
7+
"WebFetch(domain:stackoverflow.com)",
8+
"Bash(dotnet fsi:*)"
9+
],
10+
"deny": [],
11+
"ask": []
12+
}
13+
}

src/FSharp.SystemCommandLine/FSharp.SystemCommandLine.fsproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
</ItemGroup>
1919

2020
<ItemGroup>
21-
<PackageReference Include="System.CommandLine" Version="2.0.0-beta7.25380.108" />
21+
<PackageReference Include="System.CommandLine" Version="2.0.0" />
2222
</ItemGroup>
2323

2424
<ItemGroup>

src/FSharp.SystemCommandLine/Inputs.fs

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -160,18 +160,40 @@ module Input =
160160
|> editOption (fun o -> o.Recursive <- true)
161161

162162
/// Creates a named option of type `Option<'T option>` that defaults to `None`.
163-
let optionMaybe<'T> (name: string) =
163+
let optionMaybe<'T> (name: string) =
164164
let o = Option<'T option>(name, aliases = [||])
165165
let isBool = typeof<'T> = typeof<bool>
166-
o.CustomParser <- (fun result ->
167-
match result.Tokens |> Seq.toList with
168-
| [] when isBool -> true |> unbox<'T> |> Some
169-
| [] -> None
166+
167+
o.Arity <- ArgumentArity (0, 1)
168+
169+
o.CustomParser <- (fun result ->
170+
let tokens = result.Tokens |> Seq.toList
171+
match tokens with
172+
| [] ->
173+
// In rc.1, when there are no tokens, return None and let DefaultValueFactory handle it
174+
None
170175
| [ token ] -> MaybeParser.parseTokenValue token.Value
171176
| _ :: _ -> failwith "F# Option can only be used with a single argument."
172177
)
173-
o.Arity <- ArgumentArity (0, 1)
174-
o.DefaultValueFactory <- (fun _ -> None)
178+
179+
o.DefaultValueFactory <- (fun argResult ->
180+
// Check if this is a bool option that was explicitly specified without a value
181+
if isBool then
182+
// argResult.Parent should be the OptionResult
183+
match argResult.Parent with
184+
| :? Parsing.OptionResult as optionResult ->
185+
// Check if option was specified on command line without a value
186+
if optionResult.IdentifierTokenCount > 0 && argResult.Tokens.Count = 0 then
187+
// Option was specified (e.g., --flag) without a value
188+
true |> unbox<'T> |> Some
189+
else
190+
None
191+
| _ ->
192+
None
193+
else
194+
None
195+
)
196+
175197
ActionInput.OfOption<'T option> o
176198

177199
/// Creates a named argument. Example: `argument "file-name"`

0 commit comments

Comments
 (0)