{"cells":[{"cell_type":"markdown","metadata":{},"source":["# Runtime (Polyglot)"]},{"cell_type":"code","execution_count":1,"metadata":{"duration_ms":1490,"language":"fsharp"},"outputs":[],"source":["#r \"nuget: fsharp.control.asyncseq, 3.2.1\"\n","#r \"nuget: system.reactive, 6.0.1-preview.1\"\n","#r \"nuget: system.reactive.linq, 6.0.1-preview.1\"\n","#r \"nuget: argu, 6.2.4\""]},{"cell_type":"code","execution_count":2,"metadata":{"duration_ms":7781,"language":"fsharp"},"outputs":[],"source":["#!import ../../lib/fsharp/Notebooks.livemd"]},{"cell_type":"code","execution_count":3,"metadata":{"duration_ms":171,"language":"fsharp"},"outputs":[],"source":["#!import ../../lib/fsharp/Testing.livemd"]},{"cell_type":"code","execution_count":4,"metadata":{"duration_ms":33,"language":"fsharp"},"outputs":[],"source":["#!import ../../lib/fsharp/Common.fs"]},{"cell_type":"code","execution_count":5,"metadata":{"duration_ms":14,"language":"fsharp"},"outputs":[],"source":["#!import ../../lib/fsharp/CommonFSharp.fs"]},{"cell_type":"code","execution_count":6,"metadata":{"duration_ms":215,"language":"fsharp"},"outputs":[],"source":["#!import ../../lib/fsharp/Async.fs"]},{"cell_type":"code","execution_count":7,"metadata":{"duration_ms":68,"language":"fsharp"},"outputs":[],"source":["#!import ../../lib/fsharp/AsyncSeq.fs"]},{"cell_type":"code","execution_count":8,"metadata":{"duration_ms":32,"language":"fsharp"},"outputs":[],"source":["#!import ../../lib/fsharp/Runtime.fs"]},{"cell_type":"code","execution_count":9,"metadata":{"duration_ms":158,"language":"fsharp"},"outputs":[],"source":["#!import ../../lib/fsharp/FileSystem.fs"]},{"cell_type":"code","execution_count":10,"metadata":{"duration_ms":10,"language":"fsharp"},"outputs":[],"source":["#if !INTERACTIVE\n","open Lib\n","#endif"]},{"cell_type":"code","execution_count":11,"metadata":{"duration_ms":12,"language":"fsharp"},"outputs":[],"source":["open Common"]},{"cell_type":"code","execution_count":12,"metadata":{"duration_ms":24,"language":"fsharp"},"outputs":[],"source":["///- --test\n","\n","open SpiralFileSystem.Operators"]},{"cell_type":"markdown","metadata":{},"source":["## parseArgs"]},{"cell_type":"code","execution_count":13,"metadata":{"duration_ms":21,"language":"fsharp"},"outputs":[],"source":["let inline parseArgs<'T when 'T :> Argu.IArgParserTemplate> args =\n","    let assemblyName = System.Reflection.Assembly.GetEntryAssembly().GetName().Name\n","    let errorHandler : Argu.IExiter =\n","        if assemblyName = \"fsi\" // a notebook cell (Kino runs F# cells on dotnet fsi): report the error, don't exit\n","        then Argu.ExceptionExiter ()\n","        else Argu.ProcessExiter (function Argu.ErrorCode.HelpText -> None | _ -> Some System.ConsoleColor.Red)\n","\n","    let parser =\n","        Argu.ArgumentParser.Create<'T> (\n","            programName = $\"{assemblyName}{SpiralPlatform.get_executable_suffix ()}\",\n","            errorHandler = errorHandler\n","        )\n","\n","    parser.ParseCommandLine args"]},{"cell_type":"code","execution_count":14,"metadata":{"duration_ms":30,"language":"fsharp"},"outputs":[],"source":["///- --test\n","\n","[<RequireQualifiedAccess>]\n","type Arguments =\n","    | [<Argu.ArguAttributes.MainCommand; Argu.ArguAttributes.ExactlyOnce; Argu.ArguAttributes.Last>]\n","        Paths of paths : string list\n","\n","    interface Argu.IArgParserTemplate with\n","        member s.Usage =\n","            match s with\n","            | Paths _ -> nameof Paths"]},{"cell_type":"code","execution_count":15,"metadata":{"duration_ms":95,"language":"fsharp"},"outputs":[{"data":{"text/plain":["\"USAGE: fsi [--help] <paths>...\n","\n","PATHS:\n","\n","    <paths>...            Paths\n","\n","OPTIONS:\n","\n","    --help                display this list of options.\n","\""]},"execution_count":15,"metadata":{},"output_type":"execute_result"}],"source":["///- --test\n","\n","Argu.ArgumentParser.Create<Arguments>().PrintUsage ()"]},{"cell_type":"code","execution_count":16,"metadata":{"duration_ms":35,"language":"fsharp"},"outputs":[{"name":"stdout","output_type":"stream","text":["<fun:it@49274-1>\n","\n","\"Argu.ArguParseException: ERROR: missing parameter '<paths>...'.\n","USAGE: fsi [--help] <paths>...\n","\n","PATHS:\n","\n","    <paths>...            Paths\n","\n","OPTIONS:\n","\n","    --help                display this list of options.\n","\""]}],"source":["///- --test\n","\n","fun () -> parseArgs<Arguments> [||] |> ignore\n","|> _throwsC (fun ex _ ->\n","    SpiralSm.format_exception ex\n","    |> _stringContains \"Argu.ArguParseException: ERROR: missing parameter '<paths>...'.\"\n",")"]},{"cell_type":"code","execution_count":17,"metadata":{"duration_ms":15,"language":"fsharp"},"outputs":[],"source":["let inline parseAllArgs<'T when 'T :> Argu.IArgParserTemplate> args =\n","    args\n","    |> parseArgs<'T>\n","    |> fun results -> results.GetAllResults ()"]},{"cell_type":"code","execution_count":18,"metadata":{"duration_ms":57,"language":"fsharp"},"outputs":[{"name":"stdout","output_type":"stream","text":["[Paths [\"a b\"; \"c\"]]"]}],"source":["///- --test\n","\n","[<RequireQualifiedAccess>]\n","type Arguments =\n","    | [<Argu.ArguAttributes.MainCommand; Argu.ArguAttributes.ExactlyOnce; Argu.ArguAttributes.Last>]\n","        Paths of paths : string list\n","\n","    interface Argu.IArgParserTemplate with\n","        member s.Usage =\n","            match s with\n","            | Paths _ -> nameof Paths\n","\n","parseAllArgs<Arguments> [| \"a b\"; \"c\" |]\n","|> _assertEqual [ Arguments.Paths [ \"a b\"; \"c\" ] ]"]},{"cell_type":"code","execution_count":19,"metadata":{"duration_ms":17,"language":"fsharp"},"outputs":[],"source":["let inline parseArgsMap<'T when 'T :> Argu.IArgParserTemplate> args =\n","    args\n","    |> parseAllArgs<'T>\n","    |> List.groupBy CommonFSharp.getUnionCaseName<'T>\n","    |> Map.ofList"]},{"cell_type":"code","execution_count":20,"metadata":{"duration_ms":66,"language":"fsharp"},"outputs":[{"name":"stdout","output_type":"stream","text":["\n","\n","/home/runner/work/polyglot/polyglot/lib/fsharp/input.fsx(5,5): info FS3886: This list expression contains a single tuple element. Did you mean to use ';' instead of ',' to separate list elements?\n","\n","map [(\"Paths\", [Paths [\"a b\"; \"c\"]])]"]}],"source":["///- --test\n","\n","parseArgsMap<Arguments> [| \"a b\"; \"c\" |]\n","|> _assertEqual (\n","    [ nameof Arguments.Paths, [ Arguments.Paths [ \"a b\"; \"c\" ] ] ]\n","    |> Map.ofList\n",")"]}],"metadata":{"kernelspec":{"display_name":"Spiral","language":"spiral","name":"spiral"}},"nbformat":4,"nbformat_minor":5}