From 0842fd38f2e7f6dbcb35a4235f5054c94b457207 Mon Sep 17 00:00:00 2001 From: "Andres G. Aragoneses" Date: Wed, 23 Aug 2023 15:08:27 +0800 Subject: [PATCH] Fsdk(,.Tests): make 2nd test pass We had to modify the API to pass a predicate, to be able to distinguish the program from the main argument. --- Fsdk.Tests/ArgsParsing.fs | 6 ++++-- Fsdk/Misc.fs | 41 ++++++++++++++++++++++++++++++++------- 2 files changed, 38 insertions(+), 9 deletions(-) diff --git a/Fsdk.Tests/ArgsParsing.fs b/Fsdk.Tests/ArgsParsing.fs index 0b7ec93..d5c28ff 100644 --- a/Fsdk.Tests/ArgsParsing.fs +++ b/Fsdk.Tests/ArgsParsing.fs @@ -10,7 +10,9 @@ type ArgsParsing() = [] member __.``simplest flags usage``() = let commandLine = "someProgram --someLongFlag1 -f2".Split(' ') - let res: Misc.ArgsParsed = Misc.ParseArgs commandLine + + let res: Misc.ArgsParsed = + Misc.ParseArgs commandLine (fun arg -> arg = "someProgram") match res with | Misc.ArgsParsed.OnlyFlags flags -> @@ -25,7 +27,7 @@ type ArgsParsing() = "someProgram --someLongPreFlag1 -f2 someNonFlagArg --someLongPostFlag3 -f4" .Split(' ') - let res = Misc.ParseArgs commandLine + let res = Misc.ParseArgs commandLine (fun arg -> arg = "someProgram") match res with | Misc.ArgsParsed.BothFlags(preFlags, arg, postFlags) -> diff --git a/Fsdk/Misc.fs b/Fsdk/Misc.fs index e70acb1..2e487c9 100644 --- a/Fsdk/Misc.fs +++ b/Fsdk/Misc.fs @@ -98,20 +98,47 @@ module Misc = | OnlyFlags of seq | BothFlags of seq * string * seq - let ParseArgs(args: array) : ArgsParsed = - let rec innerFunc (theArgs: List) acc = + let ParseArgs + (args: array) + (predicateToRecognizeProgram: string -> bool) + : ArgsParsed = + let rec innerFunc + (theArgs: List) + (acc: ArgsParsed) + : ArgsParsed = match theArgs with | [] -> acc | head :: tail -> - if head.StartsWith "--" || head.StartsWith "-" then - innerFunc tail (head :: acc) - else + if predicateToRecognizeProgram head then acc + elif head.StartsWith "--" || head.StartsWith "-" then + let newAcc = + match acc with + | ArgsParsed.OnlyFlags flagsSoFar -> + ArgsParsed.OnlyFlags( + head :: (flagsSoFar |> List.ofSeq) |> Seq.ofList + ) + | ArgsParsed.BothFlags(preFlags, arg, postFlags) -> + ArgsParsed.BothFlags( + (head :: (preFlags |> List.ofSeq) |> Seq.ofList), + arg, + postFlags + ) + + innerFunc tail newAcc + else + match acc with + | ArgsParsed.OnlyFlags flagsSoFar -> + let newAcc = + ArgsParsed.BothFlags(Seq.empty, head, flagsSoFar) + + innerFunc tail newAcc + | bothFlags -> bothFlags let revArgs = Array.rev args |> List.ofArray - let args = innerFunc revArgs List.Empty + let parsedArgs = innerFunc revArgs (ArgsParsed.OnlyFlags List.Empty) - ArgsParsed.OnlyFlags args + parsedArgs let FsxOnlyArguments() = let cmdLineArgs = Environment.GetCommandLineArgs() |> List.ofSeq