more correct args parsing, fix account filtering in hledger-web

This commit is contained in:
Simon Michael 2010-11-29 00:37:21 +00:00
parent 3979885037
commit 93b1f2b0ca
5 changed files with 23 additions and 27 deletions

View File

@ -59,8 +59,8 @@ usage_chart = concat [
main :: IO ()
main = do
(opts, cmd, args) <- parseArgumentsWith (options_cli++options_chart) usage_chart
run opts (cmd:args)
(opts, args) <- parseArgumentsWith (options_cli++options_chart) usage_chart
run opts args
where
run opts args
| Help `elem` opts = putStr usage_chart

View File

@ -49,8 +49,8 @@ usage_vty = concat [
main :: IO ()
main = do
(opts, cmd, args) <- parseArgumentsWith (options_cli++options_vty) usage_vty
run opts (cmd:args)
(opts, args) <- parseArgumentsWith (options_cli++options_vty) usage_vty
run opts args
where
run opts args
| Help `elem` opts = putStr usage_vty

View File

@ -54,8 +54,8 @@ usage_web = concat [
main :: IO ()
main = do
(opts, cmd, args) <- parseArgumentsWith (options_cli++options_web) usage_web
run opts (cmd:args)
(opts, args) <- parseArgumentsWith (options_cli++options_web) usage_web
run opts args
where
run opts args
| Help `elem` opts = putStr usage_web

View File

@ -54,14 +54,14 @@ import Hledger.Cli.Version (progversionstr, binaryfilename)
main :: IO ()
main = do
(opts, cmd, args) <- parseArgumentsWith options_cli usage_cli
run cmd opts args
(opts, args) <- parseArgumentsWith options_cli usage_cli
run opts args
where
run cmd opts args
run _ [] = putStr usage_cli
run opts (cmd:args)
| Help `elem` opts = putStr usage_cli
| Version `elem` opts = putStrLn $ progversionstr progname_cli
| BinaryFilename `elem` opts = putStrLn $ binaryfilename progname_cli
| null cmd = maybe (putStr usage_cli) (withJournalDo opts args cmd) defaultcmd
| cmd `isPrefixOf` "balance" = withJournalDo opts args cmd balance
| cmd `isPrefixOf` "convert" = withJournalDo opts args cmd convert
| cmd `isPrefixOf` "print" = withJournalDo opts args cmd print'
@ -71,5 +71,3 @@ main = do
| cmd `isPrefixOf` "stats" = withJournalDo opts args cmd stats
| cmd `isPrefixOf` "test" = runtests opts args >> return ()
| otherwise = putStr usage_cli
defaultcmd = Nothing

View File

@ -141,22 +141,20 @@ optValuesForConstructor f opts = concatMap get opts
optValuesForConstructors fs opts = concatMap get opts
where get o = [v | any (\f -> f v == o) fs] where v = value o
-- | Parse the command-line arguments into options, command name (first
-- argument), and command arguments (rest of arguments), using the
-- specified options. Any smart dates in the options are converted to
-- explicit YYYY/MM/DD format based on the current time. If parsing fails,
-- raise an error, displaying the problem along with the specified usage
-- string.
parseArgumentsWith :: [OptDescr Opt] -> String -> IO ([Opt], String, [String])
-- | Parse the command-line arguments into options and arguments using the
-- specified option descriptors. Any smart dates in the options are
-- 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
args <- liftM (map decodeString) getArgs
let (os,as,es) = getOpt Permute options args
os' <- fixOptDates os
let os'' = if Debug `elem` os' then Verbose:os' else os'
case (as,es) of
(cmd:args,[]) -> return (os'',cmd,args)
([],[]) -> return (os'',"",[])
(_,errs) -> ioError (userError' (concat errs ++ usage))
rawargs <- liftM (map decodeString) 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
-- | Convert any fuzzy dates within these option values to explicit ones,
-- based on today's date.