2008-10-08 21:00:22 +04:00
|
|
|
module Options (
|
|
|
|
Flag(..),
|
|
|
|
usage,
|
|
|
|
parseArguments,
|
|
|
|
ledgerFilePathFromOpts,
|
|
|
|
beginDateFromOpts,
|
|
|
|
endDateFromOpts,
|
|
|
|
parsePatternArgs,
|
|
|
|
regexFor,
|
|
|
|
nullpats,
|
|
|
|
wildcard,
|
|
|
|
)
|
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
|
2007-02-09 04:23:12 +03:00
|
|
|
import System.Environment (getEnv)
|
2007-02-16 14:51:30 +03:00
|
|
|
import Data.Maybe (fromMaybe)
|
2007-01-30 12:07:12 +03:00
|
|
|
|
2008-10-03 04:05:16 +04:00
|
|
|
import Ledger.Utils
|
2008-10-03 04:12:59 +04:00
|
|
|
import Ledger.Types
|
2008-10-03 04:40:06 +04:00
|
|
|
import Ledger.Parse (parseLedgerFile, parseError)
|
|
|
|
import Ledger.Ledger (cacheLedger)
|
2007-02-13 06:48:16 +03:00
|
|
|
|
2007-02-16 14:51:30 +03:00
|
|
|
|
2008-10-08 21:00:22 +04:00
|
|
|
usagehdr = "Usage: hledger [OPTIONS] "++commands++" [ACCTPATTERNS] [-- DESCPATTERNS]\nOptions:"
|
|
|
|
commands = "register|balance|print"
|
|
|
|
defaultcmd = "register"
|
|
|
|
defaultfile = "~/.ledger"
|
|
|
|
fileenvvar = "LEDGER"
|
2007-02-16 12:00:17 +03:00
|
|
|
|
2008-10-08 21:00:22 +04:00
|
|
|
usage = usageInfo usagehdr options
|
2007-02-09 04:23:12 +03:00
|
|
|
|
2007-03-12 10:40:33 +03:00
|
|
|
data Flag =
|
|
|
|
File String |
|
2008-10-08 21:00:22 +04:00
|
|
|
Begin String |
|
|
|
|
End String |
|
2007-03-12 10:40:33 +03:00
|
|
|
ShowSubs |
|
2007-05-01 09:55:35 +04:00
|
|
|
Help |
|
2007-03-12 10:40:33 +03:00
|
|
|
Version
|
|
|
|
deriving (Show,Eq)
|
2007-02-16 15:24:13 +03:00
|
|
|
|
2008-10-08 21:00:22 +04:00
|
|
|
options :: [OptDescr Flag]
|
|
|
|
options = [
|
|
|
|
Option ['f'] ["file"] (ReqArg File "FILE") "ledger file; - means use standard input",
|
|
|
|
Option ['b'] [] (ReqArg Begin "BEGINDATE") "begin reports from this date (inclusive)",
|
|
|
|
Option ['e'] [] (ReqArg End "ENDDATE") "end reports on this date (exclusive)",
|
|
|
|
Option ['s'] ["showsubs"] (NoArg ShowSubs) "balance report: show subaccounts",
|
|
|
|
Option ['h'] ["help"] (NoArg Help) "show this help"
|
|
|
|
--Option ['V'] ["version"] (NoArg Version) "show version"
|
|
|
|
]
|
2007-02-18 21:12:02 +03:00
|
|
|
|
2008-10-08 21:00:22 +04:00
|
|
|
-- | Parse the command-line arguments into ledger options, ledger command
|
|
|
|
-- name, and ledger command arguments
|
|
|
|
parseArguments :: IO ([Flag], String, [String])
|
|
|
|
parseArguments = do
|
|
|
|
args <- getArgs
|
|
|
|
case (getOpt RequireOrder options args) of
|
|
|
|
(opts,[],[]) -> return (opts, defaultcmd, [])
|
|
|
|
(opts,cmd:args,[]) -> return (opts, cmd, args)
|
|
|
|
(_,_,errs) -> ioError (userError (concat errs ++ usage))
|
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
|
|
|
|
ledgerFilePathFromOpts :: [Flag] -> IO String
|
|
|
|
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
|
|
|
|
|
|
|
-- | get the value of the begin date option, or a default
|
|
|
|
beginDateFromOpts :: [Flag] -> String
|
|
|
|
beginDateFromOpts opts =
|
|
|
|
case beginopts of
|
|
|
|
(x:_) -> last beginopts
|
|
|
|
_ -> defaultdate
|
|
|
|
where
|
|
|
|
beginopts = concatMap getbegindate opts
|
|
|
|
getbegindate (Begin s) = [s]
|
|
|
|
getbegindate _ = []
|
|
|
|
defaultdate = ""
|
|
|
|
|
|
|
|
-- | get the value of the end date option, or a default
|
|
|
|
endDateFromOpts :: [Flag] -> String
|
|
|
|
endDateFromOpts opts =
|
|
|
|
case endopts of
|
|
|
|
(x:_) -> last endopts
|
|
|
|
_ -> defaultdate
|
|
|
|
where
|
|
|
|
endopts = concatMap getenddate opts
|
|
|
|
getenddate (End s) = [s]
|
|
|
|
getenddate _ = []
|
|
|
|
defaultdate = ""
|
2007-03-12 10:40:33 +03:00
|
|
|
|
2008-10-01 15:08:30 +04:00
|
|
|
-- | ledger pattern arguments are: 0 or more account patterns
|
2008-10-01 15:44:47 +04:00
|
|
|
-- optionally followed by -- and 0 or more description patterns.
|
2008-10-03 15:52:07 +04:00
|
|
|
-- No arguments implies match all. Here we gather these into two lists.
|
|
|
|
-- parsePatternArgs :: [String] -> (Regex,Regex)
|
|
|
|
parsePatternArgs :: [String] -> ([String],[String])
|
|
|
|
parsePatternArgs args = (as, ds')
|
2007-07-11 10:58:47 +04:00
|
|
|
where (as, ds) = break (=="--") args
|
|
|
|
ds' = dropWhile (=="--") ds
|
2007-07-11 09:46:20 +04:00
|
|
|
|
2008-10-01 15:08:30 +04:00
|
|
|
-- | convert a list of strings to a regular expression matching any of them,
|
2008-10-01 15:44:47 +04:00
|
|
|
-- or a wildcard if there are none.
|
2008-10-01 15:08:30 +04:00
|
|
|
regexFor :: [String] -> Regex
|
|
|
|
regexFor [] = wildcard
|
2008-10-03 15:52:07 +04:00
|
|
|
regexFor ss = mkRegex $ concat $ ["("] ++ (intersperse "|" ss) ++ [")"]
|
2007-07-11 09:46:20 +04:00
|
|
|
|
2008-10-01 15:08:30 +04:00
|
|
|
wildcard :: Regex
|
2008-10-03 04:40:06 +04:00
|
|
|
wildcard = mkRegex ".*"
|
|
|
|
|
2008-10-03 07:38:33 +04:00
|
|
|
nullpats = (wildcard,wildcard)
|
2008-10-08 21:00:22 +04:00
|
|
|
|
|
|
|
-- testoptions RequireOrder ["foo","-v"]
|
|
|
|
-- testoptions Permute ["foo","-v"]
|
|
|
|
-- testoptions (ReturnInOrder Arg) ["foo","-v"]
|
|
|
|
-- testoptions Permute ["foo","--","-v"]
|
|
|
|
-- testoptions Permute ["-?o","--name","bar","--na=baz"]
|
|
|
|
-- testoptions Permute ["--ver","foo"]
|
|
|
|
testoptions order cmdline = putStr $
|
|
|
|
case getOpt order options cmdline of
|
|
|
|
(o,n,[] ) -> "options=" ++ show o ++ " args=" ++ show n
|
|
|
|
(_,_,errs) -> concat errs ++ usage
|
|
|
|
|