2007-03-12 10:40:33 +03:00
|
|
|
module Options
|
2007-02-10 20:36:50 +03:00
|
|
|
where
|
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
|
|
|
|
2007-02-13 06:48:16 +03:00
|
|
|
import Utils
|
2007-07-11 10:58:47 +04:00
|
|
|
import Types
|
2007-02-13 06:48:16 +03:00
|
|
|
|
2007-02-16 14:51:30 +03:00
|
|
|
|
2007-03-12 10:58:44 +03:00
|
|
|
usagehdr = "Usage: hledger [OPTIONS] "++commands++" [ACCTPATTERNS] [-- DESCPATTERNS]\nOptions:"
|
2007-07-04 13:28:07 +04:00
|
|
|
commands = "register|balance|print"
|
2007-03-12 10:40:33 +03:00
|
|
|
defaultcmd = "register"
|
2007-02-16 12:00:17 +03:00
|
|
|
|
2007-02-16 15:24:13 +03:00
|
|
|
options :: [OptDescr Flag]
|
|
|
|
options = [
|
2007-03-12 10:40:33 +03:00
|
|
|
Option ['f'] ["file"] (ReqArg File "FILE") "ledger file; - means use standard input",
|
2007-05-01 09:55:35 +04:00
|
|
|
Option ['s'] ["showsubs"] (NoArg ShowSubs) "balance report: show subaccounts", -- register: show subtotals
|
|
|
|
Option ['h'] ["help"] (NoArg Help) "show this help"
|
2007-03-12 10:40:33 +03:00
|
|
|
--Option ['V'] ["version"] (NoArg Version) "show version"
|
|
|
|
]
|
2007-02-09 04:23:12 +03:00
|
|
|
|
2007-03-12 10:40:33 +03:00
|
|
|
data Flag =
|
|
|
|
File String |
|
|
|
|
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
|
|
|
|
2007-03-12 10:40:33 +03:00
|
|
|
parseOptions :: [String] -> IO ([Flag], [String])
|
|
|
|
parseOptions argv =
|
|
|
|
case getOpt RequireOrder options argv of
|
|
|
|
(opts,[],[]) -> return (opts, [defaultcmd])
|
|
|
|
(opts,args,[]) -> return (opts, args)
|
2007-03-12 10:58:44 +03:00
|
|
|
(_,_,errs) -> ioError (userError (concat errs ++ usage))
|
2007-02-13 06:48:16 +03:00
|
|
|
|
2007-03-12 10:40:33 +03: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
|
2007-03-12 10:58:44 +03:00
|
|
|
(_,_,errs) -> concat errs ++ usage
|
2007-02-16 12:00:17 +03:00
|
|
|
|
2007-03-12 10:58:44 +03:00
|
|
|
usage = usageInfo usagehdr options
|
2007-02-18 21:12:02 +03:00
|
|
|
|
2007-07-03 21:25:16 +04:00
|
|
|
ledgerFilePath :: [Flag] -> IO String
|
|
|
|
ledgerFilePath = findFileFromOpts "~/ledger.dat" "LEDGER"
|
|
|
|
|
2007-03-12 10:40:33 +03:00
|
|
|
-- find a file path from options, an env var or a default value
|
|
|
|
findFileFromOpts :: FilePath -> String -> [Flag] -> IO String
|
|
|
|
findFileFromOpts defaultpath envvar opts = do
|
|
|
|
envordefault <- getEnv envvar `catch` \_ -> return defaultpath
|
|
|
|
paths <- mapM tildeExpand $ [envordefault] ++ (concatMap getfile opts)
|
|
|
|
return $ last paths
|
|
|
|
where
|
|
|
|
getfile (File s) = [s]
|
|
|
|
getfile _ = []
|
2007-02-18 21:12:02 +03:00
|
|
|
|
2007-03-12 10:40:33 +03:00
|
|
|
tildeExpand :: FilePath -> IO FilePath
|
|
|
|
tildeExpand ('~':[]) = getHomeDirectory
|
|
|
|
tildeExpand ('~':'/':xs) = getHomeDirectory >>= return . (++ ('/':xs))
|
|
|
|
-- -- ~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)
|
|
|
|
tildeExpand xs = return xs
|
|
|
|
-- -- courtesy of allberry_b
|
|
|
|
|
|
|
|
-- ledger pattern args are 0 or more account patterns optionally followed
|
|
|
|
-- by -- and 0 or more description patterns
|
2007-07-11 10:58:47 +04:00
|
|
|
parsePatternArgs :: [String] -> FilterPatterns
|
|
|
|
parsePatternArgs args = argpats as ds'
|
|
|
|
where (as, ds) = break (=="--") args
|
|
|
|
ds' = dropWhile (=="--") ds
|
2007-07-11 09:46:20 +04:00
|
|
|
|
2007-07-11 10:58:47 +04:00
|
|
|
argpats :: [String] -> [String] -> FilterPatterns
|
|
|
|
argpats as ds = (regexify as, regexify ds)
|
2007-07-11 09:46:20 +04:00
|
|
|
where
|
2007-07-11 10:58:47 +04:00
|
|
|
regexify :: [String] -> Maybe Regex
|
|
|
|
regexify [] = Nothing
|
|
|
|
regexify ss = Just $ mkRegex $ "(" ++ (unwords $ intersperse "|" ss) ++ ")"
|
2007-07-11 09:46:20 +04:00
|
|
|
|