From 8f9f933971aac9da101113ef77a4c32fa5d148d4 Mon Sep 17 00:00:00 2001 From: Patrick Stevens <3138005+Smaug123@users.noreply.github.com> Date: Wed, 4 Sep 2024 20:20:51 +0100 Subject: [PATCH] Handle URI in arg parser (#240) --- ConsumePlugin/Args.fs | 2 +- ConsumePlugin/GeneratedArgs.fs | 10 ++++---- .../TestArgParser/TestArgParser.fs | 23 ++++++++++--------- WoofWare.Myriad.Plugins/ArgParserGenerator.fs | 6 +++++ 4 files changed, 24 insertions(+), 17 deletions(-) diff --git a/ConsumePlugin/Args.fs b/ConsumePlugin/Args.fs index e8d086b..79e5345 100644 --- a/ConsumePlugin/Args.fs +++ b/ConsumePlugin/Args.fs @@ -111,7 +111,7 @@ type ChildRecordWithPositional = { Thing1 : int [] - Thing2 : string list + Thing2 : Uri list } [] diff --git a/ConsumePlugin/GeneratedArgs.fs b/ConsumePlugin/GeneratedArgs.fs index e519e65..babddae 100644 --- a/ConsumePlugin/GeneratedArgs.fs +++ b/ConsumePlugin/GeneratedArgs.fs @@ -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 diff --git a/WoofWare.Myriad.Plugins.Test/TestArgParser/TestArgParser.fs b/WoofWare.Myriad.Plugins.Test/TestArgParser/TestArgParser.fs index 1c3cfc3..4fd6ada 100644 --- a/WoofWare.Myriad.Plugins.Test/TestArgParser/TestArgParser.fs +++ b/WoofWare.Myriad.Plugins.Test/TestArgParser/TestArgParser.fs @@ -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/" ] [] let ``Can consume stacked record, child has no positionals, parent has positionals`` () = diff --git a/WoofWare.Myriad.Plugins/ArgParserGenerator.fs b/WoofWare.Myriad.Plugins/ArgParserGenerator.fs index 483f4d5..354d0f1 100644 --- a/WoofWare.Myriad.Plugins/ArgParserGenerator.fs +++ b/WoofWare.Myriad.Plugins/ArgParserGenerator.fs @@ -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