Skip to content

Commit

Permalink
Fsdk,Fsdk.Tests(ArgsParsing): error case
Browse files Browse the repository at this point in the history
  • Loading branch information
knocte committed Aug 23, 2023
1 parent 3d087c0 commit d0524bd
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
16 changes: 16 additions & 0 deletions Fsdk.Tests/ArgsParsing.fs
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,19 @@ type ArgsParsing() =
Assert.That(Seq.item 0 postFlags, Is.EqualTo "--someLongPostFlag3")
Assert.That(Seq.item 1 postFlags, Is.EqualTo "-f4")
| _ -> Assert.Fail "res was not ArgsParsing.BothFlags subtype"

[<Test>]
member __.errors() =
let commandLine = "someProgramThatDoesNotMatchPredicate".Split(' ')

let res =
Misc.ParseArgs
commandLine
(fun arg ->
arg = "someProgramArgThatDoesNotMatchProgramUsedInCommandLine"
)

match res with
| Misc.ArgsParsed.ErrorDetectingProgram -> Assert.Pass()
| _ ->
Assert.Fail "res was not ArgsParsing.ErrorDetectingProgram subtype"
7 changes: 6 additions & 1 deletion Fsdk/Misc.fs
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ module Misc =
#endif

type ArgsParsed =
| ErrorDetectingProgram
| NoArgsWhatsoever
| ArgWithoutFlags of string
| OnlyFlags of List<string>
Expand All @@ -109,13 +110,15 @@ module Misc =
(acc: ArgsParsed)
: ArgsParsed =
match theArgs with
| [] -> acc
| [] -> ErrorDetectingProgram
| head :: tail ->
if predicateToRecognizeProgram head then
acc
elif head.StartsWith "--" || head.StartsWith "-" then
let newAcc =
match acc with
| ErrorDetectingProgram ->
failwith "Error case should not be used as acc"
| NoArgsWhatsoever ->
ArgsParsed.OnlyFlags(head :: List.Empty)
| ArgWithoutFlags arg ->
Expand All @@ -136,6 +139,8 @@ module Misc =
innerFunc tail newAcc
else
match acc with
| ErrorDetectingProgram ->
failwith "Error case should not be used as acc"
| NoArgsWhatsoever -> innerFunc tail (ArgWithoutFlags head)
| ArgsParsed.OnlyFlags flagsSoFar ->
let newAcc =
Expand Down

0 comments on commit d0524bd

Please sign in to comment.