lib: add toggleopt, for flags that toggle when repeated

This commit is contained in:
Simon Michael 2023-05-09 10:08:52 -10:00
parent 2d03148d7f
commit 8735af77df

View File

@ -14,6 +14,7 @@ module Hledger.Data.RawOptions (
unsetboolopt,
appendopts,
boolopt,
toggleopt,
choiceopt,
collectopts,
stringopt,
@ -59,6 +60,11 @@ appendopts new = overRawOpts (++new)
boolopt :: String -> RawOpts -> Bool
boolopt name = isJust . lookup name . unRawOpts
-- | Like boolopt, except if the flag is repeated on the command line it toggles the value.
-- An even number of repetitions is equivalent to none.
toggleopt :: String -> RawOpts -> Bool
toggleopt name rawopts = odd $ length [ n | (n,_) <- unRawOpts rawopts, n==name]
-- | From a list of RawOpts, get the last one (ie the right-most on the command line)
-- for which the given predicate returns a Just value.
-- Useful for exclusive choice flags like --daily|--weekly|--quarterly...