lib: parse optional ,COMM suffix in --value (#131)

This commit is contained in:
Simon Michael 2019-06-01 14:54:34 -07:00
parent 18e19b9ad2
commit 6b6c3385c9

View File

@ -333,22 +333,31 @@ reportOptsToggleStatus s ropts@ReportOpts{statuses_=ss}
| s `elem` ss = ropts{statuses_=filter (/= s) ss} | s `elem` ss = ropts{statuses_=filter (/= s) ss}
| otherwise = ropts{statuses_=simplifyStatuses (s:ss)} | otherwise = ropts{statuses_=simplifyStatuses (s:ss)}
-- | Parse the type of valuation to be performed, if any, specified by
-- -B/--cost/-V/--value flags. If there's more than one of these, the
-- rightmost flag wins.
valuationTypeFromRawOpts :: RawOpts -> Maybe ValuationType valuationTypeFromRawOpts :: RawOpts -> Maybe ValuationType
valuationTypeFromRawOpts = lastDef Nothing . filter isJust . map valuationfromrawopt valuationTypeFromRawOpts = lastDef Nothing . filter isJust . map valuationfromrawopt
where where
valuationfromrawopt (n,v) valuationfromrawopt (n,v) -- option name, value
| n == "B" = Just $ AtCost Nothing | n == "B" = Just $ AtCost Nothing
| n == "V" = Just $ AtDefault Nothing | n == "V" = Just $ AtDefault Nothing
| n == "value" = Just $ valuation v | n == "value" = Just $ valuation v
| otherwise = Nothing | otherwise = Nothing
valuation v valuation v
| v `elem` ["cost","c"] = AtCost Nothing | t `elem` ["cost","c"] = AtCost mc
| v `elem` ["end" ,"e"] = AtEnd Nothing | t `elem` ["end" ,"e"] = AtEnd mc
| v `elem` ["now" ,"n"] = AtNow Nothing | t `elem` ["now" ,"n"] = AtNow mc
| otherwise = | otherwise =
case parsedateM v of case parsedateM t of
Just d -> AtDate d Nothing Just d -> AtDate d mc
Nothing -> usageError $ "could not parse \""++v++"\" as value date, should be: transaction|period|now|t|p|n|YYYY-MM-DD" Nothing -> usageError $ "could not parse \""++t++"\" as valuation type, should be: cost|end|now|c|e|n|YYYY-MM-DD"
where
-- parse TYPE[,COMM]
(t,c') = break (==',') v
mc = case drop 1 c' of
"" -> Nothing
c -> Just $ T.pack c
type DisplayExp = String type DisplayExp = String