|
| 1 | +//------------------------------------------------------------------------------ |
| 2 | +// This code was generated by myriad. |
| 3 | +// Changes to this file will be lost when the code is regenerated. |
| 4 | +//------------------------------------------------------------------------------ |
| 5 | + |
| 6 | + |
| 7 | + |
| 8 | + |
| 9 | + |
| 10 | + |
| 11 | + |
| 12 | +namespace ConsumePlugin.OpensLeakVictim |
| 13 | + |
| 14 | +open WoofWare.Myriad.Plugins |
| 15 | + |
| 16 | +/// Methods to parse arguments for the type LeakArgs |
| 17 | +[<RequireQualifiedAccess ; CompilationRepresentation(CompilationRepresentationFlags.ModuleSuffix)>] |
| 18 | +module LeakArgs = |
| 19 | + type private ParseState_LeakArgs = |
| 20 | + /// Ready to consume a key or positional arg |
| 21 | + | AwaitingKey |
| 22 | + /// Waiting to receive a value for the key we've already consumed |
| 23 | + | AwaitingValue of key : string |
| 24 | + |
| 25 | + let parse' (getEnvironmentVariable : string -> string option) (args : string list) : LeakArgs = |
| 26 | + let ArgParser_errors = ResizeArray () |
| 27 | + |
| 28 | + let helpText () = |
| 29 | + [ (sprintf "%s int32%s%s" (sprintf "--%s" "foo") "" "") ] |> String.concat "\n" |
| 30 | + |
| 31 | + let parser_LeftoverArgs : string ResizeArray = ResizeArray () |
| 32 | + let mutable arg_0 : int option = None |
| 33 | + |
| 34 | + /// Processes the key-value pair, returning Error if no key was matched. |
| 35 | + /// If the key is an arg which can have arity 1, but throws when consuming that arg, we return Error(<the message>). |
| 36 | + /// This can nevertheless be a successful parse, e.g. when the key may have arity 0. |
| 37 | + let processKeyValue (key : string) (value : string) : Result<unit, string option> = |
| 38 | + if System.String.Equals (key, sprintf "--%s" "foo", System.StringComparison.OrdinalIgnoreCase) then |
| 39 | + match arg_0 with |
| 40 | + | Some x -> |
| 41 | + sprintf |
| 42 | + "Argument '%s' was supplied multiple times: %s and %s" |
| 43 | + (sprintf "--%s" "foo") |
| 44 | + (x.ToString ()) |
| 45 | + (value.ToString ()) |
| 46 | + |> ArgParser_errors.Add |
| 47 | + |
| 48 | + Ok () |
| 49 | + | None -> |
| 50 | + try |
| 51 | + arg_0 <- value |> (fun x -> System.Int32.Parse x) |> Some |
| 52 | + Ok () |
| 53 | + with _ as exc -> |
| 54 | + exc.Message |> Some |> Error |
| 55 | + else |
| 56 | + Error None |
| 57 | + |
| 58 | + /// Returns false if we didn't set a value. |
| 59 | + let setFlagValue (key : string) : bool = false |
| 60 | + |
| 61 | + let rec go (state : ParseState_LeakArgs) (args : string list) = |
| 62 | + match args with |
| 63 | + | [] -> |
| 64 | + match state with |
| 65 | + | ParseState_LeakArgs.AwaitingKey -> () |
| 66 | + | ParseState_LeakArgs.AwaitingValue key -> |
| 67 | + if setFlagValue key then |
| 68 | + () |
| 69 | + else |
| 70 | + sprintf |
| 71 | + "Trailing argument %s had no value. Use a double-dash to separate positional args from key-value args." |
| 72 | + key |
| 73 | + |> ArgParser_errors.Add |
| 74 | + | "--" :: rest -> |
| 75 | + match state with |
| 76 | + | ParseState_LeakArgs.AwaitingKey -> () |
| 77 | + | ParseState_LeakArgs.AwaitingValue key -> |
| 78 | + if setFlagValue key then |
| 79 | + () |
| 80 | + else |
| 81 | + sprintf |
| 82 | + "Trailing argument %s had no value. Use a double-dash to separate positional args from key-value args." |
| 83 | + key |
| 84 | + |> ArgParser_errors.Add |
| 85 | + |
| 86 | + parser_LeftoverArgs.AddRange (rest |> Seq.map (fun x -> x)) |
| 87 | + | arg :: args -> |
| 88 | + match state with |
| 89 | + | ParseState_LeakArgs.AwaitingKey -> |
| 90 | + if arg.StartsWith ("--", System.StringComparison.Ordinal) then |
| 91 | + if arg = "--help" then |
| 92 | + helpText () |> failwithf "Help text requested.\n%s" |
| 93 | + else |
| 94 | + let equals = arg.IndexOf (char 61) |
| 95 | + |
| 96 | + if equals < 0 then |
| 97 | + args |> go (ParseState_LeakArgs.AwaitingValue arg) |
| 98 | + else |
| 99 | + let key = arg.[0 .. equals - 1] |
| 100 | + let value = arg.[equals + 1 ..] |
| 101 | + |
| 102 | + match processKeyValue key value with |
| 103 | + | Ok () -> go ParseState_LeakArgs.AwaitingKey args |
| 104 | + | Error x -> |
| 105 | + match x with |
| 106 | + | None -> |
| 107 | + failwithf "Unable to process argument %s as key %s and value %s" arg key value |
| 108 | + | Some msg -> |
| 109 | + sprintf "%s (at arg %s)" msg arg |> ArgParser_errors.Add |
| 110 | + go ParseState_LeakArgs.AwaitingKey args |
| 111 | + else |
| 112 | + arg |> (fun x -> x) |> parser_LeftoverArgs.Add |
| 113 | + go ParseState_LeakArgs.AwaitingKey args |
| 114 | + | ParseState_LeakArgs.AwaitingValue key -> |
| 115 | + match processKeyValue key arg with |
| 116 | + | Ok () -> go ParseState_LeakArgs.AwaitingKey args |
| 117 | + | Error exc -> |
| 118 | + if setFlagValue key then |
| 119 | + go ParseState_LeakArgs.AwaitingKey (arg :: args) |
| 120 | + else |
| 121 | + match exc with |
| 122 | + | None -> |
| 123 | + failwithf "Unable to process supplied arg %s. Help text follows.\n%s" key (helpText ()) |
| 124 | + | Some msg -> msg |> ArgParser_errors.Add |
| 125 | + |
| 126 | + go ParseState_LeakArgs.AwaitingKey args |
| 127 | + |
| 128 | + let parser_LeftoverArgs = |
| 129 | + if 0 = parser_LeftoverArgs.Count then |
| 130 | + () |
| 131 | + else |
| 132 | + parser_LeftoverArgs |
| 133 | + |> String.concat " " |
| 134 | + |> sprintf "There were leftover args: %s" |
| 135 | + |> ArgParser_errors.Add |
| 136 | + |
| 137 | + Unchecked.defaultof<_> |
| 138 | + |
| 139 | + let arg_0 = |
| 140 | + match arg_0 with |
| 141 | + | None -> |
| 142 | + sprintf "Required argument '%s' received no value" (sprintf "--%s" "foo") |
| 143 | + |> ArgParser_errors.Add |
| 144 | + |
| 145 | + Unchecked.defaultof<_> |
| 146 | + | Some x -> x |
| 147 | + |
| 148 | + if 0 = ArgParser_errors.Count then |
| 149 | + { |
| 150 | + Foo = arg_0 |
| 151 | + } |
| 152 | + else |
| 153 | + ArgParser_errors |> String.concat "\n" |> failwithf "Errors during parse!\n%s" |
| 154 | + |
| 155 | + let parse (args : string list) : LeakArgs = |
| 156 | + parse' (System.Environment.GetEnvironmentVariable >> Option.ofObj) args |
0 commit comments