2009-01-18 22:02:08 +03:00
|
|
|
-- #!/usr/bin/env runhaskell
|
|
|
|
{-# OPTIONS_GHC -cpp #-}
|
2008-10-01 11:04:47 +04:00
|
|
|
{-|
|
|
|
|
hledger - a ledger-compatible text-based accounting tool.
|
2007-02-18 21:12:02 +03:00
|
|
|
|
2009-01-18 00:03:34 +03:00
|
|
|
Copyright (c) 2007-2009 Simon Michael <simon@joyful.com>
|
2008-10-01 12:44:23 +04:00
|
|
|
Released under GPL version 3 or later.
|
2008-10-01 11:04:47 +04:00
|
|
|
|
2009-01-18 00:03:34 +03:00
|
|
|
hledger is a haskell clone of John Wiegley's "ledger" text-based
|
|
|
|
accounting tool (http://newartisans.com/software/ledger.html).
|
|
|
|
It generates ledger-compatible register & balance reports from a plain
|
|
|
|
text ledger file, and demonstrates a functional implementation of ledger.
|
|
|
|
For more information, see hledger's home page: http://joyful.com/hledger
|
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
|
2008-12-05 11:51:14 +03:00
|
|
|
> > l <- ledgerfromfilewithopts [] [] "sample.ledger"
|
2008-10-12 13:17:21 +04:00
|
|
|
> > balance [] [] l
|
|
|
|
> $-1 assets
|
|
|
|
> $2 expenses
|
|
|
|
> $-2 income
|
2008-12-05 11:51:14 +03:00
|
|
|
> $1 liabilities
|
2008-10-12 13:17:21 +04:00
|
|
|
> > register [] ["income","expenses"] l
|
2008-12-05 11:51:14 +03:00
|
|
|
> 2008/01/01 income income:salary $-1 $-1
|
|
|
|
> 2008/06/01 gift income:gifts $-1 $-2
|
|
|
|
> 2008/06/03 eat & shop expenses:food $1 $-1
|
2008-10-12 13:17:21 +04:00
|
|
|
> 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 (
|
2009-01-25 15:52:28 +03:00
|
|
|
-- for easy ghci access
|
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,
|
2009-01-25 15:52:28 +03:00
|
|
|
#ifdef HAPPS
|
|
|
|
module WebCommand,
|
|
|
|
#endif
|
2008-10-12 13:17:21 +04:00
|
|
|
)
|
2007-02-10 22:16:56 +03:00
|
|
|
where
|
2008-12-10 00:00:46 +03:00
|
|
|
import Control.Monad.Error
|
2008-09-28 07:43:59 +04:00
|
|
|
import qualified Data.Map as Map (lookup)
|
2008-12-10 00:00:46 +03:00
|
|
|
import System.IO
|
|
|
|
|
2009-02-27 05:55:54 +03:00
|
|
|
import Version (versionmsg)
|
2008-10-10 07:32:12 +04:00
|
|
|
import Ledger
|
2008-10-12 13:17:21 +04:00
|
|
|
import Utils
|
|
|
|
import Options
|
2009-01-25 10:06:59 +03:00
|
|
|
import Tests
|
2008-10-10 07:32:12 +04:00
|
|
|
import BalanceCommand
|
|
|
|
import PrintCommand
|
|
|
|
import RegisterCommand
|
2009-01-20 06:48:05 +03:00
|
|
|
#ifdef VTY
|
2009-01-25 15:52:28 +03:00
|
|
|
import UICommand
|
2009-01-18 22:02:08 +03:00
|
|
|
#endif
|
2009-01-20 07:02:21 +03:00
|
|
|
#ifdef ANSI
|
2009-01-25 15:52:28 +03:00
|
|
|
import ANSICommand
|
2009-01-20 07:02:21 +03:00
|
|
|
#endif
|
2009-01-20 07:04:40 +03:00
|
|
|
#ifdef HAPPS
|
2009-01-25 15:52:28 +03:00
|
|
|
import WebCommand
|
2009-01-20 07:04:40 +03:00
|
|
|
#endif
|
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
|
2008-11-26 07:04:05 +03:00
|
|
|
| Help `elem` opts = putStr $ usage
|
2009-01-20 08:57:25 +03:00
|
|
|
| Version `elem` opts = putStr versionmsg
|
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
|
2009-01-20 06:48:05 +03:00
|
|
|
#ifdef VTY
|
2009-01-25 15:52:28 +03:00
|
|
|
| cmd `isPrefixOf` "ui" = parseLedgerAndDo opts args ui
|
2009-01-20 07:02:21 +03:00
|
|
|
#endif
|
|
|
|
#ifdef ANSI
|
2009-01-25 15:52:28 +03:00
|
|
|
| cmd `isPrefixOf` "ansi" = parseLedgerAndDo opts args ansi
|
2009-01-20 07:04:40 +03:00
|
|
|
#endif
|
|
|
|
#ifdef HAPPS
|
2009-01-25 15:52:28 +03:00
|
|
|
| cmd `isPrefixOf` "web" = parseLedgerAndDo opts args web
|
2009-01-18 22:02:08 +03:00
|
|
|
#endif
|
2008-11-22 07:49:00 +03:00
|
|
|
| cmd `isPrefixOf` "test" = runtests opts args >> return ()
|
2008-11-26 07:04:05 +03: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-11-26 02:52:42 +03:00
|
|
|
parseLedgerAndDo opts args cmd = do
|
2008-12-10 10:29:08 +03:00
|
|
|
f <- ledgerFilePathFromOpts opts
|
2009-01-17 22:23:21 +03:00
|
|
|
-- XXX we read the file twice - inelegant
|
|
|
|
-- and, doesn't work with stdin. kludge it, stdin won't work with ui command
|
|
|
|
let f' = if f == "-" then "/dev/null" else f
|
|
|
|
rawtext <- readFile f'
|
2009-01-25 10:06:59 +03:00
|
|
|
t <- getCurrentLocalTime
|
|
|
|
let runcmd = cmd opts args . prepareLedger opts args t rawtext
|
|
|
|
return f >>= runErrorT . parseLedgerFile t >>= either (hPutStrLn stderr) runcmd
|