Skip to content

Commit

Permalink
Fsdk(,.Tests): make 2nd test pass
Browse files Browse the repository at this point in the history
We had to modify the API to pass a predicate, to be able to
distinguish the program from the main argument.
  • Loading branch information
knocte committed Aug 23, 2023
1 parent 08eeb29 commit 0842fd3
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 9 deletions.
6 changes: 4 additions & 2 deletions Fsdk.Tests/ArgsParsing.fs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ type ArgsParsing() =
[<Test>]
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 ->
Expand All @@ -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) ->
Expand Down
41 changes: 34 additions & 7 deletions Fsdk/Misc.fs
Original file line number Diff line number Diff line change
Expand Up @@ -98,20 +98,47 @@ module Misc =
| OnlyFlags of seq<string>
| BothFlags of seq<string> * string * seq<string>

let ParseArgs(args: array<string>) : ArgsParsed =
let rec innerFunc (theArgs: List<string>) acc =
let ParseArgs
(args: array<string>)
(predicateToRecognizeProgram: string -> bool)
: ArgsParsed =
let rec innerFunc
(theArgs: List<string>)
(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
Expand Down

0 comments on commit 0842fd3

Please sign in to comment.