Handle URI in arg parser (#240)

This commit is contained in:
Patrick Stevens
2024-09-04 20:20:51 +01:00
committed by GitHub
parent 3a55ba1242
commit 8f9f933971
4 changed files with 24 additions and 17 deletions

View File

@@ -111,7 +111,7 @@ type ChildRecordWithPositional =
{
Thing1 : int
[<PositionalArgs>]
Thing2 : string list
Thing2 : Uri list
}
[<ArgParser true>]

View File

@@ -1801,11 +1801,11 @@ module ParentRecordChildPosArgParse =
[
(sprintf "--and-another bool%s%s" "" "")
(sprintf "--thing1 int32%s%s" "" "")
(sprintf "--thing2 string%s%s" " (positional args) (can be repeated)" "")
(sprintf "--thing2 URI%s%s" " (positional args) (can be repeated)" "")
]
|> String.concat "\n"
let arg_1 : string ResizeArray = ResizeArray ()
let arg_1 : Uri ResizeArray = ResizeArray ()
let mutable arg_2 : bool option = None
let mutable arg_0 : int option = None
@@ -1840,7 +1840,7 @@ module ParentRecordChildPosArgParse =
with _ as exc ->
exc.Message |> Some |> Error
else if System.String.Equals (key, "--thing2", System.StringComparison.OrdinalIgnoreCase) then
value |> (fun x -> x) |> arg_1.Add
value |> (fun x -> System.Uri x) |> arg_1.Add
() |> Ok
else
Error None
@@ -1873,7 +1873,7 @@ module ParentRecordChildPosArgParse =
"Trailing argument %s had no value. Use a double-dash to separate positional args from key-value args."
key
|> ArgParser_errors.Add
| "--" :: rest -> arg_1.AddRange (rest |> Seq.map (fun x -> x))
| "--" :: rest -> arg_1.AddRange (rest |> Seq.map (fun x -> System.Uri x))
| arg :: args ->
match state with
| ParseState_ParentRecordChildPos.AwaitingKey ->
@@ -1897,7 +1897,7 @@ module ParentRecordChildPosArgParse =
sprintf "%s (at arg %s)" msg arg |> ArgParser_errors.Add
go ParseState_ParentRecordChildPos.AwaitingKey args
else
arg |> (fun x -> x) |> arg_1.Add
arg |> (fun x -> System.Uri x) |> arg_1.Add
go ParseState_ParentRecordChildPos.AwaitingKey args
| ParseState_ParentRecordChildPos.AwaitingValue key ->
match processKeyValue key arg with

View File

@@ -367,18 +367,19 @@ Required argument '--exact' received no value"""
let parsed =
ParentRecordChildPos.parse'
getEnvVar
[ "--and-another=true" ; "--thing1=9" ; "--thing2=some" ; "--thing2=thing" ]
[
"--and-another=true"
"--thing1=9"
"--thing2=https://example.com"
"--thing2=http://example.com"
]
parsed
|> shouldEqual
{
Child =
{
Thing1 = 9
Thing2 = [ "some" ; "thing" ]
}
AndAnother = true
}
parsed.AndAnother |> shouldEqual true
parsed.Child.Thing1 |> shouldEqual 9
parsed.Child.Thing2
|> List.map (fun (x : Uri) -> x.ToString ())
|> shouldEqual [ "https://example.com/" ; "http://example.com/" ]
[<Test>]
let ``Can consume stacked record, child has no positionals, parent has positionals`` () =

View File

@@ -257,6 +257,12 @@ module internal ArgParserGenerator =
(SynExpr.createIdent "x")),
Accumulation.Required,
ty
| Uri ->
SynExpr.createLambda
"x"
(SynExpr.applyFunction (SynExpr.createLongIdent [ "System" ; "Uri" ]) (SynExpr.createIdent "x")),
Accumulation.Required,
ty
| TimeSpan ->
let parseExact =
attrs