diff --git a/hledger/Hledger/Cli/Commands.hs b/hledger/Hledger/Cli/Commands.hs index 73d25eadd..9436128be 100644 --- a/hledger/Hledger/Cli/Commands.hs +++ b/hledger/Hledger/Cli/Commands.hs @@ -72,6 +72,7 @@ import Hledger.Cli.Commands.Balance import Hledger.Cli.Commands.Balancesheet import Hledger.Cli.Commands.Balancesheetequity import Hledger.Cli.Commands.Cashflow +import Hledger.Cli.Commands.Check import Hledger.Cli.Commands.Checkdates import Hledger.Cli.Commands.Checkdupes import Hledger.Cli.Commands.Close @@ -109,6 +110,7 @@ builtinCommands = [ ,(balancesheetequitymode , balancesheetequity) ,(balancesheetmode , balancesheet) ,(cashflowmode , cashflow) + ,(checkmode , check) ,(checkdatesmode , checkdates) ,(checkdupesmode , checkdupes) ,(closemode , close) @@ -166,9 +168,7 @@ commandsList progversion othercmds = [ ,"" ,"Data management:" ,"+autosync download/deduplicate/convert OFX data" - ,"+check check more powerful balance assertions" - ," check-dates check transactions are ordered by date" - ," check-dupes check for accounts with the same leaf name" + ," check check for various kinds of issue in the data" ," close (equity) generate balance-resetting transactions" ," diff compare account transactions in two journal files" ,"+interest generate interest transactions" diff --git a/hledger/Hledger/Cli/Commands/Check.hs b/hledger/Hledger/Cli/Commands/Check.hs new file mode 100644 index 000000000..abb824355 --- /dev/null +++ b/hledger/Hledger/Cli/Commands/Check.hs @@ -0,0 +1,67 @@ +{-# LANGUAGE LambdaCase #-} +{-# LANGUAGE RecordWildCards #-} +{-# LANGUAGE TemplateHaskell #-} + +module Hledger.Cli.Commands.Check ( + checkmode + ,check +) where + +import Hledger +import Hledger.Cli.CliOptions +import Hledger.Cli.Commands.Checkdupes (checkdupes) +import Hledger.Cli.Commands.Checkdates (checkdates) +import System.Console.CmdArgs.Explicit +import Data.Either (partitionEithers) +import Data.Char (toUpper) +import Safe (readMay) +import Control.Monad (forM_) + +checkmode :: Mode RawOpts +checkmode = hledgerCommandMode + $(embedFileRelative "Hledger/Cli/Commands/Check.txt") + [] + [generalflagsgroup1] + hiddenflags + ([], Just $ argsFlag "[CHECKS]") + +check :: CliOpts -> Journal -> IO () +check copts@CliOpts{..} j = do + let + args = listofstringopt "args" rawopts_ + -- we must reset the report spec generated by argsToCliOpts + -- since we are not using arguments as a query in the usual way, + copts' = + case updateReportSpecFromOpts update reportspec_ of + Left e -> error' e + Right rs -> copts{reportspec_=rs} + where update ropts = ropts{querystring_=[]} + + case partitionEithers (map parseCheck args) of + (unknowns@(_:_), _) -> error' $ "These checks are unknown: "++unwords unknowns + ([], checks) -> forM_ checks $ runCheck copts' j + +-- | A type of error check that we can perform on the data. +data Check = + Dates + | Leafnames + deriving (Read,Show,Eq) + +-- | Parse the lower-case name of an error check, or return the name unparsed. +parseCheck :: String -> Either String Check +parseCheck s = maybe (Left s) Right $ readMay $ capitalise s + +capitalise :: String -> String +capitalise (c:cs) = toUpper c : cs +capitalise s = s + +-- | Parse a check argument: a string which is the lower-case name of an error check, +-- followed by zero or more space-separated arguments for that check. +-- parseCheckArgument :: String -> Either String (Check,[String]) + +runCheck :: CliOpts -> Journal -> Check -> IO () +runCheck copts j = + \case + Dates -> checkdates copts j + Leafnames -> checkdupes copts j + diff --git a/hledger/Hledger/Cli/Commands/Check.md b/hledger/Hledger/Cli/Commands/Check.md new file mode 100644 index 000000000..a6e2be4db --- /dev/null +++ b/hledger/Hledger/Cli/Commands/Check.md @@ -0,0 +1,4 @@ +check\ +Check for various kinds of issue in the data. + +_FLAGS diff --git a/hledger/Hledger/Cli/Commands/Check.txt b/hledger/Hledger/Cli/Commands/Check.txt new file mode 100644 index 000000000..8000f2e61 --- /dev/null +++ b/hledger/Hledger/Cli/Commands/Check.txt @@ -0,0 +1,4 @@ +check +Check for various kinds of issue in the data. + +_FLAGS diff --git a/hledger/hledger.cabal b/hledger/hledger.cabal index 8cc38b417..8545c6c58 100644 --- a/hledger/hledger.cabal +++ b/hledger/hledger.cabal @@ -4,7 +4,7 @@ cabal-version: 1.12 -- -- see: https://github.com/sol/hpack -- --- hash: 4517e1a53a08aa05c53bb06d8591b5591f3ae6f688bf39f809c8f774fbd1d41c +-- hash: 3b5d4e8c7e8f90c11eea9adcc8b197ce679113e8f72f7a02a060a328a43db663 name: hledger version: 1.19.99 @@ -66,6 +66,7 @@ extra-source-files: Hledger/Cli/Commands/Balancesheet.txt Hledger/Cli/Commands/Balancesheetequity.txt Hledger/Cli/Commands/Cashflow.txt + Hledger/Cli/Commands/Check.txt Hledger/Cli/Commands/Checkdates.txt Hledger/Cli/Commands/Checkdupes.txt Hledger/Cli/Commands/Close.txt @@ -122,6 +123,7 @@ library Hledger.Cli.Commands.Balancesheet Hledger.Cli.Commands.Balancesheetequity Hledger.Cli.Commands.Cashflow + Hledger.Cli.Commands.Check Hledger.Cli.Commands.Checkdates Hledger.Cli.Commands.Checkdupes Hledger.Cli.Commands.Close diff --git a/hledger/hledger.m4.md b/hledger/hledger.m4.md index d7820d188..b372c986c 100644 --- a/hledger/hledger.m4.md +++ b/hledger/hledger.m4.md @@ -1618,13 +1618,9 @@ _include_({{Hledger/Cli/Commands/Balancesheetequity.md}}) _include_({{Hledger/Cli/Commands/Cashflow.md}}) -## check-dates +## check -_include_({{Hledger/Cli/Commands/Checkdates.md}}) - -## check-dupes - -_include_({{Hledger/Cli/Commands/Checkdupes.md}}) +_include_({{Hledger/Cli/Commands/Check.md}}) ## close diff --git a/hledger/package.yaml b/hledger/package.yaml index 457b4a415..598dc2de2 100644 --- a/hledger/package.yaml +++ b/hledger/package.yaml @@ -60,6 +60,7 @@ extra-source-files: - Hledger/Cli/Commands/Balancesheet.txt - Hledger/Cli/Commands/Balancesheetequity.txt - Hledger/Cli/Commands/Cashflow.txt +- Hledger/Cli/Commands/Check.txt - Hledger/Cli/Commands/Checkdates.txt - Hledger/Cli/Commands/Checkdupes.txt - Hledger/Cli/Commands/Close.txt @@ -168,6 +169,7 @@ library: - Hledger.Cli.Commands.Balancesheet - Hledger.Cli.Commands.Balancesheetequity - Hledger.Cli.Commands.Cashflow + - Hledger.Cli.Commands.Check - Hledger.Cli.Commands.Checkdates - Hledger.Cli.Commands.Checkdupes - Hledger.Cli.Commands.Close