hledger/Options.hs

46 lines
1.3 KiB
Haskell
Raw Normal View History

2007-02-10 20:36:50 +03:00
module Options
where
2007-01-30 12:07:12 +03:00
import System.Console.GetOpt
import Data.Maybe ( fromMaybe )
2007-02-09 04:23:12 +03:00
import System.Environment (getEnv)
2007-01-30 12:07:12 +03:00
import Utils
2007-01-30 12:07:12 +03:00
data Flag = File String | Version deriving Show
options :: [OptDescr Flag]
options = [
Option ['f'] ["file"] (OptArg inp "FILE") "ledger file, or - to read stdin"
, Option ['v'] ["version"] (NoArg Version) "show version number"
2007-01-30 12:07:12 +03:00
]
inp :: Maybe String -> Flag
inp = File . fromMaybe "stdin"
getOptions :: [String] -> IO ([Flag], [String])
getOptions argv =
case getOpt RequireOrder options argv of
2007-01-30 12:07:12 +03:00
(o,n,[] ) -> return (o,n)
(_,_,errs) -> ioError (userError (concat errs ++ usageInfo header options))
where header = "Usage: hledger [OPTIONS]"
get_content :: Flag -> Maybe String
get_content (File s) = Just s
2007-02-09 04:23:12 +03:00
2007-02-13 07:37:44 +03:00
defaultLedgerFile = "~/ledger.dat"
2007-02-09 04:23:12 +03:00
getLedgerFilePath :: IO String
getLedgerFilePath = do
2007-02-13 07:37:44 +03:00
defaultpath <- tildeExpand defaultLedgerFile
getEnv "LEDGER" `catch` \_ -> return defaultpath >>= return
-- ledger pattern args are a list of account patterns optionally followed
-- by -- and a list of description patterns
ledgerPatternArgs :: [String] -> ([String],[String])
ledgerPatternArgs args =
case "--" `elem` args of
True -> ((takeWhile (/= "--") args), tail $ (dropWhile (/= "--") args))
False -> (args,[])