diff --git a/hledger-lib/Hledger/Reports/ReportOptions.hs b/hledger-lib/Hledger/Reports/ReportOptions.hs index 3c456b4ab..7973aacd3 100644 --- a/hledger-lib/Hledger/Reports/ReportOptions.hs +++ b/hledger-lib/Hledger/Reports/ReportOptions.hs @@ -22,6 +22,7 @@ module Hledger.Reports.ReportOptions ( updateReportSpec, updateReportSpecWith, rawOptsToReportSpec, + balanceTypeOverride, flat_, tree_, reportOptsToggleStatus, @@ -190,7 +191,7 @@ rawOptsToReportOpts rawopts = do Just (Right x) -> return x Just (Left err) -> fail $ "could not parse format option: " ++ err - let reportopts = defreportopts + return defreportopts {period_ = periodFromRawOpts d rawopts ,interval_ = intervalFromRawOpts rawopts ,statuses_ = statusesFromRawOpts rawopts @@ -225,16 +226,6 @@ rawOptsToReportOpts rawopts = do ,transpose_ = boolopt "transpose" rawopts } - adjustReportDefaults reportopts - --- | Warn users about option combinations which produce uninteresting results. -adjustReportDefaults :: ReportOpts -> IO ReportOpts -adjustReportDefaults ropts = case reporttype_ ropts of - ValueChangeReport -> case fromMaybe (AtEnd Nothing) $ value_ ropts of - v@(AtEnd _) -> return ropts{value_=Just v} -- Set value_ to AtEnd by default, unless overridden - _ -> fail "--valuechange only produces sensible results with --value=end" - _ -> return ropts - -- | The result of successfully parsing a ReportOpts on a particular -- Day. Any ambiguous dates are completed and Queries are parsed, -- ensuring that there are no regular expression errors. Values here @@ -306,13 +297,19 @@ reporttypeopt = _ -> Nothing balancetypeopt :: RawOpts -> BalanceType -balancetypeopt = - fromMaybe PeriodChange . choiceopt parse where +balancetypeopt = fromMaybe PeriodChange . balanceTypeOverride + +balanceTypeOverride :: RawOpts -> Maybe BalanceType +balanceTypeOverride rawopts = choiceopt parse rawopts <|> reportbal + where parse = \case "historical" -> Just HistoricalBalance "cumulative" -> Just CumulativeChange "change" -> Just PeriodChange _ -> Nothing + reportbal = case reporttypeopt rawopts of + ValueChangeReport -> Just PeriodChange + _ -> Nothing -- Get the period specified by any -b/--begin, -e/--end and/or -p/--period -- options appearing in the command line. @@ -440,17 +437,25 @@ reportOptsToggleStatus s ropts@ReportOpts{statuses_=ss} -- allowed to combine -B/--cost with any other valuation type. If -- there's more than one valuation type, the rightmost flag wins. valuationTypeFromRawOpts :: RawOpts -> (Costing, Maybe ValuationType) -valuationTypeFromRawOpts rawopts = (costing, lastMay $ mapMaybe snd valuationopts) +valuationTypeFromRawOpts rawopts = (costing, valuation) where - costing = if (any ((Cost==) . fst) valuationopts) then Cost else NoCost + costing = if (any ((Cost==) . fst) valuationopts) then Cost else NoCost + valuation = case reporttypeopt rawopts of + ValueChangeReport -> case directval of + Nothing -> Just $ AtEnd Nothing -- If no valuation requested for valuechange, use AtEnd + Just (AtEnd _) -> directval -- If AtEnd valuation requested, use it + Just _ -> usageError "--valuechange only produces sensible results with --value=end" + _ -> directval -- Otherwise, use requested valuation + where directval = lastMay $ mapMaybe snd valuationopts + valuationopts = collectopts valuationfromrawopt rawopts valuationfromrawopt (n,v) -- option name, value | n == "B" = Just (Cost, Nothing) | n == "V" = Just (NoCost, Just $ AtEnd Nothing) | n == "X" = Just (NoCost, Just $ AtEnd (Just $ T.pack v)) - | n == "value" = Just $ valuation v + | n == "value" = Just $ valueopt v | otherwise = Nothing - valuation v + valueopt v | t `elem` ["cost","c"] = (Cost, usageError "--value=cost,COMM is no longer supported, please specify valuation explicitly, e.g. --cost --value=then,COMM" <$ mc) | t `elem` ["then" ,"t"] = (NoCost, Just $ AtThen mc) | t `elem` ["end" ,"e"] = (NoCost, Just $ AtEnd mc) diff --git a/hledger/Hledger/Cli/CompoundBalanceCommand.hs b/hledger/Hledger/Cli/CompoundBalanceCommand.hs index f355c41e0..82df236c7 100644 --- a/hledger/Hledger/Cli/CompoundBalanceCommand.hs +++ b/hledger/Hledger/Cli/CompoundBalanceCommand.hs @@ -106,13 +106,7 @@ compoundBalanceCommand CompoundBalanceCommandSpec{..} opts@CliOpts{reportspec_=r where ropts@ReportOpts{..} = rsOpts rspec -- use the default balance type for this report, unless the user overrides - mBalanceTypeOverride = - choiceopt parse rawopts where - parse = \case - "historical" -> Just HistoricalBalance - "cumulative" -> Just CumulativeChange - "change" -> Just PeriodChange - _ -> Nothing + mBalanceTypeOverride = balanceTypeOverride rawopts balancetype = fromMaybe cbctype mBalanceTypeOverride -- Set balance type in the report options. ropts' = ropts{balancetype_=balancetype} diff --git a/hledger/test/journal/valuechange.test b/hledger/test/journal/valuechange.test index b38d293f1..bb6b1ff06 100644 --- a/hledger/test/journal/valuechange.test +++ b/hledger/test/journal/valuechange.test @@ -55,7 +55,7 @@ Ending balances (historical) in 2000-01-01..2000-04-30, valued at period ends: || 15 B 8 B 15 B 20 B # 4. Balance sheet also reports on value change -$ hledger -f- balancesheet -MN --valuechange --change -b 2000 +$ hledger -f- balancesheet -MN --valuechange -b 2000 Balance Sheet 2000-01-01..2000-04-30 (Period-End Value Changes) || Jan Feb Mar Apr