hledger/Options.hs

127 lines
4.8 KiB
Haskell
Raw Normal View History

module Options
2007-02-10 20:36:50 +03:00
where
import System
2007-01-30 12:07:12 +03:00
import System.Console.GetOpt
import System.Directory
import Text.Printf
2008-11-22 08:48:56 +03:00
import Ledger.AccountName (negativepatternchar)
2007-02-16 14:51:30 +03:00
2008-10-15 21:34:55 +04:00
usagehdr = "Usage: hledger [OPTS] balance|print|register [ACCTPATS] [-- DESCPATS]\n\nOptions"++warning++":"
warning = if negativepatternchar=='-' then " (must appear before command)" else " (can appear anywhere)"
usageftr = "\n" ++
"Commands (may be abbreviated):\n" ++
"balance - show account balances\n" ++
"print - show parsed and reformatted ledger entries\n" ++
"register - show register transactions\n" ++
"\n" ++
"Account and description patterns are regular expressions, optionally prefixed\n" ++
"with " ++ [negativepatternchar] ++ " to make them negative.\n"
defaultfile = "~/.ledger"
fileenvvar = "LEDGER"
optionorder = if negativepatternchar=='-' then RequireOrder else Permute
2008-10-08 21:24:59 +04:00
-- | Command-line options we accept.
options :: [OptDescr Opt]
options = [
Option ['f'] ["file"] (ReqArg File "FILE") "ledger file; - means use standard input",
2008-10-15 21:34:55 +04:00
Option ['b'] ["begin"] (ReqArg Begin "YYYY/MM/DD") "report on entries on or after this date",
Option ['e'] ["end"] (ReqArg End "YYYY/MM/DD") "report on entries prior to this date",
2008-10-16 13:50:16 +04:00
Option ['C'] ["cleared"] (NoArg Cleared) "report only on cleared entries",
Option ['R'] ["real"] (NoArg Real) "report only on real (non-virtual) transactions",
2008-11-22 12:40:22 +03:00
Option ['s'] ["subtotal"] (NoArg SubTotal) "balance report: show subaccounts",
2008-10-10 05:36:21 +04:00
Option ['h'] ["help","usage"] (NoArg Help) "show this help",
Option ['V'] ["version"] (NoArg Version) "show version"
2008-10-08 21:24:59 +04:00
]
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 =
File String |
Begin String |
End String |
Cleared |
2008-10-16 13:50:16 +04:00
Real |
SubTotal |
2007-05-01 09:55:35 +04:00
Help |
Version
deriving (Show,Eq)
2007-02-16 15:24:13 +03:00
2008-10-15 21:34:55 +04:00
usage = usageInfo usagehdr options ++ usageftr
versionno = "0.2"
version = printf "hledger version %s \n" versionno :: String
2008-10-10 05:36:21 +04:00
-- | Parse the command-line arguments into ledger options, ledger command
-- name, and ledger command arguments
2008-10-08 21:24:59 +04:00
parseArguments :: IO ([Opt], String, [String])
parseArguments = do
args <- getArgs
case (getOpt optionorder options args) of
(opts,cmd:args,[]) -> return (opts, cmd, args)
2008-10-16 01:47:56 +04:00
(opts,[],[]) -> return (opts, [], [])
(_,_,errs) -> ioError (userError (concat errs ++ usage))
-- | 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
ledgerFilePathFromOpts opts = do
envordefault <- getEnv fileenvvar `catch` \_ -> return defaultfile
paths <- mapM tildeExpand $ [envordefault] ++ (concatMap getfile opts)
return $ last paths
where
getfile (File s) = [s]
getfile _ = []
-- | 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)
tildeExpand xs = return xs
-- | get the value of the begin date option, or a default
2008-10-08 21:24:59 +04:00
beginDateFromOpts :: [Opt] -> 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
2008-10-08 21:24:59 +04:00
endDateFromOpts :: [Opt] -> String
endDateFromOpts opts =
case endopts of
(x:_) -> last endopts
_ -> defaultdate
where
endopts = concatMap getenddate opts
getenddate (End s) = [s]
getenddate _ = []
defaultdate = ""
-- | Gather any ledger-style account/description pattern arguments into
-- two lists. These are 0 or more account patterns optionally followed by
2008-10-08 23:36:22 +04:00
-- -- and 0 or more description patterns.
parseAccountDescriptionArgs :: [String] -> ([String],[String])
parseAccountDescriptionArgs args = (as, ds')
where (as, ds) = break (=="--") args
ds' = dropWhile (=="--") ds
-- 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