imp: reg --sort: show supported keys in help and error message [#2211]

This commit is contained in:
Simon Michael 2024-09-09 12:15:03 -07:00
parent 3b73360584
commit 5cbbdb4670
2 changed files with 11 additions and 7 deletions

View File

@ -23,6 +23,7 @@ module Hledger.Reports.ReportOptions (
HasReportSpec(..),
SortField(..),
SortSpec,
sortKeysDescription,
overEither,
setEither,
BalanceCalculation(..),
@ -696,18 +697,20 @@ getSortSpec opts =
optParser s =
let terms = map strip $ splitAtElement ',' s
termParser t = case trimmed of
"date" -> Date' isNegated
"account" -> Account' isNegated
"amount" -> Amount' isNegated
"desc" -> Description' isNegated
"date" -> Date' isNegated
"desc" -> Description' isNegated
"description" -> Description' isNegated
"absamount" -> AbsAmount' isNegated
_ -> error' $ "unsupported field '" ++ t ++ "' given to --sort"
"account" -> Account' isNegated
"amount" -> Amount' isNegated
"absamount" -> AbsAmount' isNegated
_ -> error' $ "unknown --sort key " ++ t ++ ". Supported keys are: " <> sortKeysDescription <> "."
where isNegated = isPrefixOf "-" t
trimmed = fromMaybe t (stripPrefix "-" t)
in map termParser terms
in maybe defsortspec optParser opt
-- for option's help and parse error message
sortKeysDescription = "date, desc, account, amount, absamount" -- 'description' is also accepted
-- Report dates.

View File

@ -50,7 +50,8 @@ registermode = hledgerCommandMode
,flagNone ["related","r"] (setboolopt "related") "show postings' siblings instead"
,flagNone ["invert"] (setboolopt "invert") "display all amounts with reversed sign"
,flagReq ["sort"] (\s opts -> Right $ setopt "sort" s opts) "FIELDS"
"comma-separated list of fields used to sort transactions"
("sort by: " <> sortKeysDescription
<> ", or a comma-separated combination of these. For a descending sort, prefix with -. (Default: date)")
,flagReq ["width","w"] (\s opts -> Right $ setopt "width" s opts) "N"
("set output width (default: " ++
#ifdef mingw32_HOST_OS