From 8735af77df840190d9cf9eed2cbe3d949113b5e2 Mon Sep 17 00:00:00 2001 From: Simon Michael Date: Tue, 9 May 2023 10:08:52 -1000 Subject: [PATCH] lib: add toggleopt, for flags that toggle when repeated --- hledger-lib/Hledger/Data/RawOptions.hs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/hledger-lib/Hledger/Data/RawOptions.hs b/hledger-lib/Hledger/Data/RawOptions.hs index a498c7813..c2ad93c28 100644 --- a/hledger-lib/Hledger/Data/RawOptions.hs +++ b/hledger-lib/Hledger/Data/RawOptions.hs @@ -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...