module Hledger.Cli.Main where
#if __GLASGOW_HASKELL__ <= 610
import Prelude hiding (putStr, putStrLn)
import System.IO.UTF8
#endif
import Hledger.Data
import Hledger.Cli.Commands
import Hledger.Cli.Options
import Hledger.Cli.Tests
import Hledger.Cli.Utils (withJournalDo)
import Hledger.Cli.Version (progversionstr, binaryfilename)
main :: IO ()
main = do
(opts, args) <- parseArgumentsWith options_cli
run opts args
where
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'
| cmd `isPrefixOf` "register" = withJournalDo opts args cmd register
| cmd `isPrefixOf` "histogram" = withJournalDo opts args cmd histogram
| cmd `isPrefixOf` "add" = withJournalDo opts args cmd add
| cmd `isPrefixOf` "stats" = withJournalDo opts args cmd stats
| cmd `isPrefixOf` "test" = runtests opts args >> return ()
| otherwise = argsError $ "command "++cmd++" is unrecognized."