mirror of
https://github.com/Smaug123/WoofWare.Myriad
synced 2025-10-07 04:58:41 +00:00
Also pipe through parser in PositionalArgs true
(#259)
This commit is contained in:
@@ -204,6 +204,30 @@ type FlagsIntoPositionalArgs =
|
|||||||
GrabEverything : string list
|
GrabEverything : string list
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[<ArgParser true>]
|
||||||
|
type FlagsIntoPositionalArgsChoice =
|
||||||
|
{
|
||||||
|
A : string
|
||||||
|
[<PositionalArgs true>]
|
||||||
|
GrabEverything : Choice<string, string> list
|
||||||
|
}
|
||||||
|
|
||||||
|
[<ArgParser true>]
|
||||||
|
type FlagsIntoPositionalArgsInt =
|
||||||
|
{
|
||||||
|
A : string
|
||||||
|
[<PositionalArgs true>]
|
||||||
|
GrabEverything : int list
|
||||||
|
}
|
||||||
|
|
||||||
|
[<ArgParser true>]
|
||||||
|
type FlagsIntoPositionalArgsIntChoice =
|
||||||
|
{
|
||||||
|
A : string
|
||||||
|
[<PositionalArgs true>]
|
||||||
|
GrabEverything : Choice<int, int> list
|
||||||
|
}
|
||||||
|
|
||||||
[<ArgParser true>]
|
[<ArgParser true>]
|
||||||
type FlagsIntoPositionalArgs' =
|
type FlagsIntoPositionalArgs' =
|
||||||
{
|
{
|
||||||
|
@@ -3636,7 +3636,7 @@ module FlagsIntoPositionalArgsArgParse =
|
|||||||
if setFlagValue key then
|
if setFlagValue key then
|
||||||
go ParseState_FlagsIntoPositionalArgs.AwaitingKey (arg :: args)
|
go ParseState_FlagsIntoPositionalArgs.AwaitingKey (arg :: args)
|
||||||
else if true then
|
else if true then
|
||||||
key |> arg_1.Add
|
key |> (fun x -> x) |> arg_1.Add
|
||||||
go ParseState_FlagsIntoPositionalArgs.AwaitingKey (arg :: args)
|
go ParseState_FlagsIntoPositionalArgs.AwaitingKey (arg :: args)
|
||||||
else
|
else
|
||||||
match exc with
|
match exc with
|
||||||
@@ -3675,6 +3675,498 @@ open System
|
|||||||
open System.IO
|
open System.IO
|
||||||
open WoofWare.Myriad.Plugins
|
open WoofWare.Myriad.Plugins
|
||||||
|
|
||||||
|
/// Methods to parse arguments for the type FlagsIntoPositionalArgsChoice
|
||||||
|
[<AutoOpen>]
|
||||||
|
module FlagsIntoPositionalArgsChoiceArgParse =
|
||||||
|
type private ParseState_FlagsIntoPositionalArgsChoice =
|
||||||
|
/// Ready to consume a key or positional arg
|
||||||
|
| AwaitingKey
|
||||||
|
/// Waiting to receive a value for the key we've already consumed
|
||||||
|
| AwaitingValue of key : string
|
||||||
|
|
||||||
|
/// Extension methods for argument parsing
|
||||||
|
type FlagsIntoPositionalArgsChoice with
|
||||||
|
|
||||||
|
static member parse'
|
||||||
|
(getEnvironmentVariable : string -> string)
|
||||||
|
(args : string list)
|
||||||
|
: FlagsIntoPositionalArgsChoice
|
||||||
|
=
|
||||||
|
let ArgParser_errors = ResizeArray ()
|
||||||
|
|
||||||
|
let helpText () =
|
||||||
|
[
|
||||||
|
(sprintf "%s string%s%s" (sprintf "--%s" "a") "" "")
|
||||||
|
(sprintf
|
||||||
|
"%s string%s%s"
|
||||||
|
(sprintf "--%s" "grab-everything")
|
||||||
|
" (positional args) (can be repeated)"
|
||||||
|
"")
|
||||||
|
]
|
||||||
|
|> String.concat "\n"
|
||||||
|
|
||||||
|
let arg_1 : Choice<string, string> ResizeArray = ResizeArray ()
|
||||||
|
let mutable arg_0 : string option = None
|
||||||
|
|
||||||
|
/// Processes the key-value pair, returning Error if no key was matched.
|
||||||
|
/// If the key is an arg which can have arity 1, but throws when consuming that arg, we return Error(<the message>).
|
||||||
|
/// This can nevertheless be a successful parse, e.g. when the key may have arity 0.
|
||||||
|
let processKeyValue (key : string) (value : string) : Result<unit, string option> =
|
||||||
|
if System.String.Equals (key, sprintf "--%s" "a", System.StringComparison.OrdinalIgnoreCase) then
|
||||||
|
match arg_0 with
|
||||||
|
| Some x ->
|
||||||
|
sprintf
|
||||||
|
"Argument '%s' was supplied multiple times: %s and %s"
|
||||||
|
(sprintf "--%s" "a")
|
||||||
|
(x.ToString ())
|
||||||
|
(value.ToString ())
|
||||||
|
|> ArgParser_errors.Add
|
||||||
|
|
||||||
|
Ok ()
|
||||||
|
| None ->
|
||||||
|
try
|
||||||
|
arg_0 <- value |> (fun x -> x) |> Some
|
||||||
|
Ok ()
|
||||||
|
with _ as exc ->
|
||||||
|
exc.Message |> Some |> Error
|
||||||
|
else if
|
||||||
|
System.String.Equals (
|
||||||
|
key,
|
||||||
|
sprintf "--%s" "grab-everything",
|
||||||
|
System.StringComparison.OrdinalIgnoreCase
|
||||||
|
)
|
||||||
|
then
|
||||||
|
value |> (fun x -> x) |> Choice1Of2 |> arg_1.Add
|
||||||
|
() |> Ok
|
||||||
|
else
|
||||||
|
Error None
|
||||||
|
|
||||||
|
/// Returns false if we didn't set a value.
|
||||||
|
let setFlagValue (key : string) : bool = false
|
||||||
|
|
||||||
|
let rec go (state : ParseState_FlagsIntoPositionalArgsChoice) (args : string list) =
|
||||||
|
match args with
|
||||||
|
| [] ->
|
||||||
|
match state with
|
||||||
|
| ParseState_FlagsIntoPositionalArgsChoice.AwaitingKey -> ()
|
||||||
|
| ParseState_FlagsIntoPositionalArgsChoice.AwaitingValue key ->
|
||||||
|
if setFlagValue key then
|
||||||
|
()
|
||||||
|
else
|
||||||
|
sprintf
|
||||||
|
"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) |> Seq.map Choice2Of2)
|
||||||
|
| arg :: args ->
|
||||||
|
match state with
|
||||||
|
| ParseState_FlagsIntoPositionalArgsChoice.AwaitingKey ->
|
||||||
|
if arg.StartsWith ("--", System.StringComparison.Ordinal) then
|
||||||
|
if arg = "--help" then
|
||||||
|
helpText () |> failwithf "Help text requested.\n%s"
|
||||||
|
else
|
||||||
|
let equals = arg.IndexOf (char 61)
|
||||||
|
|
||||||
|
if equals < 0 then
|
||||||
|
args |> go (ParseState_FlagsIntoPositionalArgsChoice.AwaitingValue arg)
|
||||||
|
else
|
||||||
|
let key = arg.[0 .. equals - 1]
|
||||||
|
let value = arg.[equals + 1 ..]
|
||||||
|
|
||||||
|
match processKeyValue key value with
|
||||||
|
| Ok () -> go ParseState_FlagsIntoPositionalArgsChoice.AwaitingKey args
|
||||||
|
| Error x ->
|
||||||
|
if true then
|
||||||
|
arg |> (fun x -> x) |> Choice1Of2 |> arg_1.Add
|
||||||
|
go ParseState_FlagsIntoPositionalArgsChoice.AwaitingKey args
|
||||||
|
else
|
||||||
|
match x with
|
||||||
|
| None ->
|
||||||
|
failwithf
|
||||||
|
"Unable to process argument %s as key %s and value %s"
|
||||||
|
arg
|
||||||
|
key
|
||||||
|
value
|
||||||
|
| Some msg ->
|
||||||
|
sprintf "%s (at arg %s)" msg arg |> ArgParser_errors.Add
|
||||||
|
go ParseState_FlagsIntoPositionalArgsChoice.AwaitingKey args
|
||||||
|
else
|
||||||
|
arg |> (fun x -> x) |> Choice1Of2 |> arg_1.Add
|
||||||
|
go ParseState_FlagsIntoPositionalArgsChoice.AwaitingKey args
|
||||||
|
| ParseState_FlagsIntoPositionalArgsChoice.AwaitingValue key ->
|
||||||
|
match processKeyValue key arg with
|
||||||
|
| Ok () -> go ParseState_FlagsIntoPositionalArgsChoice.AwaitingKey args
|
||||||
|
| Error exc ->
|
||||||
|
if setFlagValue key then
|
||||||
|
go ParseState_FlagsIntoPositionalArgsChoice.AwaitingKey (arg :: args)
|
||||||
|
else if true then
|
||||||
|
key |> (fun x -> x) |> Choice1Of2 |> arg_1.Add
|
||||||
|
go ParseState_FlagsIntoPositionalArgsChoice.AwaitingKey (arg :: args)
|
||||||
|
else
|
||||||
|
match exc with
|
||||||
|
| None ->
|
||||||
|
failwithf
|
||||||
|
"Unable to process supplied arg %s. Help text follows.\n%s"
|
||||||
|
key
|
||||||
|
(helpText ())
|
||||||
|
| Some msg -> msg |> ArgParser_errors.Add
|
||||||
|
|
||||||
|
go ParseState_FlagsIntoPositionalArgsChoice.AwaitingKey args
|
||||||
|
let arg_1 = arg_1 |> Seq.toList
|
||||||
|
|
||||||
|
let arg_0 =
|
||||||
|
match arg_0 with
|
||||||
|
| None ->
|
||||||
|
sprintf "Required argument '%s' received no value" (sprintf "--%s" "a")
|
||||||
|
|> ArgParser_errors.Add
|
||||||
|
|
||||||
|
Unchecked.defaultof<_>
|
||||||
|
| Some x -> x
|
||||||
|
|
||||||
|
if 0 = ArgParser_errors.Count then
|
||||||
|
{
|
||||||
|
A = arg_0
|
||||||
|
GrabEverything = arg_1
|
||||||
|
}
|
||||||
|
else
|
||||||
|
ArgParser_errors |> String.concat "\n" |> failwithf "Errors during parse!\n%s"
|
||||||
|
|
||||||
|
static member parse (args : string list) : FlagsIntoPositionalArgsChoice =
|
||||||
|
FlagsIntoPositionalArgsChoice.parse' System.Environment.GetEnvironmentVariable args
|
||||||
|
namespace ConsumePlugin
|
||||||
|
|
||||||
|
open System
|
||||||
|
open System.IO
|
||||||
|
open WoofWare.Myriad.Plugins
|
||||||
|
|
||||||
|
/// Methods to parse arguments for the type FlagsIntoPositionalArgsInt
|
||||||
|
[<AutoOpen>]
|
||||||
|
module FlagsIntoPositionalArgsIntArgParse =
|
||||||
|
type private ParseState_FlagsIntoPositionalArgsInt =
|
||||||
|
/// Ready to consume a key or positional arg
|
||||||
|
| AwaitingKey
|
||||||
|
/// Waiting to receive a value for the key we've already consumed
|
||||||
|
| AwaitingValue of key : string
|
||||||
|
|
||||||
|
/// Extension methods for argument parsing
|
||||||
|
type FlagsIntoPositionalArgsInt with
|
||||||
|
|
||||||
|
static member parse'
|
||||||
|
(getEnvironmentVariable : string -> string)
|
||||||
|
(args : string list)
|
||||||
|
: FlagsIntoPositionalArgsInt
|
||||||
|
=
|
||||||
|
let ArgParser_errors = ResizeArray ()
|
||||||
|
|
||||||
|
let helpText () =
|
||||||
|
[
|
||||||
|
(sprintf "%s string%s%s" (sprintf "--%s" "a") "" "")
|
||||||
|
(sprintf
|
||||||
|
"%s int32%s%s"
|
||||||
|
(sprintf "--%s" "grab-everything")
|
||||||
|
" (positional args) (can be repeated)"
|
||||||
|
"")
|
||||||
|
]
|
||||||
|
|> String.concat "\n"
|
||||||
|
|
||||||
|
let arg_1 : int ResizeArray = ResizeArray ()
|
||||||
|
let mutable arg_0 : string option = None
|
||||||
|
|
||||||
|
/// Processes the key-value pair, returning Error if no key was matched.
|
||||||
|
/// If the key is an arg which can have arity 1, but throws when consuming that arg, we return Error(<the message>).
|
||||||
|
/// This can nevertheless be a successful parse, e.g. when the key may have arity 0.
|
||||||
|
let processKeyValue (key : string) (value : string) : Result<unit, string option> =
|
||||||
|
if System.String.Equals (key, sprintf "--%s" "a", System.StringComparison.OrdinalIgnoreCase) then
|
||||||
|
match arg_0 with
|
||||||
|
| Some x ->
|
||||||
|
sprintf
|
||||||
|
"Argument '%s' was supplied multiple times: %s and %s"
|
||||||
|
(sprintf "--%s" "a")
|
||||||
|
(x.ToString ())
|
||||||
|
(value.ToString ())
|
||||||
|
|> ArgParser_errors.Add
|
||||||
|
|
||||||
|
Ok ()
|
||||||
|
| None ->
|
||||||
|
try
|
||||||
|
arg_0 <- value |> (fun x -> x) |> Some
|
||||||
|
Ok ()
|
||||||
|
with _ as exc ->
|
||||||
|
exc.Message |> Some |> Error
|
||||||
|
else if
|
||||||
|
System.String.Equals (
|
||||||
|
key,
|
||||||
|
sprintf "--%s" "grab-everything",
|
||||||
|
System.StringComparison.OrdinalIgnoreCase
|
||||||
|
)
|
||||||
|
then
|
||||||
|
value |> (fun x -> System.Int32.Parse x) |> arg_1.Add
|
||||||
|
() |> Ok
|
||||||
|
else
|
||||||
|
Error None
|
||||||
|
|
||||||
|
/// Returns false if we didn't set a value.
|
||||||
|
let setFlagValue (key : string) : bool = false
|
||||||
|
|
||||||
|
let rec go (state : ParseState_FlagsIntoPositionalArgsInt) (args : string list) =
|
||||||
|
match args with
|
||||||
|
| [] ->
|
||||||
|
match state with
|
||||||
|
| ParseState_FlagsIntoPositionalArgsInt.AwaitingKey -> ()
|
||||||
|
| ParseState_FlagsIntoPositionalArgsInt.AwaitingValue key ->
|
||||||
|
if setFlagValue key then
|
||||||
|
()
|
||||||
|
else
|
||||||
|
sprintf
|
||||||
|
"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 -> System.Int32.Parse x))
|
||||||
|
| arg :: args ->
|
||||||
|
match state with
|
||||||
|
| ParseState_FlagsIntoPositionalArgsInt.AwaitingKey ->
|
||||||
|
if arg.StartsWith ("--", System.StringComparison.Ordinal) then
|
||||||
|
if arg = "--help" then
|
||||||
|
helpText () |> failwithf "Help text requested.\n%s"
|
||||||
|
else
|
||||||
|
let equals = arg.IndexOf (char 61)
|
||||||
|
|
||||||
|
if equals < 0 then
|
||||||
|
args |> go (ParseState_FlagsIntoPositionalArgsInt.AwaitingValue arg)
|
||||||
|
else
|
||||||
|
let key = arg.[0 .. equals - 1]
|
||||||
|
let value = arg.[equals + 1 ..]
|
||||||
|
|
||||||
|
match processKeyValue key value with
|
||||||
|
| Ok () -> go ParseState_FlagsIntoPositionalArgsInt.AwaitingKey args
|
||||||
|
| Error x ->
|
||||||
|
if true then
|
||||||
|
arg |> (fun x -> System.Int32.Parse x) |> arg_1.Add
|
||||||
|
go ParseState_FlagsIntoPositionalArgsInt.AwaitingKey args
|
||||||
|
else
|
||||||
|
match x with
|
||||||
|
| None ->
|
||||||
|
failwithf
|
||||||
|
"Unable to process argument %s as key %s and value %s"
|
||||||
|
arg
|
||||||
|
key
|
||||||
|
value
|
||||||
|
| Some msg ->
|
||||||
|
sprintf "%s (at arg %s)" msg arg |> ArgParser_errors.Add
|
||||||
|
go ParseState_FlagsIntoPositionalArgsInt.AwaitingKey args
|
||||||
|
else
|
||||||
|
arg |> (fun x -> System.Int32.Parse x) |> arg_1.Add
|
||||||
|
go ParseState_FlagsIntoPositionalArgsInt.AwaitingKey args
|
||||||
|
| ParseState_FlagsIntoPositionalArgsInt.AwaitingValue key ->
|
||||||
|
match processKeyValue key arg with
|
||||||
|
| Ok () -> go ParseState_FlagsIntoPositionalArgsInt.AwaitingKey args
|
||||||
|
| Error exc ->
|
||||||
|
if setFlagValue key then
|
||||||
|
go ParseState_FlagsIntoPositionalArgsInt.AwaitingKey (arg :: args)
|
||||||
|
else if true then
|
||||||
|
key |> (fun x -> System.Int32.Parse x) |> arg_1.Add
|
||||||
|
go ParseState_FlagsIntoPositionalArgsInt.AwaitingKey (arg :: args)
|
||||||
|
else
|
||||||
|
match exc with
|
||||||
|
| None ->
|
||||||
|
failwithf
|
||||||
|
"Unable to process supplied arg %s. Help text follows.\n%s"
|
||||||
|
key
|
||||||
|
(helpText ())
|
||||||
|
| Some msg -> msg |> ArgParser_errors.Add
|
||||||
|
|
||||||
|
go ParseState_FlagsIntoPositionalArgsInt.AwaitingKey args
|
||||||
|
let arg_1 = arg_1 |> Seq.toList
|
||||||
|
|
||||||
|
let arg_0 =
|
||||||
|
match arg_0 with
|
||||||
|
| None ->
|
||||||
|
sprintf "Required argument '%s' received no value" (sprintf "--%s" "a")
|
||||||
|
|> ArgParser_errors.Add
|
||||||
|
|
||||||
|
Unchecked.defaultof<_>
|
||||||
|
| Some x -> x
|
||||||
|
|
||||||
|
if 0 = ArgParser_errors.Count then
|
||||||
|
{
|
||||||
|
A = arg_0
|
||||||
|
GrabEverything = arg_1
|
||||||
|
}
|
||||||
|
else
|
||||||
|
ArgParser_errors |> String.concat "\n" |> failwithf "Errors during parse!\n%s"
|
||||||
|
|
||||||
|
static member parse (args : string list) : FlagsIntoPositionalArgsInt =
|
||||||
|
FlagsIntoPositionalArgsInt.parse' System.Environment.GetEnvironmentVariable args
|
||||||
|
namespace ConsumePlugin
|
||||||
|
|
||||||
|
open System
|
||||||
|
open System.IO
|
||||||
|
open WoofWare.Myriad.Plugins
|
||||||
|
|
||||||
|
/// Methods to parse arguments for the type FlagsIntoPositionalArgsIntChoice
|
||||||
|
[<AutoOpen>]
|
||||||
|
module FlagsIntoPositionalArgsIntChoiceArgParse =
|
||||||
|
type private ParseState_FlagsIntoPositionalArgsIntChoice =
|
||||||
|
/// Ready to consume a key or positional arg
|
||||||
|
| AwaitingKey
|
||||||
|
/// Waiting to receive a value for the key we've already consumed
|
||||||
|
| AwaitingValue of key : string
|
||||||
|
|
||||||
|
/// Extension methods for argument parsing
|
||||||
|
type FlagsIntoPositionalArgsIntChoice with
|
||||||
|
|
||||||
|
static member parse'
|
||||||
|
(getEnvironmentVariable : string -> string)
|
||||||
|
(args : string list)
|
||||||
|
: FlagsIntoPositionalArgsIntChoice
|
||||||
|
=
|
||||||
|
let ArgParser_errors = ResizeArray ()
|
||||||
|
|
||||||
|
let helpText () =
|
||||||
|
[
|
||||||
|
(sprintf "%s string%s%s" (sprintf "--%s" "a") "" "")
|
||||||
|
(sprintf
|
||||||
|
"%s int32%s%s"
|
||||||
|
(sprintf "--%s" "grab-everything")
|
||||||
|
" (positional args) (can be repeated)"
|
||||||
|
"")
|
||||||
|
]
|
||||||
|
|> String.concat "\n"
|
||||||
|
|
||||||
|
let arg_1 : Choice<int, int> ResizeArray = ResizeArray ()
|
||||||
|
let mutable arg_0 : string option = None
|
||||||
|
|
||||||
|
/// Processes the key-value pair, returning Error if no key was matched.
|
||||||
|
/// If the key is an arg which can have arity 1, but throws when consuming that arg, we return Error(<the message>).
|
||||||
|
/// This can nevertheless be a successful parse, e.g. when the key may have arity 0.
|
||||||
|
let processKeyValue (key : string) (value : string) : Result<unit, string option> =
|
||||||
|
if System.String.Equals (key, sprintf "--%s" "a", System.StringComparison.OrdinalIgnoreCase) then
|
||||||
|
match arg_0 with
|
||||||
|
| Some x ->
|
||||||
|
sprintf
|
||||||
|
"Argument '%s' was supplied multiple times: %s and %s"
|
||||||
|
(sprintf "--%s" "a")
|
||||||
|
(x.ToString ())
|
||||||
|
(value.ToString ())
|
||||||
|
|> ArgParser_errors.Add
|
||||||
|
|
||||||
|
Ok ()
|
||||||
|
| None ->
|
||||||
|
try
|
||||||
|
arg_0 <- value |> (fun x -> x) |> Some
|
||||||
|
Ok ()
|
||||||
|
with _ as exc ->
|
||||||
|
exc.Message |> Some |> Error
|
||||||
|
else if
|
||||||
|
System.String.Equals (
|
||||||
|
key,
|
||||||
|
sprintf "--%s" "grab-everything",
|
||||||
|
System.StringComparison.OrdinalIgnoreCase
|
||||||
|
)
|
||||||
|
then
|
||||||
|
value |> (fun x -> System.Int32.Parse x) |> Choice1Of2 |> arg_1.Add
|
||||||
|
() |> Ok
|
||||||
|
else
|
||||||
|
Error None
|
||||||
|
|
||||||
|
/// Returns false if we didn't set a value.
|
||||||
|
let setFlagValue (key : string) : bool = false
|
||||||
|
|
||||||
|
let rec go (state : ParseState_FlagsIntoPositionalArgsIntChoice) (args : string list) =
|
||||||
|
match args with
|
||||||
|
| [] ->
|
||||||
|
match state with
|
||||||
|
| ParseState_FlagsIntoPositionalArgsIntChoice.AwaitingKey -> ()
|
||||||
|
| ParseState_FlagsIntoPositionalArgsIntChoice.AwaitingValue key ->
|
||||||
|
if setFlagValue key then
|
||||||
|
()
|
||||||
|
else
|
||||||
|
sprintf
|
||||||
|
"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 -> System.Int32.Parse x) |> Seq.map Choice2Of2)
|
||||||
|
| arg :: args ->
|
||||||
|
match state with
|
||||||
|
| ParseState_FlagsIntoPositionalArgsIntChoice.AwaitingKey ->
|
||||||
|
if arg.StartsWith ("--", System.StringComparison.Ordinal) then
|
||||||
|
if arg = "--help" then
|
||||||
|
helpText () |> failwithf "Help text requested.\n%s"
|
||||||
|
else
|
||||||
|
let equals = arg.IndexOf (char 61)
|
||||||
|
|
||||||
|
if equals < 0 then
|
||||||
|
args |> go (ParseState_FlagsIntoPositionalArgsIntChoice.AwaitingValue arg)
|
||||||
|
else
|
||||||
|
let key = arg.[0 .. equals - 1]
|
||||||
|
let value = arg.[equals + 1 ..]
|
||||||
|
|
||||||
|
match processKeyValue key value with
|
||||||
|
| Ok () -> go ParseState_FlagsIntoPositionalArgsIntChoice.AwaitingKey args
|
||||||
|
| Error x ->
|
||||||
|
if true then
|
||||||
|
arg |> (fun x -> System.Int32.Parse x) |> Choice1Of2 |> arg_1.Add
|
||||||
|
go ParseState_FlagsIntoPositionalArgsIntChoice.AwaitingKey args
|
||||||
|
else
|
||||||
|
match x with
|
||||||
|
| None ->
|
||||||
|
failwithf
|
||||||
|
"Unable to process argument %s as key %s and value %s"
|
||||||
|
arg
|
||||||
|
key
|
||||||
|
value
|
||||||
|
| Some msg ->
|
||||||
|
sprintf "%s (at arg %s)" msg arg |> ArgParser_errors.Add
|
||||||
|
go ParseState_FlagsIntoPositionalArgsIntChoice.AwaitingKey args
|
||||||
|
else
|
||||||
|
arg |> (fun x -> System.Int32.Parse x) |> Choice1Of2 |> arg_1.Add
|
||||||
|
go ParseState_FlagsIntoPositionalArgsIntChoice.AwaitingKey args
|
||||||
|
| ParseState_FlagsIntoPositionalArgsIntChoice.AwaitingValue key ->
|
||||||
|
match processKeyValue key arg with
|
||||||
|
| Ok () -> go ParseState_FlagsIntoPositionalArgsIntChoice.AwaitingKey args
|
||||||
|
| Error exc ->
|
||||||
|
if setFlagValue key then
|
||||||
|
go ParseState_FlagsIntoPositionalArgsIntChoice.AwaitingKey (arg :: args)
|
||||||
|
else if true then
|
||||||
|
key |> (fun x -> System.Int32.Parse x) |> Choice1Of2 |> arg_1.Add
|
||||||
|
go ParseState_FlagsIntoPositionalArgsIntChoice.AwaitingKey (arg :: args)
|
||||||
|
else
|
||||||
|
match exc with
|
||||||
|
| None ->
|
||||||
|
failwithf
|
||||||
|
"Unable to process supplied arg %s. Help text follows.\n%s"
|
||||||
|
key
|
||||||
|
(helpText ())
|
||||||
|
| Some msg -> msg |> ArgParser_errors.Add
|
||||||
|
|
||||||
|
go ParseState_FlagsIntoPositionalArgsIntChoice.AwaitingKey args
|
||||||
|
let arg_1 = arg_1 |> Seq.toList
|
||||||
|
|
||||||
|
let arg_0 =
|
||||||
|
match arg_0 with
|
||||||
|
| None ->
|
||||||
|
sprintf "Required argument '%s' received no value" (sprintf "--%s" "a")
|
||||||
|
|> ArgParser_errors.Add
|
||||||
|
|
||||||
|
Unchecked.defaultof<_>
|
||||||
|
| Some x -> x
|
||||||
|
|
||||||
|
if 0 = ArgParser_errors.Count then
|
||||||
|
{
|
||||||
|
A = arg_0
|
||||||
|
GrabEverything = arg_1
|
||||||
|
}
|
||||||
|
else
|
||||||
|
ArgParser_errors |> String.concat "\n" |> failwithf "Errors during parse!\n%s"
|
||||||
|
|
||||||
|
static member parse (args : string list) : FlagsIntoPositionalArgsIntChoice =
|
||||||
|
FlagsIntoPositionalArgsIntChoice.parse' System.Environment.GetEnvironmentVariable args
|
||||||
|
namespace ConsumePlugin
|
||||||
|
|
||||||
|
open System
|
||||||
|
open System.IO
|
||||||
|
open WoofWare.Myriad.Plugins
|
||||||
|
|
||||||
/// Methods to parse arguments for the type FlagsIntoPositionalArgs'
|
/// Methods to parse arguments for the type FlagsIntoPositionalArgs'
|
||||||
[<AutoOpen>]
|
[<AutoOpen>]
|
||||||
module FlagsIntoPositionalArgs'ArgParse =
|
module FlagsIntoPositionalArgs'ArgParse =
|
||||||
@@ -3800,7 +4292,7 @@ module FlagsIntoPositionalArgs'ArgParse =
|
|||||||
if setFlagValue key then
|
if setFlagValue key then
|
||||||
go ParseState_FlagsIntoPositionalArgs'.AwaitingKey (arg :: args)
|
go ParseState_FlagsIntoPositionalArgs'.AwaitingKey (arg :: args)
|
||||||
else if false then
|
else if false then
|
||||||
key |> arg_1.Add
|
key |> (fun x -> x) |> arg_1.Add
|
||||||
go ParseState_FlagsIntoPositionalArgs'.AwaitingKey (arg :: args)
|
go ParseState_FlagsIntoPositionalArgs'.AwaitingKey (arg :: args)
|
||||||
else
|
else
|
||||||
match exc with
|
match exc with
|
||||||
|
@@ -640,6 +640,45 @@ Required argument '--exact' received no value"""
|
|||||||
GrabEverything = [ "--c" ; "hi" ; "--help" ]
|
GrabEverything = [ "--c" ; "hi" ; "--help" ]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[<Test>]
|
||||||
|
let ``Can collect non-help args into positional args with Choice`` () =
|
||||||
|
let getEnvVar (_ : string) = failwith "do not call"
|
||||||
|
|
||||||
|
FlagsIntoPositionalArgsChoice.parse' getEnvVar [ "--a" ; "foo" ; "--b=false" ; "--c" ; "hi" ; "--" ; "--help" ]
|
||||||
|
|> shouldEqual
|
||||||
|
{
|
||||||
|
A = "foo"
|
||||||
|
GrabEverything =
|
||||||
|
[
|
||||||
|
Choice1Of2 "--b=false"
|
||||||
|
Choice1Of2 "--c"
|
||||||
|
Choice1Of2 "hi"
|
||||||
|
Choice2Of2 "--help"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
|
||||||
|
[<Test>]
|
||||||
|
let ``Can collect non-help args into positional args, and we parse on the way`` () =
|
||||||
|
let getEnvVar (_ : string) = failwith "do not call"
|
||||||
|
|
||||||
|
FlagsIntoPositionalArgsInt.parse' getEnvVar [ "3" ; "--a" ; "foo" ; "5" ; "--" ; "98" ]
|
||||||
|
|> shouldEqual
|
||||||
|
{
|
||||||
|
A = "foo"
|
||||||
|
GrabEverything = [ 3 ; 5 ; 98 ]
|
||||||
|
}
|
||||||
|
|
||||||
|
[<Test>]
|
||||||
|
let ``Can collect non-help args into positional args with Choice, and we parse on the way`` () =
|
||||||
|
let getEnvVar (_ : string) = failwith "do not call"
|
||||||
|
|
||||||
|
FlagsIntoPositionalArgsIntChoice.parse' getEnvVar [ "3" ; "--a" ; "foo" ; "5" ; "--" ; "98" ]
|
||||||
|
|> shouldEqual
|
||||||
|
{
|
||||||
|
A = "foo"
|
||||||
|
GrabEverything = [ Choice1Of2 3 ; Choice1Of2 5 ; Choice2Of2 98 ]
|
||||||
|
}
|
||||||
|
|
||||||
[<Test>]
|
[<Test>]
|
||||||
let ``Can refuse to collect non-help args with PositionalArgs false`` () =
|
let ``Can refuse to collect non-help args with PositionalArgs false`` () =
|
||||||
let getEnvVar (_ : string) = failwith "do not call"
|
let getEnvVar (_ : string) = failwith "do not call"
|
||||||
|
@@ -1140,6 +1140,12 @@ module internal ArgParserGenerator =
|
|||||||
| Some includeFlagLike ->
|
| Some includeFlagLike ->
|
||||||
[
|
[
|
||||||
SynExpr.createIdent "key"
|
SynExpr.createIdent "key"
|
||||||
|
|> SynExpr.pipeThroughFunction leftoverArgParser
|
||||||
|
|> fun i ->
|
||||||
|
match leftoverArgAcc with
|
||||||
|
| ChoicePositional.Choice _ ->
|
||||||
|
i |> SynExpr.pipeThroughFunction (SynExpr.createIdent "Choice1Of2")
|
||||||
|
| ChoicePositional.Normal _ -> i
|
||||||
|> SynExpr.pipeThroughFunction (SynExpr.createLongIdent' [ leftoverArgs ; Ident.create "Add" ])
|
|> SynExpr.pipeThroughFunction (SynExpr.createLongIdent' [ leftoverArgs ; Ident.create "Add" ])
|
||||||
|
|
||||||
SynExpr.createIdent "go"
|
SynExpr.createIdent "go"
|
||||||
|
Reference in New Issue
Block a user