briefer, more informative usage error messages
This commit is contained in:
parent
93b1f2b0ca
commit
27ad4d7efe
@ -59,7 +59,7 @@ usage_chart = concat [
|
|||||||
|
|
||||||
main :: IO ()
|
main :: IO ()
|
||||||
main = do
|
main = do
|
||||||
(opts, args) <- parseArgumentsWith (options_cli++options_chart) usage_chart
|
(opts, args) <- parseArgumentsWith $ options_cli++options_chart
|
||||||
run opts args
|
run opts args
|
||||||
where
|
where
|
||||||
run opts args
|
run opts args
|
||||||
|
|||||||
@ -49,7 +49,7 @@ usage_vty = concat [
|
|||||||
|
|
||||||
main :: IO ()
|
main :: IO ()
|
||||||
main = do
|
main = do
|
||||||
(opts, args) <- parseArgumentsWith (options_cli++options_vty) usage_vty
|
(opts, args) <- parseArgumentsWith $ options_cli++options_vty
|
||||||
run opts args
|
run opts args
|
||||||
where
|
where
|
||||||
run opts args
|
run opts args
|
||||||
|
|||||||
@ -54,7 +54,7 @@ usage_web = concat [
|
|||||||
|
|
||||||
main :: IO ()
|
main :: IO ()
|
||||||
main = do
|
main = do
|
||||||
(opts, args) <- parseArgumentsWith (options_cli++options_web) usage_web
|
(opts, args) <- parseArgumentsWith $ options_cli++options_web
|
||||||
run opts args
|
run opts args
|
||||||
where
|
where
|
||||||
run opts args
|
run opts args
|
||||||
|
|||||||
@ -54,14 +54,15 @@ import Hledger.Cli.Version (progversionstr, binaryfilename)
|
|||||||
|
|
||||||
main :: IO ()
|
main :: IO ()
|
||||||
main = do
|
main = do
|
||||||
(opts, args) <- parseArgumentsWith options_cli usage_cli
|
(opts, args) <- parseArgumentsWith options_cli
|
||||||
run opts args
|
run opts args
|
||||||
where
|
where
|
||||||
run _ [] = putStr usage_cli
|
run opts _
|
||||||
run opts (cmd:args)
|
|
||||||
| Help `elem` opts = putStr usage_cli
|
| Help `elem` opts = putStr usage_cli
|
||||||
| Version `elem` opts = putStrLn $ progversionstr progname_cli
|
| Version `elem` opts = putStrLn $ progversionstr progname_cli
|
||||||
| BinaryFilename `elem` opts = putStrLn $ binaryfilename 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` "balance" = withJournalDo opts args cmd balance
|
||||||
| cmd `isPrefixOf` "convert" = withJournalDo opts args cmd convert
|
| cmd `isPrefixOf` "convert" = withJournalDo opts args cmd convert
|
||||||
| cmd `isPrefixOf` "print" = withJournalDo opts args cmd print'
|
| cmd `isPrefixOf` "print" = withJournalDo opts args cmd print'
|
||||||
@ -70,4 +71,4 @@ main = do
|
|||||||
| cmd `isPrefixOf` "add" = withJournalDo opts args cmd add
|
| cmd `isPrefixOf` "add" = withJournalDo opts args cmd add
|
||||||
| cmd `isPrefixOf` "stats" = withJournalDo opts args cmd stats
|
| cmd `isPrefixOf` "stats" = withJournalDo opts args cmd stats
|
||||||
| cmd `isPrefixOf` "test" = runtests opts args >> return ()
|
| cmd `isPrefixOf` "test" = runtests opts args >> return ()
|
||||||
| otherwise = putStr usage_cli
|
| otherwise = argsError $ "command "++cmd++" is unrecognized."
|
||||||
|
|||||||
@ -146,15 +146,18 @@ optValuesForConstructors fs opts = concatMap get opts
|
|||||||
-- converted to explicit YYYY/MM/DD format based on the current time. If
|
-- converted to explicit YYYY/MM/DD format based on the current time. If
|
||||||
-- parsing fails, raise an error, displaying the problem along with the
|
-- parsing fails, raise an error, displaying the problem along with the
|
||||||
-- provided usage string.
|
-- provided usage string.
|
||||||
parseArgumentsWith :: [OptDescr Opt] -> String -> IO ([Opt], [String])
|
parseArgumentsWith :: [OptDescr Opt] -> IO ([Opt], [String])
|
||||||
parseArgumentsWith options usage = do
|
parseArgumentsWith options = do
|
||||||
rawargs <- liftM (map decodeString) getArgs
|
rawargs <- map decodeString `fmap` getArgs
|
||||||
let (opts,args,errs) = getOpt Permute options rawargs
|
let (opts,args,errs) = getOpt Permute options rawargs
|
||||||
opts' <- fixOptDates opts
|
opts' <- fixOptDates opts
|
||||||
let opts'' = if Debug `elem` opts' then Verbose:opts' else opts'
|
let opts'' = if Debug `elem` opts' then Verbose:opts' else opts'
|
||||||
if null errs
|
if null errs
|
||||||
then return (opts'',args)
|
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,
|
-- | Convert any fuzzy dates within these option values to explicit ones,
|
||||||
-- based on today's date.
|
-- based on today's date.
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user