2008-10-10 05:53:39 +04:00
|
|
|
module Options
|
2007-02-10 20:36:50 +03:00
|
|
|
where
|
2008-10-08 21:00:22 +04:00
|
|
|
import System
|
2007-01-30 12:07:12 +03:00
|
|
|
import System.Console.GetOpt
|
2007-03-12 10:40:33 +03:00
|
|
|
import System.Directory
|
2008-11-24 01:15:51 +03:00
|
|
|
import Text.Printf
|
2008-11-26 07:04:05 +03:00
|
|
|
import Ledger.Parse
|
2008-11-22 15:37:55 +03:00
|
|
|
import Ledger.Dates
|
2008-11-25 00:51:31 +03:00
|
|
|
import Ledger.Utils
|
|
|
|
|
2007-02-16 14:51:30 +03:00
|
|
|
|
2008-11-26 08:21:44 +03:00
|
|
|
defaultfile = "~/.ledger"
|
|
|
|
fileenvvar = "LEDGER"
|
2008-11-25 22:29:33 +03:00
|
|
|
usagehdr = "Usage: hledger [OPTS] COMMAND [ACCTPATTERNS] [-- DESCPATTERNS]\n" ++
|
|
|
|
"\n" ++
|
|
|
|
"Options (before command, unless using --options-anywhere):"
|
2008-10-17 05:29:53 +04:00
|
|
|
usageftr = "\n" ++
|
|
|
|
"Commands (may be abbreviated):\n" ++
|
2008-11-24 03:14:28 +03:00
|
|
|
" balance - show account balances\n" ++
|
|
|
|
" print - show formatted ledger entries\n" ++
|
|
|
|
" register - show register transactions\n" ++
|
2008-10-17 05:29:53 +04:00
|
|
|
"\n" ++
|
2008-11-25 22:29:33 +03:00
|
|
|
"Account and description patterns are regular expressions which filter by\n" ++
|
|
|
|
"account name and entry description. Prefix a pattern with - to negate it,\n" ++
|
|
|
|
"and separate account and description patterns with --.\n" ++
|
|
|
|
"(With --options-anywhere, use ^ and ^^.)\n" ++
|
2008-11-22 07:49:20 +03:00
|
|
|
"\n" ++
|
2008-11-24 03:14:28 +03:00
|
|
|
"Also: hledger [-v] test [TESTPATTERNS] to run self-tests.\n" ++
|
|
|
|
"\n"
|
2008-11-26 08:21:44 +03:00
|
|
|
usage = usageInfo usagehdr options ++ usageftr
|
2007-02-16 12:00:17 +03:00
|
|
|
|
2008-10-08 21:24:59 +04:00
|
|
|
-- | Command-line options we accept.
|
|
|
|
options :: [OptDescr Opt]
|
|
|
|
options = [
|
2008-11-24 03:14:28 +03:00
|
|
|
Option ['f'] ["file"] (ReqArg File "FILE") filehelp,
|
|
|
|
Option ['b'] ["begin"] (ReqArg Begin "Y/M/D") "report on entries on or after this date",
|
|
|
|
Option ['e'] ["end"] (ReqArg End "Y/M/D") "report on entries prior to this date",
|
|
|
|
Option ['C'] ["cleared"] (NoArg Cleared) "report only on cleared entries",
|
|
|
|
Option ['B'] ["cost","basis"] (NoArg CostBasis) "report cost basis of commodities",
|
|
|
|
Option [] ["depth"] (ReqArg Depth "N") "balance report: maximum account depth to show",
|
2008-11-27 00:18:24 +03:00
|
|
|
Option ['d'] ["display"] (ReqArg Display "EXPR") ("display only transactions matching simple EXPR\n" ++
|
|
|
|
"(where EXPR is 'dOP[Y/M/D]', OP is <, <=, =, >=, >)"),
|
2008-11-24 03:14:28 +03:00
|
|
|
Option ['E'] ["empty"] (NoArg Empty) "balance report: show accounts with zero balance",
|
|
|
|
Option ['R'] ["real"] (NoArg Real) "report only on real (non-virtual) transactions",
|
2008-11-25 22:29:33 +03:00
|
|
|
Option [] ["options-anywhere"] (NoArg OptionsAnywhere) "allow options anywhere, use ^ to negate patterns",
|
2008-11-24 03:14:28 +03:00
|
|
|
Option ['n'] ["collapse"] (NoArg Collapse) "balance report: no grand total",
|
|
|
|
Option ['s'] ["subtotal"] (NoArg SubTotal) "balance report: show subaccounts",
|
|
|
|
Option ['h'] ["help"] (NoArg Help) "show this help",
|
|
|
|
Option ['v'] ["verbose"] (NoArg Verbose) "verbose test output",
|
|
|
|
Option ['V'] ["version"] (NoArg Version) "show version"
|
2008-10-08 21:24:59 +04:00
|
|
|
]
|
2008-11-24 03:14:28 +03:00
|
|
|
where
|
|
|
|
filehelp = printf "ledger file; - means use standard input. Defaults\nto the %s environment variable or %s"
|
|
|
|
fileenvvar defaultfile
|
2007-02-09 04:23:12 +03:00
|
|
|
|
2008-10-08 21:24:59 +04:00
|
|
|
-- | An option value from a command-line flag.
|
|
|
|
data Opt =
|
2007-03-12 10:40:33 +03:00
|
|
|
File String |
|
2008-10-08 21:00:22 +04:00
|
|
|
Begin String |
|
|
|
|
End String |
|
2008-10-16 13:04:44 +04:00
|
|
|
Cleared |
|
2008-11-22 23:35:17 +03:00
|
|
|
CostBasis |
|
2008-11-22 16:11:54 +03:00
|
|
|
Depth String |
|
2008-11-25 00:51:31 +03:00
|
|
|
Display String |
|
2008-11-22 12:39:58 +03:00
|
|
|
Empty |
|
2008-10-16 13:50:16 +04:00
|
|
|
Real |
|
2008-11-25 22:29:33 +03:00
|
|
|
OptionsAnywhere |
|
2008-11-22 12:46:57 +03:00
|
|
|
Collapse |
|
2008-10-17 20:58:09 +04:00
|
|
|
SubTotal |
|
2007-05-01 09:55:35 +04:00
|
|
|
Help |
|
2008-11-22 07:49:00 +03:00
|
|
|
Verbose |
|
2007-03-12 10:40:33 +03:00
|
|
|
Version
|
|
|
|
deriving (Show,Eq)
|
2007-02-16 15:24:13 +03:00
|
|
|
|
2008-11-24 01:30:20 +03:00
|
|
|
versionno = "0.3pre"
|
2008-11-24 01:15:51 +03:00
|
|
|
version = printf "hledger version %s \n" versionno :: String
|
2008-10-10 05:36:21 +04:00
|
|
|
|
2008-10-08 21:00:22 +04:00
|
|
|
-- | Parse the command-line arguments into ledger options, ledger command
|
2008-11-26 07:04:05 +03:00
|
|
|
-- name, and ledger command arguments. Also any dates in the options are
|
|
|
|
-- converted to full YYYY/MM/DD format, while we are in the IO monad
|
|
|
|
-- and can get the current time.
|
2008-10-08 21:24:59 +04:00
|
|
|
parseArguments :: IO ([Opt], String, [String])
|
2008-10-08 21:00:22 +04:00
|
|
|
parseArguments = do
|
|
|
|
args <- getArgs
|
2008-11-25 22:29:33 +03:00
|
|
|
let order = if "--options-anywhere" `elem` args then Permute else RequireOrder
|
|
|
|
case (getOpt order options args) of
|
2008-11-26 07:04:05 +03:00
|
|
|
(opts,cmd:args,[]) -> do {opts' <- fixDates opts; return (opts',cmd,args)}
|
|
|
|
(opts,[],[]) -> do {opts' <- fixDates opts; return (opts',[],[])}
|
|
|
|
(opts,_,errs) -> ioError (userError (concat errs ++ usage))
|
|
|
|
|
|
|
|
-- | Convert any fuzzy/relative dates within these option values to
|
|
|
|
-- explicit ones, based on today's date.
|
|
|
|
fixDates :: [Opt] -> IO [Opt]
|
|
|
|
fixDates opts = do
|
2008-11-26 07:51:15 +03:00
|
|
|
t <- today
|
|
|
|
return $ map (fixopt t) opts
|
2008-11-26 07:04:05 +03:00
|
|
|
where
|
2008-11-26 07:51:15 +03:00
|
|
|
fixopt t (Begin s) = Begin $ fixdate t s
|
|
|
|
fixopt t (End s) = End $ fixdate t s
|
|
|
|
fixopt t (Display s) = -- hacky
|
2008-11-26 07:04:05 +03:00
|
|
|
Display $ gsubRegexPRBy "\\[.+?\\]" fixbracketeddate s
|
2008-11-26 07:51:15 +03:00
|
|
|
where fixbracketeddate s = "[" ++ (fixdate t $ init $ tail s) ++ "]"
|
2008-11-26 07:04:05 +03:00
|
|
|
fixopt _ o = o
|
|
|
|
|
|
|
|
-- | Convert a fuzzy date string to an explicit yyyy/mm/dd date, using the
|
|
|
|
-- provided today's date for defaults.
|
2008-11-26 07:51:15 +03:00
|
|
|
fixdate :: Date -> String -> String
|
|
|
|
fixdate t s = printf "%04s/%02s/%02s" y' m' d'
|
|
|
|
where
|
|
|
|
(ty,tm,td) = dateComponents t
|
2008-11-26 07:04:05 +03:00
|
|
|
(y,m,d) = fromparse $ parsewith smartdate $ map toLower s
|
|
|
|
(y',m',d') = case (y,m,d) of
|
2008-11-26 07:51:15 +03:00
|
|
|
("","","today") -> (show ty,show tm,show td)
|
|
|
|
("","","yesterday") -> (show y, show m, show d)
|
|
|
|
where (y,m,d) = toGregorian $ addDays (-1) $ fromGregorian ty tm td
|
|
|
|
("","","tomorrow") -> (show y, show m, show d)
|
|
|
|
where (y,m,d) = toGregorian $ addDays 1 $ fromGregorian ty tm td
|
|
|
|
("","",d) -> (show ty,show tm,d)
|
|
|
|
("",m,d) -> (show ty,m,d)
|
|
|
|
otherwise -> (y,m,d)
|
2007-07-03 21:25:16 +04:00
|
|
|
|
2008-10-08 21:00:22 +04:00
|
|
|
-- | Get the ledger file path from options, an environment variable, or a default
|
2008-10-08 21:24:59 +04:00
|
|
|
ledgerFilePathFromOpts :: [Opt] -> IO String
|
2008-10-08 21:00:22 +04:00
|
|
|
ledgerFilePathFromOpts opts = do
|
|
|
|
envordefault <- getEnv fileenvvar `catch` \_ -> return defaultfile
|
2007-03-12 10:40:33 +03:00
|
|
|
paths <- mapM tildeExpand $ [envordefault] ++ (concatMap getfile opts)
|
|
|
|
return $ last paths
|
|
|
|
where
|
|
|
|
getfile (File s) = [s]
|
|
|
|
getfile _ = []
|
2007-02-18 21:12:02 +03:00
|
|
|
|
2008-10-08 21:00:22 +04:00
|
|
|
-- | Expand ~ in a file path (does not handle ~name).
|
|
|
|
tildeExpand :: FilePath -> IO FilePath
|
|
|
|
tildeExpand ('~':[]) = getHomeDirectory
|
|
|
|
tildeExpand ('~':'/':xs) = getHomeDirectory >>= return . (++ ('/':xs))
|
|
|
|
--handle ~name, requires -fvia-C or ghc 6.8:
|
|
|
|
--import System.Posix.User
|
|
|
|
-- tildeExpand ('~':xs) = do let (user, path) = span (/= '/') xs
|
|
|
|
-- pw <- getUserEntryForName user
|
|
|
|
-- return (homeDirectory pw ++ path)
|
2007-03-12 10:40:33 +03:00
|
|
|
tildeExpand xs = return xs
|
2008-10-08 21:00:22 +04:00
|
|
|
|
2008-11-22 15:37:55 +03:00
|
|
|
-- | Get the value of the begin date option, if any.
|
2008-11-26 07:04:05 +03:00
|
|
|
beginDateFromOpts :: [Opt] -> Maybe Date
|
2008-11-26 02:52:42 +03:00
|
|
|
beginDateFromOpts opts =
|
2008-11-26 07:04:05 +03:00
|
|
|
if null beginopts
|
|
|
|
then Nothing
|
|
|
|
else Just $ parsedate $ printf "%04s/%02s/%02s" y m d
|
2008-10-08 21:00:22 +04:00
|
|
|
where
|
|
|
|
beginopts = concatMap getbegindate opts
|
|
|
|
getbegindate (Begin s) = [s]
|
|
|
|
getbegindate _ = []
|
|
|
|
defaultdate = ""
|
2008-11-26 07:04:05 +03:00
|
|
|
(y,m,d) = fromparse $ parsewith smartdate $ last beginopts
|
2008-10-08 21:00:22 +04:00
|
|
|
|
2008-11-22 15:37:55 +03:00
|
|
|
-- | Get the value of the end date option, if any.
|
2008-11-26 07:04:05 +03:00
|
|
|
endDateFromOpts :: [Opt] -> Maybe Date
|
|
|
|
endDateFromOpts opts =
|
|
|
|
if null endopts
|
|
|
|
then Nothing
|
|
|
|
else Just $ parsedate $ printf "%04s/%02s/%02s" y m d
|
2008-10-08 21:00:22 +04:00
|
|
|
where
|
|
|
|
endopts = concatMap getenddate opts
|
|
|
|
getenddate (End s) = [s]
|
|
|
|
getenddate _ = []
|
|
|
|
defaultdate = ""
|
2008-11-26 07:04:05 +03:00
|
|
|
(y,m,d) = fromparse $ parsewith smartdate $ last endopts
|
2007-03-12 10:40:33 +03:00
|
|
|
|
2008-11-22 16:11:54 +03:00
|
|
|
-- | Get the value of the depth option, if any.
|
|
|
|
depthFromOpts :: [Opt] -> Maybe Int
|
2008-11-24 00:21:18 +03:00
|
|
|
depthFromOpts opts =
|
2008-11-22 16:11:54 +03:00
|
|
|
case depthopts of
|
|
|
|
(x:_) -> Just $ read x
|
|
|
|
_ -> Nothing
|
|
|
|
where
|
|
|
|
depthopts = concatMap getdepth opts
|
|
|
|
getdepth (Depth s) = [s]
|
|
|
|
getdepth _ = []
|
|
|
|
|
2008-11-25 00:51:31 +03:00
|
|
|
-- | Get the value of the display option, if any.
|
|
|
|
displayFromOpts :: [Opt] -> Maybe String
|
|
|
|
displayFromOpts opts =
|
|
|
|
case displayopts of
|
|
|
|
(s:_) -> Just s
|
|
|
|
_ -> Nothing
|
|
|
|
where
|
|
|
|
displayopts = concatMap getdisplay opts
|
|
|
|
getdisplay (Display s) = [s]
|
|
|
|
getdisplay _ = []
|
|
|
|
|
2008-10-08 22:02:34 +04:00
|
|
|
-- | Gather any ledger-style account/description pattern arguments into
|
|
|
|
-- two lists. These are 0 or more account patterns optionally followed by
|
2008-11-25 22:29:33 +03:00
|
|
|
-- a separator and then 0 or more description patterns. The separator is
|
|
|
|
-- usually -- but with --options-anywhere is ^^ so we need to provide the
|
|
|
|
-- options as well.
|
|
|
|
parseAccountDescriptionArgs :: [Opt] -> [String] -> ([String],[String])
|
|
|
|
parseAccountDescriptionArgs opts args = (as, ds')
|
|
|
|
where (as, ds) = break (==patseparator) args
|
|
|
|
ds' = dropWhile (==patseparator) ds
|
|
|
|
patseparator = replicate 2 negchar
|
2008-11-26 08:21:44 +03:00
|
|
|
negchar
|
|
|
|
| OptionsAnywhere `elem` opts = '^'
|
|
|
|
| otherwise = '-'
|
|
|
|
|
2008-10-08 21:00:22 +04:00
|
|
|
testoptions order cmdline = putStr $
|
|
|
|
case getOpt order options cmdline of
|
|
|
|
(o,n,[] ) -> "options=" ++ show o ++ " args=" ++ show n
|
2008-11-26 07:04:05 +03:00
|
|
|
(o,_,errs) -> concat errs ++ usage
|
2008-10-08 21:00:22 +04:00
|
|
|
|