Options cleanups

This commit is contained in:
Simon Michael 2009-01-25 08:09:26 +00:00
parent aed74a9ef9
commit f807c0f095

View File

@ -12,56 +12,63 @@ import Ledger.Utils
import Ledger.Types import Ledger.Types
import Ledger.Dates import Ledger.Dates
configflags = [
configflags = [
#ifdef VTY #ifdef VTY
"vty" "vty"
#endif #endif
#ifdef ANSI #ifdef ANSI
,"ansi" ,"ansi"
#endif #endif
#ifdef HAPPS #ifdef HAPPS
,"happs" ,"happs"
#endif #endif
] ]
versionmsg = "hledger " ++ version ++ configmsg ++ "\n" configmsg = if null configflags
version = "0.3.x" then ""
configmsg = if null configflags else " with " ++ intercalate ", " configflags
then ""
else " with " ++ intercalate ", " configflags version = "0.3.x"
ledgerdefault = "~/.ledger" progname = "hledger"
ledgerenvvar = "LEDGER" versionmsg = progname ++ " " ++ version ++ configmsg ++ "\n"
timelogdefault = "~/.timelog" ledgerpath = "~/.ledger"
timelogenvvar = "TIMELOG" ledgerenvvar = "LEDGER"
timeprogname = "hours" timeprogname = "hours"
usagehdr = "Usage: hledger [OPTION] COMMAND [ACCTPATTERNS] [-- DESCPATTERNS]\n" ++ timelogpath = "~/.timelog"
"or: hours [OPTIONS] [PERIOD [COMMAND [PATTERNS]]]\n" ++ timelogenvvar = "TIMELOG"
"\n" ++
"Commands (can be abbreviated):\n" ++ usagehdr =
" balance - show account balances\n" ++ "Usage: "++progname++" [OPTION] COMMAND [ACCTPATTERNS] [-- DESCPATTERNS]\n" ++
" print - show formatted ledger entries\n" ++ "or: "++timeprogname++" [OPTIONS] [PERIOD [COMMAND [PATTERNS]]]\n" ++
" register - show register transactions\n" ++ "\n" ++
"Commands (can be abbreviated):\n" ++
" balance - show account balances\n" ++
" print - show formatted ledger entries\n" ++
" register - show register transactions\n" ++
#ifdef VTY #ifdef VTY
" ui - run a simple vty-based text ui\n" ++ " ui - run a simple vty-based text ui\n" ++
#endif #endif
#ifdef ANSI #ifdef ANSI
" ansi - run a simple ansi-based text ui\n" ++ " ansi - run a simple ansi-based text ui\n" ++
#endif #endif
#ifdef HAPPS #ifdef HAPPS
" happs - run a web server providing a minimal web ui\n" ++ " happs - run a web server providing a minimal web ui\n" ++
#endif #endif
"\n" ++ "\n" ++
"Options (before command, unless using --options-anywhere):" "Options (before command, unless using --options-anywhere):"
usageftr = "\n" ++
"All dates can be y/m/d or ledger-style smart dates like \"last month\".\n" ++
"\n" ++ usageftr =
"Account and description patterns are regular expressions which filter by\n" ++ "\n" ++
"account name and entry description. Prefix a pattern with - to negate it,\n" ++ "All dates can be y/m/d or ledger-style smart dates like \"last month\".\n" ++
"and separate account and description patterns with --.\n" ++ "\n" ++
"(With --options-anywhere, use ^ and ^^. \"hours\" implies --options-anywhere.)\n" ++ "Account and description patterns are regular expressions which filter by\n" ++
"\n" ++ "account name and entry description. Prefix a pattern with - to negate it,\n" ++
"Also: hledger [-v] test [TESTPATTERNS] to run self-tests.\n" ++ "and separate account and description patterns with --.\n" ++
"\n" "(With --options-anywhere, use ^ and ^^. \""++timeprogname++"\" implies --options-anywhere.)\n" ++
"\n" ++
"Also: "++progname++" [-v] test [TESTPATTERNS] to run self-tests.\n" ++
"\n"
usage = usageInfo usagehdr options ++ usageftr usage = usageInfo usagehdr options ++ usageftr
-- | Command-line options we accept. -- | Command-line options we accept.
@ -92,7 +99,7 @@ options = [
] ]
where where
filehelp = printf "ledger file; - means use standard input. Defaults\nto the %s environment variable or %s" filehelp = printf "ledger file; - means use standard input. Defaults\nto the %s environment variable or %s"
ledgerenvvar ledgerdefault ledgerenvvar ledgerpath
-- | An option value from a command-line flag. -- | An option value from a command-line flag.
data Opt = data Opt =
@ -132,7 +139,7 @@ optValuesForConstructors fs opts = concatMap get opts
-- command arguments. Any dates in the options are converted to full -- command arguments. Any dates in the options are converted to full
-- YYYY/MM/DD format, while we are in the IO monad and can get the current -- YYYY/MM/DD format, while we are in the IO monad and can get the current
-- time. Arguments are parsed differently if the program was invoked as -- time. Arguments are parsed differently if the program was invoked as
-- "hours". -- \"hours\".
parseArguments :: IO ([Opt], String, [String]) parseArguments :: IO ([Opt], String, [String])
parseArguments = do parseArguments = do
args <- getArgs args <- getArgs
@ -227,8 +234,8 @@ ledgerFilePathFromOpts :: [Opt] -> IO String
ledgerFilePathFromOpts opts = do ledgerFilePathFromOpts opts = do
istimequery <- usingTimeProgramName istimequery <- usingTimeProgramName
let (e,d) = if istimequery let (e,d) = if istimequery
then (timelogenvvar,timelogdefault) then (timelogenvvar,timelogpath)
else (ledgerenvvar,ledgerdefault) else (ledgerenvvar,ledgerpath)
envordefault <- getEnv e `catch` \_ -> return d envordefault <- getEnv e `catch` \_ -> return d
paths <- mapM tildeExpand $ [envordefault] ++ optValuesForConstructor File opts paths <- mapM tildeExpand $ [envordefault] ++ optValuesForConstructor File opts
return $ last paths return $ last paths