2007-05-02 11:37:08 +04:00
|
|
|
#!/usr/bin/env runhaskell
|
2008-10-01 11:04:47 +04:00
|
|
|
{-|
|
|
|
|
hledger - a ledger-compatible text-based accounting tool.
|
2007-02-18 21:12:02 +03:00
|
|
|
|
2008-10-01 12:44:23 +04:00
|
|
|
Copyright (c) 2007-2008 Simon Michael <simon@joyful.com>
|
|
|
|
Released under GPL version 3 or later.
|
2008-10-01 11:04:47 +04:00
|
|
|
|
2008-10-01 12:44:23 +04:00
|
|
|
This is a minimal haskell clone of John Wiegley's ledger
|
2008-10-01 13:33:05 +04:00
|
|
|
<http://newartisans.com/software/ledger.html>. hledger generates
|
2008-10-03 06:25:18 +04:00
|
|
|
simple ledger-compatible register & balance reports from a plain text
|
2008-10-10 15:55:12 +04:00
|
|
|
ledger file, and demonstrates a functional implementation of ledger.
|
2008-10-01 11:04:47 +04:00
|
|
|
|
2008-10-12 13:17:21 +04:00
|
|
|
You can use the command line:
|
|
|
|
|
|
|
|
> $ hledger --help
|
|
|
|
|
|
|
|
or ghci:
|
|
|
|
|
|
|
|
> $ ghci hledger
|
|
|
|
> > l <- ledgerfromfile "sample.ledger"
|
|
|
|
> > balance [] [] l
|
|
|
|
> $-1 assets
|
|
|
|
> $2 expenses
|
|
|
|
> $-2 income
|
|
|
|
> $1 liabilities:debts
|
|
|
|
> > register [] ["income","expenses"] l
|
|
|
|
> 2007/01/01 income income:salary $-1 $-1
|
|
|
|
> 2007/01/01 gift income:gifts $-1 $-2
|
|
|
|
> 2007/01/01 eat & shop expenses:food $1 $-1
|
|
|
|
> expenses:supplies $1 0
|
|
|
|
|
2007-02-18 21:12:02 +03:00
|
|
|
-}
|
2007-01-28 13:30:24 +03:00
|
|
|
|
2008-10-12 13:17:21 +04:00
|
|
|
module Main (
|
2008-10-13 01:52:48 +04:00
|
|
|
module Main,
|
2008-10-12 13:17:21 +04:00
|
|
|
module Utils,
|
|
|
|
module Options,
|
|
|
|
module BalanceCommand,
|
|
|
|
module PrintCommand,
|
|
|
|
module RegisterCommand,
|
|
|
|
)
|
2007-02-10 22:16:56 +03:00
|
|
|
where
|
2008-09-28 07:43:59 +04:00
|
|
|
import qualified Data.Map as Map (lookup)
|
2008-10-10 07:32:12 +04:00
|
|
|
import Ledger
|
2008-10-12 13:17:21 +04:00
|
|
|
import Utils
|
|
|
|
import Options
|
2008-10-10 07:32:12 +04:00
|
|
|
import BalanceCommand
|
|
|
|
import PrintCommand
|
|
|
|
import RegisterCommand
|
2008-10-10 05:53:39 +04:00
|
|
|
import Tests
|
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
|
2008-10-08 21:00:22 +04:00
|
|
|
(opts, cmd, args) <- parseArguments
|
|
|
|
run cmd opts args
|
2008-10-10 07:32:12 +04:00
|
|
|
where
|
|
|
|
run cmd opts args
|
|
|
|
| Help `elem` opts = putStr usage
|
|
|
|
| Version `elem` opts = putStr version
|
2008-10-12 13:17:21 +04:00
|
|
|
| cmd `isPrefixOf` "balance" = parseLedgerAndDo opts args balance
|
|
|
|
| cmd `isPrefixOf` "print" = parseLedgerAndDo opts args print'
|
|
|
|
| cmd `isPrefixOf` "register" = parseLedgerAndDo opts args register
|
2008-10-10 15:55:12 +04:00
|
|
|
| cmd `isPrefixOf` "test" = runtests >> return ()
|
2008-10-10 07:32:12 +04:00
|
|
|
| otherwise = putStr usage
|
2008-10-03 06:25:18 +04:00
|
|
|
|
|
|
|
-- | parse the user's specified ledger file and do some action with it
|
|
|
|
-- (or report a parse error). This function makes the whole thing go.
|
2008-10-10 07:32:12 +04:00
|
|
|
parseLedgerAndDo :: [Opt] -> [String] -> ([Opt] -> [String] -> Ledger -> IO ()) -> IO ()
|
2008-10-08 21:48:11 +04:00
|
|
|
parseLedgerAndDo opts args cmd =
|
2008-10-08 22:02:34 +04:00
|
|
|
ledgerFilePathFromOpts opts >>= parseLedgerFile >>= either printParseError runthecommand
|
2008-10-08 21:48:11 +04:00
|
|
|
where
|
2008-10-15 21:04:47 +04:00
|
|
|
runthecommand = cmd opts args . cacheLedger . normaliseRawLedgerAmounts . filterRawLedger begin end descpats
|
2008-10-08 21:48:11 +04:00
|
|
|
begin = beginDateFromOpts opts
|
|
|
|
end = endDateFromOpts opts
|
2008-10-15 21:04:47 +04:00
|
|
|
descpats = snd $ parseAccountDescriptionArgs args
|
2007-07-03 21:25:16 +04:00
|
|
|
|