hledger/hledger.hs

61 lines
1.5 KiB
Haskell
Raw Normal View History

2007-01-30 12:07:12 +03:00
#!/usr/bin/runhaskell
2007-02-10 20:36:50 +03:00
-- hledger - ledger-compatible money management utilities (& haskell study)
-- 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 20:36:50 +03:00
module Main where
2007-01-30 12:07:12 +03:00
import System (getArgs)
import Data.List (isPrefixOf)
2007-02-10 20:36:50 +03:00
import Test.HUnit (runTestTT)
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-09 04:23:12 +03:00
import Types
import Parse
import Tests
2007-02-09 03:18:20 +03:00
main :: IO ()
2007-01-30 12:07:12 +03:00
main = do
(opts, args) <- (getArgs >>= getOptions)
test
if args == []
then register []
else
let (command, args) = (head args, tail args) in
if "reg" `isPrefixOf` command then register args
else if "bal" `isPrefixOf` command then balance args
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
runTestTT hunittests
-- quickCheck prop1
return ()
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
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