dev: cleanups

This commit is contained in:
Simon Michael 2024-10-19 16:15:45 -10:00
parent 3306abfd43
commit f5b10b2d40
2 changed files with 14 additions and 17 deletions

View File

@ -252,18 +252,6 @@ runPager s = do
s s
#endif #endif
-- | Given a list of arguments, split any of the form --flag=VAL or -fVAL
-- into separate list items. Multiple valueless short flags joined together is not supported.
splitFlagsAndVals :: [String] -> [String]
splitFlagsAndVals = concatMap $
\case
a@('-':'-':_) | '=' `elem` a -> let (x,y) = break (=='=') a in [x, drop 1 y]
a@('-':f:_:_) | not $ f=='-' -> [take 2 a, drop 2 a]
a -> [a]
toFlag [c] = ['-',c]
toFlag s = '-':'-':s
-- | Given one or more long or short option names, read the rightmost value of this option from the command line arguments. -- | Given one or more long or short option names, read the rightmost value of this option from the command line arguments.
-- If the value is missing raise an error. -- If the value is missing raise an error.
getOpt :: [String] -> IO (Maybe String) getOpt :: [String] -> IO (Maybe String)
@ -276,6 +264,19 @@ getOpt names = do
([],flag:_) -> error' $ flag <> " requires a value" ([],flag:_) -> error' $ flag <> " requires a value"
(argsafter,_) -> Just $ last argsafter (argsafter,_) -> Just $ last argsafter
-- | Given a list of arguments, split any of the form --flag=VAL or -fVAL
-- into separate list items. Multiple valueless short flags joined together is not supported.
splitFlagsAndVals :: [String] -> [String]
splitFlagsAndVals = concatMap $
\case
a@('-':'-':_) | '=' `elem` a -> let (x,y) = break (=='=') a in [x, drop 1 y]
a@('-':f:_:_) | not $ f=='-' -> [take 2 a, drop 2 a]
a -> [a]
-- | Convert a short or long flag name to a flag with leading hyphen(s).
toFlag [c] = ['-',c]
toFlag s = '-':'-':s
-- | Parse y/yes/always or n/no/never to true or false, or with any other value raise an error. -- | Parse y/yes/always or n/no/never to true or false, or with any other value raise an error.
parseYN :: String -> Bool parseYN :: String -> Bool
parseYN s parseYN s

View File

@ -1,11 +1,7 @@
{-# LANGUAGE CPP #-} {-# LANGUAGE CPP #-}
{-# LANGUAGE TemplateHaskell #-} {-# LANGUAGE TemplateHaskell #-}
{-|
-} module Hledger.UI.UIOptions where
module Hledger.UI.UIOptions
where
import Data.Default (def) import Data.Default (def)
import Data.List (intercalate) import Data.List (intercalate)