2007-05-02 11:37:08 +04:00
|
|
|
#!/usr/bin/env runhaskell
|
2007-02-18 21:12:02 +03:00
|
|
|
{-
|
2007-03-10 03:06:48 +03:00
|
|
|
hledger - ledger-compatible money management tool (& haskell study)
|
|
|
|
GPLv3, (c) Simon Michael & contributors
|
2007-05-01 11:15:53 +04:00
|
|
|
A port of John Wiegley's ledger at http://newartisans.com/ledger.html
|
2007-02-18 21:12:02 +03:00
|
|
|
|
2007-07-02 20:43:14 +04:00
|
|
|
See Types.hs for a code overview.
|
2007-02-18 21:12:02 +03:00
|
|
|
-}
|
2007-01-28 13:30:24 +03:00
|
|
|
|
2007-02-16 14:51:30 +03:00
|
|
|
module Main
|
2007-02-10 22:16:56 +03:00
|
|
|
where
|
2007-02-11 02:24:33 +03:00
|
|
|
import System
|
2007-02-16 14:51:30 +03:00
|
|
|
import Text.ParserCombinators.Parsec (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-16 14:51:30 +03:00
|
|
|
import Utils
|
2007-02-09 03:18:20 +03:00
|
|
|
|
2007-03-12 10:40:33 +03:00
|
|
|
|
2007-02-09 03:18:20 +03:00
|
|
|
main :: IO ()
|
2007-01-30 12:07:12 +03:00
|
|
|
main = do
|
2007-03-12 10:40:33 +03:00
|
|
|
(opts, (cmd:args)) <- getArgs >>= parseOptions
|
2007-03-12 11:17:51 +03:00
|
|
|
let (acctpats, descpats) = parseLedgerPatternArgs args
|
|
|
|
run cmd opts acctpats descpats
|
|
|
|
where run cmd opts acctpats descpats
|
2007-05-01 09:55:35 +04:00
|
|
|
| Help `elem` opts = putStr usage
|
2007-03-12 11:17:51 +03:00
|
|
|
| cmd `isPrefixOf` "register" = register opts acctpats descpats
|
|
|
|
| cmd `isPrefixOf` "balance" = balance opts acctpats descpats
|
2007-03-12 12:38:02 +03:00
|
|
|
| cmd `isPrefixOf` "test" = selftest
|
2007-03-12 10:58:44 +03:00
|
|
|
| otherwise = putStr usage
|
2007-02-09 06:17:12 +03:00
|
|
|
|
|
|
|
-- commands
|
|
|
|
|
2007-03-12 11:17:51 +03:00
|
|
|
register :: [Flag] -> [String] -> [String] -> IO ()
|
|
|
|
register opts acctpats descpats = do
|
|
|
|
doWithLedger opts printRegister
|
2007-03-12 10:40:33 +03:00
|
|
|
where
|
2007-07-02 22:57:37 +04:00
|
|
|
printRegister l =
|
2007-03-12 10:40:33 +03:00
|
|
|
putStr $ showTransactionsWithBalances
|
2007-07-02 23:39:34 +04:00
|
|
|
(ledgerTransactionsMatching (acctpats,descpats) l)
|
2007-03-12 10:40:33 +03:00
|
|
|
0
|
|
|
|
|
2007-03-12 11:17:51 +03:00
|
|
|
balance :: [Flag] -> [String] -> [String] -> IO ()
|
|
|
|
balance opts acctpats _ = do
|
|
|
|
doWithLedger opts printBalance
|
2007-03-12 10:40:33 +03:00
|
|
|
where
|
2007-07-02 22:57:37 +04:00
|
|
|
printBalance l =
|
2007-07-02 23:39:34 +04:00
|
|
|
putStr $ showLedgerAccounts l acctpats showsubs maxdepth
|
2007-03-12 10:40:33 +03:00
|
|
|
where
|
|
|
|
showsubs = (ShowSubs `elem` opts)
|
|
|
|
maxdepth = case (acctpats, showsubs) of
|
|
|
|
([],False) -> 1
|
|
|
|
otherwise -> 9999
|
|
|
|
|
2007-03-12 12:38:02 +03:00
|
|
|
selftest :: IO ()
|
|
|
|
selftest = do
|
|
|
|
Tests.tests
|
|
|
|
Tests.props
|
|
|
|
-- Amount.tests
|
2007-02-10 20:36:50 +03:00
|
|
|
return ()
|
|
|
|
|
|
|
|
-- utils
|
|
|
|
|
2007-07-02 23:15:39 +04:00
|
|
|
doWithLedger :: [Flag] -> (Ledger -> IO ()) -> IO ()
|
2007-03-12 10:40:33 +03:00
|
|
|
doWithLedger opts cmd = do
|
2007-07-03 09:43:00 +04:00
|
|
|
ledgerFilePath opts >>= (trace "parsing" $ parseLedgerFile) >>= doWithParsed cmd
|
2007-02-10 20:36:50 +03:00
|
|
|
|
2007-07-02 23:15:39 +04:00
|
|
|
doWithParsed :: (Ledger -> IO ()) -> (Either ParseError RawLedger) -> IO ()
|
2007-07-02 22:57:37 +04:00
|
|
|
doWithParsed cmd parsed = do
|
2007-03-12 10:40:33 +03:00
|
|
|
case parsed of Left e -> parseError e
|
2007-07-02 22:57:37 +04:00
|
|
|
Right l -> cmd $ cacheLedger l
|
2007-03-11 00:24:57 +03:00
|
|
|
|
2007-03-11 03:56:03 +03:00
|
|
|
-- interactive testing:
|
|
|
|
--
|
2007-03-12 10:40:33 +03:00
|
|
|
-- p <- ledgerFilePath [] >>= parseLedgerFile
|
2007-07-02 23:15:39 +04:00
|
|
|
-- let l = either (\_ -> RawLedger [] [] []) id p
|
2007-07-02 23:39:34 +04:00
|
|
|
-- let ant = rawLedgerAccountNameTree l
|
|
|
|
-- let at = rawLedgerAccountTreeMatching l [] True 999
|
|
|
|
-- putStr $ drawTree $ treemap show $ rawLedgerAccountTreeMatching l ["a"] False 999
|