Show error message on conflicting --sort and -H

This commit is contained in:
Michael Rees 2024-07-22 10:32:56 -05:00 committed by Simon Michael
parent 4b564966c9
commit b429f57afb
2 changed files with 12 additions and 3 deletions

View File

@ -16,6 +16,7 @@ module Hledger.Reports.PostingsReport (
postingsReport, postingsReport,
mkpostingsReportItem, mkpostingsReportItem,
SortSpec, SortSpec,
defsortspec,
-- * Tests -- * Tests
tests_PostingsReport tests_PostingsReport
@ -89,7 +90,7 @@ postingsReport rspec@ReportSpec{_rsReportOpts=ropts@ReportOpts{..}} j = items
-- Posting report items ready for display. -- Posting report items ready for display.
items = items =
dbg4 "postingsReport items" $ dbg4 "postingsReport items" $
postingsReportItems sortedps (nullposting,Nothing) whichdate mdepth startbal runningcalc startnum postingsReportItems postings (nullposting,Nothing) whichdate mdepth startbal runningcalc startnum
where where
-- In historical mode we'll need a starting balance, which we -- In historical mode we'll need a starting balance, which we
-- may be converting to value per hledger_options.m4.md "Effect -- may be converting to value per hledger_options.m4.md "Effect
@ -104,6 +105,10 @@ postingsReport rspec@ReportSpec{_rsReportOpts=ropts@ReportOpts{..}} j = items
runningcalc = registerRunningCalculationFn ropts runningcalc = registerRunningCalculationFn ropts
startnum = if historical then length precedingps + 1 else 1 startnum = if historical then length precedingps + 1 else 1
postings | historical = if sortspec_ /= defsortspec
then error "--historical and --sort should not be used together"
else sortedps
| otherwise = sortedps
-- | Based on the given report options, return a function that does the appropriate -- | Based on the given report options, return a function that does the appropriate
-- running calculation for the register report, ie a running average or running total. -- running calculation for the register report, ie a running average or running total.

View File

@ -33,6 +33,7 @@ module Hledger.Reports.ReportOptions (
defreportopts, defreportopts,
rawOptsToReportOpts, rawOptsToReportOpts,
defreportspec, defreportspec,
defsortspec,
setDefaultConversionOp, setDefaultConversionOp,
reportOptsToSpec, reportOptsToSpec,
updateReportSpec, updateReportSpec,
@ -678,9 +679,12 @@ data SortField
= Date' Bool = Date' Bool
| Account' Bool | Account' Bool
| Amount' Bool | Amount' Bool
deriving (Show) deriving (Show, Eq)
type SortSpec = [SortField] type SortSpec = [SortField]
defsortspec :: SortSpec
defsortspec = [Date' False]
-- Load a SortSpec from the argument given to --sort -- Load a SortSpec from the argument given to --sort
-- If there is no spec given, then sort by [Date' False] by default -- If there is no spec given, then sort by [Date' False] by default
getSortSpec :: RawOpts -> SortSpec getSortSpec :: RawOpts -> SortSpec
@ -696,7 +700,7 @@ getSortSpec opts =
where isNegated = isPrefixOf "-" t where isNegated = isPrefixOf "-" t
trimmed = fromMaybe t (stripPrefix "-" t) trimmed = fromMaybe t (stripPrefix "-" t)
in map termParser terms in map termParser terms
in maybe [Date' False] optParser opt in maybe defsortspec optParser opt
-- Report dates. -- Report dates.