From c99f37241cf7496125b201b8ba3dfcf7fe235f13 Mon Sep 17 00:00:00 2001 From: Simon Michael Date: Fri, 6 Dec 2013 13:30:36 -0800 Subject: [PATCH] allow an optional level argument with --debug --- hledger/Hledger/Cli/Add.hs | 2 +- hledger/Hledger/Cli/Main.hs | 4 ++-- hledger/Hledger/Cli/Options.hs | 9 ++++----- 3 files changed, 7 insertions(+), 8 deletions(-) diff --git a/hledger/Hledger/Cli/Add.hs b/hledger/Hledger/Cli/Add.hs index 3e52694c8..545a52004 100644 --- a/hledger/Hledger/Cli/Add.hs +++ b/hledger/Hledger/Cli/Add.hs @@ -264,7 +264,7 @@ journalAddTransaction :: Journal -> CliOpts -> Transaction -> IO Journal journalAddTransaction j@Journal{jtxns=ts} opts t = do let f = journalFilePath j appendToJournalFileOrStdout f $ showTransaction t - when (debug_ opts) $ do + when (debug_ opts > 0) $ do putStrLn $ printf "\nAdded transaction to %s:" f putStrLn =<< registerFromString (show t) return j{jtxns=ts++[t]} diff --git a/hledger/Hledger/Cli/Main.hs b/hledger/Hledger/Cli/Main.hs index 7d677bba0..f53c693b8 100644 --- a/hledger/Hledger/Cli/Main.hs +++ b/hledger/Hledger/Cli/Main.hs @@ -125,7 +125,7 @@ main = do putStrLn $ "interval from opts: " ++ (show $ intervalFromOpts $ reportopts_ opts) putStrLn $ "query from opts & args: " ++ (show $ queryFromOpts d $ reportopts_ opts) let - dbg s = if debug_ opts then trace s else id + dbg s = if debug_ opts > 0 then trace s else id runHledgerCommand -- high priority flags and situations. --help should be highest priority. | hasHelp argsbeforecmd = dbg "--help before command, showing general help" generalHelp @@ -151,7 +151,7 @@ main = do -- an external command | isExternalCommand = do let shellcmd = printf "%s-%s %s" progname cmd (unwords' argsaftercmd) - when (debug_ opts) $ do + when (debug_ opts > 0) $ do printf "external command selected: %s\n" cmd printf "external command arguments: %s\n" (show argsaftercmd) printf "running shell command: %s\n" (show shellcmd) diff --git a/hledger/Hledger/Cli/Options.hs b/hledger/Hledger/Cli/Options.hs index 58b5a501a..1b48ca1ca 100644 --- a/hledger/Hledger/Cli/Options.hs +++ b/hledger/Hledger/Cli/Options.hs @@ -106,7 +106,6 @@ import Hledger import Hledger.Data.FormatStrings as Format import Hledger.Cli.Version - -- -- 1. cmdargs mode and flag (option) definitions for the hledger CLI, -- can be reused by other packages as well. @@ -121,7 +120,7 @@ type RawOpts = [(String,String)] helpflags = [ flagNone ["help","h","?"] (setboolopt "help") "Display general help or (with --help after COMMAND) command help." -- ,flagNone ["browse-args"] (setboolopt "browse-args") "use a web UI to select options and build up a command line" - ,flagNone ["debug"] (setboolopt "debug") "Show extra debug output" + ,flagOpt "1" ["debug"] (\s opts -> Right $ setopt "debug" s opts) "N" "Show debug output (optional argument sets debug level)" ,flagNone ["version","V"] (setboolopt "version") "Print version information" ] @@ -410,14 +409,14 @@ convertmode = (defCommandMode ["convert"]) { -- completed, but before adding defaults or derived values (XXX add) -- --- cli options, used in hledger and above +-- | Command line options. Used in the @hledger@ package and above. data CliOpts = CliOpts { rawopts_ :: RawOpts ,command_ :: String ,file_ :: Maybe FilePath ,rules_file_ :: Maybe FilePath ,alias_ :: [String] - ,debug_ :: Bool + ,debug_ :: Int -- ^ debug level, set by @--debug[=N]@. See also 'Hledger.Utils.debugLevel'. ,no_new_accounts_ :: Bool -- add ,width_ :: Maybe String -- register ,reportopts_ :: ReportOpts @@ -448,7 +447,7 @@ rawOptsToCliOpts rawopts = do ,file_ = maybestringopt "file" rawopts ,rules_file_ = maybestringopt "rules-file" rawopts ,alias_ = map stripquotes $ listofstringopt "alias" rawopts - ,debug_ = boolopt "debug" rawopts + ,debug_ = intopt "debug" rawopts ,no_new_accounts_ = boolopt "no-new-accounts" rawopts -- add ,width_ = maybestringopt "width" rawopts -- register ,reportopts_ = defreportopts {