From 7510d99aec9148710b8efba7474bd5315c6e1cc3 Mon Sep 17 00:00:00 2001 From: Simon Michael Date: Tue, 5 Jan 2021 16:18:24 -0800 Subject: [PATCH] check: accept case-insensitive prefixes as arguments Might a bad idea, but avoiding wasteful typing.. --- hledger/Hledger/Cli/Commands/Check.hs | 19 ++++++++++++------- hledger/Hledger/Cli/Commands/Check.md | 2 ++ 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/hledger/Hledger/Cli/Commands/Check.hs b/hledger/Hledger/Cli/Commands/Check.hs index e7f872b43..aff15c3a7 100644 --- a/hledger/Hledger/Cli/Commands/Check.hs +++ b/hledger/Hledger/Cli/Commands/Check.hs @@ -15,8 +15,8 @@ import Hledger.Cli.Commands.Check.Ordereddates (journalCheckOrdereddates) import Hledger.Cli.Commands.Check.Uniqueleafnames (journalCheckUniqueleafnames) import System.Console.CmdArgs.Explicit import Data.Either (partitionEithers) -import Data.Char (toUpper) -import Safe (readMay) +import Data.Char (toLower,toUpper) +import Data.List (isPrefixOf, find) import Control.Monad (forM_) import System.IO (stderr, hPutStrLn) import System.Exit (exitFailure) @@ -60,19 +60,24 @@ data Check = | Ordereddates | Payees | Uniqueleafnames - deriving (Read,Show,Eq) + deriving (Read,Show,Eq,Enum,Bounded) --- | Parse the name of an error check, or return the name unparsed. --- Names are conventionally all lower case, but this parses case insensitively. +-- | Parse the name (or a name prefix) of an error check, or return the name unparsed. +-- Check names are conventionally all lower case, but this parses case insensitively. 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 (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. +-- or a prefix thereof, followed by zero or more space-separated arguments for that check. parseCheckArgument :: String -> Either String (Check,[String]) parseCheckArgument s = dbg3 "check argument" $ diff --git a/hledger/Hledger/Cli/Commands/Check.md b/hledger/Hledger/Cli/Commands/Check.md index f5a50cd88..9be84e922 100644 --- a/hledger/Hledger/Cli/Commands/Check.md +++ b/hledger/Hledger/Cli/Commands/Check.md @@ -8,6 +8,8 @@ prevent problems in your data. Some of these are run automatically; or, you can use this `check` command to run them on demand, with no output and a zero exit code if all is well. +Specify their names (or a prefix) as argument(s). + Some examples: ```shell