Skip to content

Commit

Permalink
Handle URI in arg parser (#240)
Browse files Browse the repository at this point in the history
  • Loading branch information
Smaug123 authored Sep 4, 2024
1 parent 3a55ba1 commit 8f9f933
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 17 deletions.
2 changes: 1 addition & 1 deletion ConsumePlugin/Args.fs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ type ChildRecordWithPositional =
{
Thing1 : int
[<PositionalArgs>]
Thing2 : string list
Thing2 : Uri list
}

[<ArgParser true>]
Expand Down
10 changes: 5 additions & 5 deletions ConsumePlugin/GeneratedArgs.fs
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 ->
Expand All @@ -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
Expand Down
23 changes: 12 additions & 11 deletions WoofWare.Myriad.Plugins.Test/TestArgParser/TestArgParser.fs
Original file line number Diff line number Diff line change
Expand Up @@ -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`` () =
Expand Down
6 changes: 6 additions & 0 deletions WoofWare.Myriad.Plugins/ArgParserGenerator.fs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 8f9f933

Please sign in to comment.