From 993d0b5c54e364b26fe78a8e6ff62bd626e42c1e Mon Sep 17 00:00:00 2001 From: Simon Michael Date: Fri, 20 Aug 2021 17:55:59 -1000 Subject: [PATCH] cli: --forecast now rejects instead of silently ignoring an interval Something that confused me when testing #1632. --- hledger-lib/Hledger/Read/Common.hs | 20 +++++++++++++++----- hledger/hledger.m4.md | 2 +- 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/hledger-lib/Hledger/Read/Common.hs b/hledger-lib/Hledger/Read/Common.hs index 47adea68a..a58c31acd 100644 --- a/hledger-lib/Hledger/Read/Common.hs +++ b/hledger-lib/Hledger/Read/Common.hs @@ -165,6 +165,7 @@ import Hledger.Reports.ReportOptions (ReportOpts(..), queryFromFlags, rawOptsToR import Hledger.Utils import Text.Printf (printf) import Hledger.Read.InputOptions +import Data.String.Compat (unlines) --- ** doctest setup -- $setup @@ -254,15 +255,24 @@ rawOptsToInputOpts rawopts = do } where noinferprice = boolopt "strict" rawopts || stringopt "args" rawopts == "balancednoautoconversion" --- | Get period expression from --forecast option. --- This will fail with a usage error if the forecast period expression cannot be parsed. +-- | Get the date span from --forecast's PERIODEXPR argument, if any. +-- This will fail with a usage error if the period expression cannot be parsed, +-- or if it contains a report interval. forecastPeriodFromRawOpts :: Day -> RawOpts -> Maybe DateSpan forecastPeriodFromRawOpts d rawopts = case maybestringopt "forecast" rawopts of Nothing -> Nothing Just "" -> Just forecastspanDefault - Just str -> either (\e -> usageError $ "could not parse forecast period : "++customErrorBundlePretty e) - (\(_,requestedspan) -> Just $ requestedspan `spanDefaultsFrom` forecastspanDefault) $ - parsePeriodExpr d $ stripquotes $ T.pack str + Just arg -> + either + (\e -> usageError $ "could not parse forecast period : "++customErrorBundlePretty e) + (\(interval, requestedspan) -> + case interval of + NoInterval -> Just $ requestedspan `spanDefaultsFrom` forecastspanDefault + _ -> usageError $ unlines + [ "--forecast's argument should not contain a report interval" + , "(" ++ show interval ++ " in \"" ++ arg ++ "\")" + ]) + (parsePeriodExpr d $ stripquotes $ T.pack arg) where -- "They end on or before the specified report end date, or 180 days from today if unspecified." mspecifiedend = dbg2 "specifieddates" $ queryEndDate False datequery diff --git a/hledger/hledger.m4.md b/hledger/hledger.m4.md index 179f1333f..0423796e9 100644 --- a/hledger/hledger.m4.md +++ b/hledger/hledger.m4.md @@ -3189,7 +3189,7 @@ Note: - the equals sign is required (a space won't work) - PERIODEXPR can specify the forecast period's start and/or end dates (similar to [Report start & end date](#report-start--end-date)) -- PERIODEXPR shouldn't specify a report interval; that would be ignored, as it is specified by the periodic rules in the journal. +- PERIODEXPR shouldn't specify a report interval; each periodic transaction rule specifies its own. Some examples: `--forecast=202001-202004`, `--forecast=jan-`, `--forecast=2020`.