2007-02-10 22:16:56 +03:00
|
|
|
#!/usr/bin/env runhaskell
|
2007-02-10 20:36:50 +03:00
|
|
|
-- hledger - ledger-compatible money management utilities (& haskell study)
|
2007-01-28 11:26:25 +03:00
|
|
|
-- GPLv3, (c) Simon Michael & contributors,
|
2007-02-09 03:18:20 +03:00
|
|
|
-- John Wiegley's ledger is at http://newartisans.com/ledger.html .
|
2007-01-28 13:30:24 +03:00
|
|
|
|
2007-02-10 22:16:56 +03:00
|
|
|
module Main -- almost all IO is handled here
|
|
|
|
where
|
2007-02-10 20:36:50 +03:00
|
|
|
|
2007-01-30 12:07:12 +03:00
|
|
|
import System (getArgs)
|
2007-02-10 08:09:42 +03:00
|
|
|
import Data.List (isPrefixOf)
|
2007-02-10 20:36:50 +03:00
|
|
|
import Test.HUnit (runTestTT)
|
2007-02-10 22:16:56 +03:00
|
|
|
import Test.QuickCheck (quickCheck)
|
2007-02-10 20:36:50 +03:00
|
|
|
import Text.ParserCombinators.Parsec (parseFromFile, ParseError)
|
2007-01-28 00:51:59 +03:00
|
|
|
|
2007-01-30 12:07:12 +03:00
|
|
|
import Options
|
2007-02-11 02:10:04 +03:00
|
|
|
import Models
|
2007-02-09 04:23:12 +03:00
|
|
|
import Parse
|
|
|
|
import Tests
|
2007-02-09 03:18:20 +03:00
|
|
|
|
|
|
|
main :: IO ()
|
2007-01-30 12:07:12 +03:00
|
|
|
main = do
|
2007-02-10 08:09:42 +03:00
|
|
|
(opts, args) <- (getArgs >>= getOptions)
|
|
|
|
if args == []
|
|
|
|
then register []
|
|
|
|
else
|
2007-02-10 22:16:56 +03:00
|
|
|
let (command, args') = (head args, tail args) in
|
|
|
|
if "reg" `isPrefixOf` command then (register args')
|
|
|
|
else if "bal" `isPrefixOf` command then balance args'
|
|
|
|
else if "test" `isPrefixOf` command then test
|
|
|
|
else error "could not recognise your command"
|
2007-02-09 06:17:12 +03:00
|
|
|
|
|
|
|
-- commands
|
|
|
|
|
2007-02-10 20:36:50 +03:00
|
|
|
test :: IO ()
|
|
|
|
test = do
|
2007-02-10 22:16:56 +03:00
|
|
|
putStrLn "hunit "
|
|
|
|
runTestTT tests
|
|
|
|
putStr "quickcheck "
|
|
|
|
mapM quickCheck props
|
2007-02-10 20:36:50 +03:00
|
|
|
return ()
|
|
|
|
|
2007-02-10 08:09:42 +03:00
|
|
|
register :: [String] -> IO ()
|
|
|
|
register args = do
|
|
|
|
p <- parseLedgerFile ledgerFilePath
|
2007-02-10 20:36:50 +03:00
|
|
|
case p of Left e -> parseError e
|
|
|
|
Right l -> printRegister l
|
2007-02-10 08:09:42 +03:00
|
|
|
|
|
|
|
balance :: [String] -> IO ()
|
|
|
|
balance args = do
|
2007-02-09 06:17:12 +03:00
|
|
|
p <- parseLedgerFile ledgerFilePath
|
2007-02-10 20:36:50 +03:00
|
|
|
case p of Left e -> parseError e
|
|
|
|
Right l -> printBalances l
|
|
|
|
|
|
|
|
-- utils
|
|
|
|
|
|
|
|
parseLedgerFile :: IO String -> IO (Either ParseError Ledger)
|
|
|
|
parseLedgerFile f = f >>= parseFromFile ledger
|
|
|
|
|
|
|
|
printRegister :: Ledger -> IO ()
|
|
|
|
printRegister l = putStr $ showRegisterEntries (entries l) 0
|
|
|
|
|
|
|
|
printBalances :: Ledger -> IO ()
|
|
|
|
printBalances l = putStr $ showRegisterEntries (entries l) 0
|
2007-02-09 06:17:12 +03:00
|
|
|
|