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 :: IO ()
main = do main = do
(opts, cmd, args) <- parseArgumentsWith (options_cli++options_chart) usage_chart (opts, args) <- parseArgumentsWith (options_cli++options_chart) usage_chart
run opts (cmd:args) run opts args
where where
run opts args run opts args
| Help `elem` opts = putStr usage_chart | Help `elem` opts = putStr usage_chart

View File

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

View File

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

View File

@ -54,14 +54,14 @@ import Hledger.Cli.Version (progversionstr, binaryfilename)
main :: IO () main :: IO ()
main = do main = do
(opts, cmd, args) <- parseArgumentsWith options_cli usage_cli (opts, args) <- parseArgumentsWith options_cli usage_cli
run cmd opts args run opts args
where where
run cmd opts args run _ [] = putStr usage_cli
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
| null cmd = maybe (putStr usage_cli) (withJournalDo opts args cmd) defaultcmd
| 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'
@ -71,5 +71,3 @@ main = do
| 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 = putStr usage_cli
defaultcmd = Nothing

View File

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