briefer, more informative usage error messages

This commit is contained in:
Simon Michael 2010-11-29 01:08:19 +00:00
parent 93b1f2b0ca
commit 27ad4d7efe
5 changed files with 15 additions and 11 deletions

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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."

View File

@ -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.