From 27ad4d7efe7ca840c5f6d5fafdf93439d589394e Mon Sep 17 00:00:00 2001 From: Simon Michael Date: Mon, 29 Nov 2010 01:08:19 +0000 Subject: [PATCH] briefer, more informative usage error messages --- hledger-chart/Hledger/Chart/Main.hs | 2 +- hledger-vty/Hledger/Vty/Main.hs | 2 +- hledger-web/Hledger/Web/Main.hs | 2 +- hledger/Hledger/Cli/Main.hs | 9 +++++---- hledger/Hledger/Cli/Options.hs | 11 +++++++---- 5 files changed, 15 insertions(+), 11 deletions(-) diff --git a/hledger-chart/Hledger/Chart/Main.hs b/hledger-chart/Hledger/Chart/Main.hs index fe7d5fe9a..bfe6a6096 100644 --- a/hledger-chart/Hledger/Chart/Main.hs +++ b/hledger-chart/Hledger/Chart/Main.hs @@ -59,7 +59,7 @@ usage_chart = concat [ main :: IO () main = do - (opts, args) <- parseArgumentsWith (options_cli++options_chart) usage_chart + (opts, args) <- parseArgumentsWith $ options_cli++options_chart run opts args where run opts args diff --git a/hledger-vty/Hledger/Vty/Main.hs b/hledger-vty/Hledger/Vty/Main.hs index 9a4b0bcf1..405052948 100644 --- a/hledger-vty/Hledger/Vty/Main.hs +++ b/hledger-vty/Hledger/Vty/Main.hs @@ -49,7 +49,7 @@ usage_vty = concat [ main :: IO () main = do - (opts, args) <- parseArgumentsWith (options_cli++options_vty) usage_vty + (opts, args) <- parseArgumentsWith $ options_cli++options_vty run opts args where run opts args diff --git a/hledger-web/Hledger/Web/Main.hs b/hledger-web/Hledger/Web/Main.hs index da3a46a1a..5265d9a13 100644 --- a/hledger-web/Hledger/Web/Main.hs +++ b/hledger-web/Hledger/Web/Main.hs @@ -54,7 +54,7 @@ usage_web = concat [ main :: IO () main = do - (opts, args) <- parseArgumentsWith (options_cli++options_web) usage_web + (opts, args) <- parseArgumentsWith $ options_cli++options_web run opts args where run opts args diff --git a/hledger/Hledger/Cli/Main.hs b/hledger/Hledger/Cli/Main.hs index 01da32d26..4b793ec11 100644 --- a/hledger/Hledger/Cli/Main.hs +++ b/hledger/Hledger/Cli/Main.hs @@ -54,14 +54,15 @@ import Hledger.Cli.Version (progversionstr, binaryfilename) main :: IO () main = do - (opts, args) <- parseArgumentsWith options_cli usage_cli + (opts, args) <- parseArgumentsWith options_cli run opts args where - run _ [] = putStr usage_cli - run opts (cmd:args) + run opts _ | Help `elem` opts = putStr usage_cli | Version `elem` opts = putStrLn $ progversionstr progname_cli | BinaryFilename `elem` opts = putStrLn $ binaryfilename progname_cli + run _ [] = argsError "a command is required." + run opts (cmd:args) | cmd `isPrefixOf` "balance" = withJournalDo opts args cmd balance | cmd `isPrefixOf` "convert" = withJournalDo opts args cmd convert | cmd `isPrefixOf` "print" = withJournalDo opts args cmd print' @@ -70,4 +71,4 @@ main = do | cmd `isPrefixOf` "add" = withJournalDo opts args cmd add | cmd `isPrefixOf` "stats" = withJournalDo opts args cmd stats | cmd `isPrefixOf` "test" = runtests opts args >> return () - | otherwise = putStr usage_cli + | otherwise = argsError $ "command "++cmd++" is unrecognized." diff --git a/hledger/Hledger/Cli/Options.hs b/hledger/Hledger/Cli/Options.hs index 27f5dc93b..bfcf3f6db 100644 --- a/hledger/Hledger/Cli/Options.hs +++ b/hledger/Hledger/Cli/Options.hs @@ -146,15 +146,18 @@ optValuesForConstructors fs opts = concatMap get opts -- converted to explicit YYYY/MM/DD format based on the current time. If -- parsing fails, raise an error, displaying the problem along with the -- provided usage string. -parseArgumentsWith :: [OptDescr Opt] -> String -> IO ([Opt], [String]) -parseArgumentsWith options usage = do - rawargs <- liftM (map decodeString) getArgs +parseArgumentsWith :: [OptDescr Opt] -> IO ([Opt], [String]) +parseArgumentsWith options = do + rawargs <- map decodeString `fmap` getArgs let (opts,args,errs) = getOpt Permute options rawargs opts' <- fixOptDates opts let opts'' = if Debug `elem` opts' then Verbose:opts' else opts' if null errs then return (opts'',args) - else ioError $ userError' $ concat errs ++ usage + else argsError (concat errs) >> return ([],[]) + +argsError :: String -> IO () +argsError = ioError . userError' . (++ " Run with --help to see usage.") -- | Convert any fuzzy dates within these option values to explicit ones, -- based on today's date.