From ce888a62d51e5d33329ac28d3c87709c8c20e805 Mon Sep 17 00:00:00 2001 From: Simon Michael Date: Fri, 28 Mar 2014 16:33:48 -0700 Subject: [PATCH] restore some options utilities, fix hledger-balance-csv script --- extra/hledger-balance-csv.hs | 2 +- hledger/Hledger/Cli/Options.hs | 45 +++++++++++++++++++++------------- 2 files changed, 29 insertions(+), 18 deletions(-) diff --git a/extra/hledger-balance-csv.hs b/extra/hledger-balance-csv.hs index 46f58ed0f..7446160f7 100755 --- a/extra/hledger-balance-csv.hs +++ b/extra/hledger-balance-csv.hs @@ -27,4 +27,4 @@ printBalanceCsv opts = withJournalDo opts $ let (items,_) = balanceReport ropts (queryFromOpts d ropts) j putStrLn $ printCSV $ ["account","balance"] : - [[a, showMixedAmountWithoutPrice b] | (a, _, _, b) <- items] + [[a, showMixedAmountWithoutPrice b] | ((a, _, _), b) <- items] diff --git a/hledger/Hledger/Cli/Options.hs b/hledger/Hledger/Cli/Options.hs index 5e3ed2bc2..a3c9b3bd1 100644 --- a/hledger/Hledger/Cli/Options.hs +++ b/hledger/Hledger/Cli/Options.hs @@ -25,7 +25,7 @@ module Hledger.Cli.Options ( -- * CLI options CliOpts(..), defcliopts, - -- getCliOpts, + getCliOpts, decodeRawOpts, rawOptsToCliOpts, checkCliOpts, @@ -56,7 +56,7 @@ where import qualified Control.Exception as C -- import Control.Monad (filterM) --- import Control.Monad (when) +import Control.Monad (when) import Data.List import Data.List.Split import Data.Maybe @@ -66,7 +66,7 @@ import System.Console.CmdArgs.Explicit import System.Console.CmdArgs.Text import System.Directory import System.Environment --- import System.Exit +import System.Exit (exitSuccess) import Test.HUnit import Text.ParserCombinators.Parsec as P @@ -278,20 +278,31 @@ checkCliOpts opts@CliOpts{reportopts_=ropts} = do Right _ -> return () return opts --- not used: --- -- | Parse hledger CLI options from the command line using the given --- -- cmdargs mode, and either return them or, if a help flag is present, --- -- print the mode help and exit the program. --- getCliOpts :: Mode RawOpts -> IO CliOpts --- getCliOpts mode = do --- args <- getArgs --- let rawopts = decodeRawOpts $ processValue mode args --- opts <- rawOptsToCliOpts rawopts >>= checkCliOpts --- debugArgs args opts --- -- if any (`elem` args) ["--help","-h","-?"] --- when ("help" `inRawOpts` rawopts_ opts) $ --- putStr (showModeHelp mode) >> exitSuccess --- return opts +-- Currently only used by some extras/ scripts: +-- | Parse hledger CLI options from the command line using the given +-- cmdargs mode, and either return them or, if a help flag is present, +-- print the mode help and exit the program. +getCliOpts :: Mode RawOpts -> IO CliOpts +getCliOpts mode = do + args <- getArgs + let rawopts = decodeRawOpts $ processValue mode args + opts <- rawOptsToCliOpts rawopts >>= checkCliOpts + debugArgs args opts + -- if any (`elem` args) ["--help","-h","-?"] + when ("help" `inRawOpts` rawopts_ opts) $ + putStr (showModeHelp mode) >> exitSuccess + return opts + where + -- | Print debug info about arguments and options if --debug is present. + debugArgs :: [String] -> CliOpts -> IO () + debugArgs args opts = + when ("--debug" `elem` args) $ do + progname <- getProgName + putStrLn $ "running: " ++ progname + putStrLn $ "raw args: " ++ show args + putStrLn $ "processed opts:\n" ++ show opts + d <- getCurrentDay + putStrLn $ "search query: " ++ (show $ queryFromOpts d $ reportopts_ opts) -- CliOpts accessors