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 Thing1 : int
[<PositionalArgs>] [<PositionalArgs>]
Thing2 : string list Thing2 : Uri list
} }
[<ArgParser true>] [<ArgParser true>]

View File

@@ -1801,11 +1801,11 @@ module ParentRecordChildPosArgParse =
[ [
(sprintf "--and-another bool%s%s" "" "") (sprintf "--and-another bool%s%s" "" "")
(sprintf "--thing1 int32%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" |> 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_2 : bool option = None
let mutable arg_0 : int option = None let mutable arg_0 : int option = None
@@ -1840,7 +1840,7 @@ module ParentRecordChildPosArgParse =
with _ as exc -> with _ as exc ->
exc.Message |> Some |> Error exc.Message |> Some |> Error
else if System.String.Equals (key, "--thing2", System.StringComparison.OrdinalIgnoreCase) then 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 () |> Ok
else else
Error None 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." "Trailing argument %s had no value. Use a double-dash to separate positional args from key-value args."
key key
|> ArgParser_errors.Add |> 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 -> | arg :: args ->
match state with match state with
| ParseState_ParentRecordChildPos.AwaitingKey -> | ParseState_ParentRecordChildPos.AwaitingKey ->
@@ -1897,7 +1897,7 @@ module ParentRecordChildPosArgParse =
sprintf "%s (at arg %s)" msg arg |> ArgParser_errors.Add sprintf "%s (at arg %s)" msg arg |> ArgParser_errors.Add
go ParseState_ParentRecordChildPos.AwaitingKey args go ParseState_ParentRecordChildPos.AwaitingKey args
else else
arg |> (fun x -> x) |> arg_1.Add arg |> (fun x -> System.Uri x) |> arg_1.Add
go ParseState_ParentRecordChildPos.AwaitingKey args go ParseState_ParentRecordChildPos.AwaitingKey args
| ParseState_ParentRecordChildPos.AwaitingValue key -> | ParseState_ParentRecordChildPos.AwaitingValue key ->
match processKeyValue key arg with match processKeyValue key arg with

View File

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

View File

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