2007-02-10 20:36:50 +03:00
|
|
|
|
2007-02-16 12:00:17 +03:00
|
|
|
module Options (module Options, usageInfo)
|
2007-02-10 20:36:50 +03:00
|
|
|
where
|
2007-01-30 12:07:12 +03:00
|
|
|
import System.Console.GetOpt
|
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-02-16 14:51:30 +03:00
|
|
|
|
2007-02-16 15:24:13 +03:00
|
|
|
usageHeader = "Usage: hledger [OPTIONS] register|balance [MATCHARGS]"
|
2007-01-30 12:07:12 +03:00
|
|
|
|
|
|
|
getOptions :: [String] -> IO ([Flag], [String])
|
|
|
|
getOptions argv =
|
2007-02-13 06:48:16 +03:00
|
|
|
case getOpt RequireOrder options argv of
|
2007-01-30 12:07:12 +03:00
|
|
|
(o,n,[] ) -> return (o,n)
|
2007-02-16 12:00:17 +03:00
|
|
|
(_,_,errs) -> ioError (userError (concat errs ++ usageInfo usageHeader options))
|
|
|
|
|
2007-02-16 15:24:13 +03:00
|
|
|
options :: [OptDescr Flag]
|
|
|
|
options = [
|
|
|
|
Option ['v'] ["version"] (NoArg Version) "show version number"
|
|
|
|
, Option ['f'] ["file"] (OptArg readFileOpt "FILE") "ledger file, or - to read stdin"
|
|
|
|
, Option ['s'] ["subtotal"] (NoArg ShowSubs) "balance: show sub-accounts" --; register: show subtotals"
|
|
|
|
]
|
2007-02-09 04:23:12 +03:00
|
|
|
|
2007-02-16 15:24:13 +03:00
|
|
|
data Flag = Version | File String | ShowSubs deriving (Show,Eq)
|
|
|
|
|
|
|
|
readFileOpt :: Maybe String -> Flag
|
|
|
|
readFileOpt = File . fromMaybe "stdin"
|
|
|
|
|
|
|
|
getFile :: Flag -> String
|
|
|
|
getFile (File s) = s
|
|
|
|
getFile _ = []
|
|
|
|
|
|
|
|
getLedgerFilePath :: [Flag] -> IO String
|
|
|
|
getLedgerFilePath opts = do
|
|
|
|
defaultpath <- tildeExpand "~/ledger.dat"
|
|
|
|
envordefault <- getEnv "LEDGER" `catch` \_ -> return defaultpath
|
|
|
|
return $ last $ [envordefault] ++ (filter (/= "") (map getFile opts))
|
2007-02-13 06:48:16 +03:00
|
|
|
|
|
|
|
-- 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,[])
|
2007-02-16 12:00:17 +03:00
|
|
|
|
2007-02-16 14:51:30 +03:00
|
|
|
getDepth :: [Flag] -> Int
|
|
|
|
getDepth opts =
|
|
|
|
maximum $ [1] ++ map depthval opts where
|
|
|
|
depthval (ShowSubs) = 9999
|
|
|
|
depthval _ = 1
|
2007-02-18 21:12:02 +03:00
|
|
|
|
|
|
|
|
|
|
|
-- example:
|
|
|
|
-- module Opts where
|
|
|
|
|
|
|
|
-- import System.Console.GetOpt
|
|
|
|
-- import Data.Maybe ( fromMaybe )
|
|
|
|
|
|
|
|
-- data Flag
|
|
|
|
-- = Verbose | Version
|
|
|
|
-- | Input String | Output String | LibDir String
|
|
|
|
-- deriving Show
|
|
|
|
|
|
|
|
-- options :: [OptDescr Flag]
|
|
|
|
-- options =
|
|
|
|
-- [ Option ['v'] ["verbose"] (NoArg Verbose) "chatty output on stderr"
|
|
|
|
-- , Option ['V','?'] ["version"] (NoArg Version) "show version number"
|
|
|
|
-- , Option ['o'] ["output"] (OptArg outp "FILE") "output FILE"
|
|
|
|
-- , Option ['c'] [] (OptArg inp "FILE") "input FILE"
|
|
|
|
-- , Option ['L'] ["libdir"] (ReqArg LibDir "DIR") "library directory"
|
|
|
|
-- ]
|
|
|
|
|
|
|
|
-- inp,outp :: Maybe String -> Flag
|
|
|
|
-- outp = Output . fromMaybe "stdout"
|
|
|
|
-- inp = Input . fromMaybe "stdin"
|
|
|
|
|
|
|
|
-- compilerOpts :: [String] -> IO ([Flag], [String])
|
|
|
|
-- compilerOpts argv =
|
|
|
|
-- case getOpt Permute options argv of
|
|
|
|
-- (o,n,[] ) -> return (o,n)
|
|
|
|
-- (_,_,errs) -> ioError (userError (concat errs ++ usageInfo header options))
|
|
|
|
-- where header = "Usage: ic [OPTION...] files..."
|