check: accept case-insensitive prefixes as arguments

Might a bad idea, but avoiding wasteful typing..
This commit is contained in:
Simon Michael 2021-01-05 16:18:24 -08:00
parent 9abb33d8e7
commit 7510d99aec
2 changed files with 14 additions and 7 deletions

View File

@ -15,8 +15,8 @@ import Hledger.Cli.Commands.Check.Ordereddates (journalCheckOrdereddates)
import Hledger.Cli.Commands.Check.Uniqueleafnames (journalCheckUniqueleafnames) import Hledger.Cli.Commands.Check.Uniqueleafnames (journalCheckUniqueleafnames)
import System.Console.CmdArgs.Explicit import System.Console.CmdArgs.Explicit
import Data.Either (partitionEithers) import Data.Either (partitionEithers)
import Data.Char (toUpper) import Data.Char (toLower,toUpper)
import Safe (readMay) import Data.List (isPrefixOf, find)
import Control.Monad (forM_) import Control.Monad (forM_)
import System.IO (stderr, hPutStrLn) import System.IO (stderr, hPutStrLn)
import System.Exit (exitFailure) import System.Exit (exitFailure)
@ -60,19 +60,24 @@ data Check =
| Ordereddates | Ordereddates
| Payees | Payees
| Uniqueleafnames | Uniqueleafnames
deriving (Read,Show,Eq) deriving (Read,Show,Eq,Enum,Bounded)
-- | Parse the name of an error check, or return the name unparsed. -- | Parse the name (or a name prefix) of an error check, or return the name unparsed.
-- Names are conventionally all lower case, but this parses case insensitively. -- Check names are conventionally all lower case, but this parses case insensitively.
parseCheck :: String -> Either String Check parseCheck :: String -> Either String Check
parseCheck s = maybe (Left s) Right $ readMay $ capitalise s parseCheck s =
maybe (Left s) (Right . read) $ -- PARTIAL: read should not fail here
find (s' `isPrefixOf`) $ checknames
where
s' = capitalise $ map toLower s
checknames = map show [minBound..maxBound::Check]
capitalise :: String -> String capitalise :: String -> String
capitalise (c:cs) = toUpper c : cs capitalise (c:cs) = toUpper c : cs
capitalise s = s capitalise s = s
-- | Parse a check argument: a string which is the lower-case name of an error check, -- | 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. -- or a prefix thereof, followed by zero or more space-separated arguments for that check.
parseCheckArgument :: String -> Either String (Check,[String]) parseCheckArgument :: String -> Either String (Check,[String])
parseCheckArgument s = parseCheckArgument s =
dbg3 "check argument" $ dbg3 "check argument" $

View File

@ -8,6 +8,8 @@ prevent problems in your data.
Some of these are run automatically; or, Some of these are run automatically; or,
you can use this `check` command to run them on demand, you can use this `check` command to run them on demand,
with no output and a zero exit code if all is well. with no output and a zero exit code if all is well.
Specify their names (or a prefix) as argument(s).
Some examples: Some examples:
```shell ```shell