another part of add-on command detection that was missed

This commit is contained in:
Simon Michael 2011-08-25 23:13:08 +00:00
parent dc43ca2d00
commit 17fbc9de14
2 changed files with 28 additions and 28 deletions

View File

@ -67,24 +67,27 @@ main = do
opts <- getHledgerCliOpts addons opts <- getHledgerCliOpts addons
when (debug_ opts) $ printf "%s\n" progversion >> printf "opts: %s\n" (show opts) when (debug_ opts) $ printf "%s\n" progversion >> printf "opts: %s\n" (show opts)
run' opts addons args run' opts addons args
where where
cmd = command_ opts run' opts@CliOpts{command_=cmd} addons args
run' opts | "version" `in_` (rawopts_ opts) = putStrLn progversion
| null cmd = printModeHelpAndExit mainmode | "binary-filename" `in_` (rawopts_ opts) = putStrLn $ binaryfilename progname
| any (cmd `isPrefixOf`) ["accounts","balance"] = showModeHelpOr accountsmode $ withJournalDo opts balance | null cmd = putStr $ showModeHelp mainmode'
| any (cmd `isPrefixOf`) ["activity","histogram"] = showModeHelpOr activitymode $ withJournalDo opts histogram | cmd `isPrefixOf` "add" = showModeHelpOr addmode $ withJournalDo opts add
| cmd `isPrefixOf` "add" = showModeHelpOr addmode $ withJournalDo opts add | cmd `isPrefixOf` "convert" = showModeHelpOr convertmode $ convert opts
| cmd `isPrefixOf` "convert" = showModeHelpOr convertmode $ withJournalDo opts convert | cmd `isPrefixOf` "test" = showModeHelpOr testmode $ runtests opts
| any (cmd `isPrefixOf`) ["entries","print"] = showModeHelpOr entriesmode $ withJournalDo opts print' | any (cmd `isPrefixOf`) ["accounts","balance"] = showModeHelpOr accountsmode $ withJournalDo opts balance
| any (cmd `isPrefixOf`) ["postings","register"] = showModeHelpOr postingsmode $ withJournalDo opts register | any (cmd `isPrefixOf`) ["entries","print"] = showModeHelpOr entriesmode $ withJournalDo opts print'
| cmd `isPrefixOf` "stats" = showModeHelpOr statsmode $ withJournalDo opts stats | any (cmd `isPrefixOf`) ["postings","register"] = showModeHelpOr postingsmode $ withJournalDo opts register
| cmd `isPrefixOf` "test" = showModeHelpOr testmode $ runtests opts >> return () | any (cmd `isPrefixOf`) ["activity","histogram"] = showModeHelpOr activitymode $ withJournalDo opts histogram
| cmd `isPrefixOf` "binaryfilename" = showModeHelpOr binaryfilenamemode $ putStrLn $ binaryfilename progname | cmd `isPrefixOf` "stats" = showModeHelpOr statsmode $ withJournalDo opts stats
| otherwise = showModeHelpOr mainmode $ optserror $ "command "++cmd++" is not recognized" | not (null matchedaddon) = system shellcmd >>= exitWith
showModeHelpOr mode f = do | otherwise = optserror ("command "++cmd++" is not recognized") >> exitFailure
when ("help" `in_` (rawopts_ opts)) $ printModeHelpAndExit mode where
when ("version" `in_` (rawopts_ opts)) $ printVersionAndExit mainmode' = mainmode addons
f showModeHelpOr mode f | "help" `in_` (rawopts_ opts) = putStr $ showModeHelp mode
| otherwise = f
matchedaddon = headDef "" $ filter (cmd `isPrefixOf`) addons
shellcmd = printf "%s-%s %s" progname matchedaddon (unwords' args)
{- tests: {- tests:

View File

@ -326,7 +326,7 @@ toCliOpts rawopts = do
getHledgerCliOpts :: [String] -> IO CliOpts getHledgerCliOpts :: [String] -> IO CliOpts
getHledgerCliOpts addons = do getHledgerCliOpts addons = do
args <- getArgs args <- getArgs
toCliOpts (decodeRawOpts $ processValue (mainmode addons) $ tempMoveFlagsAfterCommand args) >>= checkCliOpts toCliOpts (decodeRawOpts $ processValue (mainmode addons) $ moveFileOption args) >>= checkCliOpts
-- utils -- utils
@ -352,11 +352,12 @@ getDirectoryContentsSafe d = getDirectoryContents d `catch` (\_ -> return [])
-- | Convert possibly encoded option values to regular unicode strings. -- | Convert possibly encoded option values to regular unicode strings.
decodeRawOpts = map (\(name,val) -> (name, fromPlatformString val)) decodeRawOpts = map (\(name,val) -> (name, fromPlatformString val))
-- workaround for http://code.google.com/p/ndmitchell/issues/detail?id=457 -- A workaround related to http://code.google.com/p/ndmitchell/issues/detail?id=457 :
-- just handles commonest case, -f option before command -- we'd like to permit options before COMMAND as well as after it. Here we
tempMoveFlagsAfterCommand (fflagandval@('-':'f':_:_):cmd:rest) = cmd:fflagandval:rest -- make sure at least -f FILE will be accepted in either position.
tempMoveFlagsAfterCommand ("-f":fval:cmd:rest) = cmd:"-f":fval:rest moveFileOption (fopt@('-':'f':_:_):cmd:rest) = cmd:fopt:rest
tempMoveFlagsAfterCommand as = as moveFileOption ("-f":fval:cmd:rest) = cmd:"-f":fval:rest
moveFileOption as = as
optserror = error' . (++ " (run with --help for usage)") optserror = error' . (++ " (run with --help for usage)")
@ -448,12 +449,8 @@ aliasesFromOpts = map parseAlias . alias_
alias' = case alias of ('=':rest) -> rest alias' = case alias of ('=':rest) -> rest
_ -> orig _ -> orig
printModeHelpAndExit mode = putStr (showModeHelp mode) >> exitSuccess
showModeHelp = showText defaultWrap . helpText HelpFormatDefault showModeHelp = showText defaultWrap . helpText HelpFormatDefault
printVersionAndExit = putStrLn progversion >> exitSuccess
tests_Hledger_Cli_Options = TestList tests_Hledger_Cli_Options = TestList
[ [
] ]