From fead7c513899ed64df20591c4c1f084fcf88c4eb Mon Sep 17 00:00:00 2001 From: Simon Michael Date: Fri, 22 Sep 2017 08:50:13 -1000 Subject: [PATCH] cli, ui, web: make -NUM a shortcut for --depth NUM --- doc/lib.m4 | 4 ++-- hledger-ui/Hledger/UI/UIOptions.hs | 9 ++++++++- hledger-web/Hledger/Web/WebOptions.hs | 8 +++++++- hledger/Hledger/Cli/CliOptions.hs | 12 +++++++++++- hledger/Hledger/Cli/Main.hs | 4 ++-- hledger/doc/options.m4.md | 4 +++- 6 files changed, 33 insertions(+), 8 deletions(-) diff --git a/doc/lib.m4 b/doc/lib.m4 index ecc18153c..972c1e699 100644 --- a/doc/lib.m4 +++ b/doc/lib.m4 @@ -141,8 +141,8 @@ m4_define({{_reportingoptions_}}, {{ `-R --real` : include only non-virtual postings -`--depth=N` -: hide accounts/postings deeper than N +`-NUM --depth=NUM` +: hide/aggregate accounts or postings more than NUM levels deep `-E --empty` : show items with zero amount, normally hidden diff --git a/hledger-ui/Hledger/UI/UIOptions.hs b/hledger-ui/Hledger/UI/UIOptions.hs index 572bcf81d..9f10621e0 100644 --- a/hledger-ui/Hledger/UI/UIOptions.hs +++ b/hledger-ui/Hledger/UI/UIOptions.hs @@ -10,6 +10,7 @@ import Data.Default import Data.Functor.Compat ((<$>)) #endif import Data.List (intercalate) +import System.Environment import Hledger.Cli hiding (progname,version,prognameandversion) import Hledger.UI.Theme (themeNames) @@ -94,6 +95,12 @@ checkUIOpts opts = Just t | not $ elem t themeNames -> Left $ "invalid theme name: "++t _ -> Right () +-- XXX some refactoring seems due getHledgerUIOpts :: IO UIOpts -getHledgerUIOpts = processArgs uimode >>= return . decodeRawOpts >>= rawOptsToUIOpts +--getHledgerUIOpts = processArgs uimode >>= return . decodeRawOpts >>= rawOptsToUIOpts +getHledgerUIOpts = do + args <- getArgs + let args' = replaceNumericFlags args + let cmdargopts = either usageError id $ process uimode args' + rawOptsToUIOpts $ decodeRawOpts cmdargopts diff --git a/hledger-web/Hledger/Web/WebOptions.hs b/hledger-web/Hledger/Web/WebOptions.hs index dae399c37..9267d7bf6 100644 --- a/hledger-web/Hledger/Web/WebOptions.hs +++ b/hledger-web/Hledger/Web/WebOptions.hs @@ -7,6 +7,7 @@ import Data.Default import Data.Functor.Compat ((<$>)) #endif import Data.Maybe +import System.Environment import Hledger.Cli hiding (progname,version,prognameandversion) import Settings @@ -92,5 +93,10 @@ checkWebOpts wopts = else Right () getHledgerWebOpts :: IO WebOpts -getHledgerWebOpts = processArgs webmode >>= return . decodeRawOpts >>= rawOptsToWebOpts +--getHledgerWebOpts = processArgs webmode >>= return . decodeRawOpts >>= rawOptsToWebOpts +getHledgerWebOpts = do + args <- getArgs + let args' = replaceNumericFlags args + let cmdargopts = either usageError id $ process webmode args' + rawOptsToWebOpts $ decodeRawOpts cmdargopts diff --git a/hledger/Hledger/Cli/CliOptions.hs b/hledger/Hledger/Cli/CliOptions.hs index 0b8e4039d..616c8c1ae 100644 --- a/hledger/Hledger/Cli/CliOptions.hs +++ b/hledger/Hledger/Cli/CliOptions.hs @@ -51,6 +51,7 @@ module Hledger.Cli.CliOptions ( outputFormatFromOpts, defaultWidth, widthFromOpts, + replaceNumericFlags, -- | For register: registerWidthsFromOpts, maybeAccountNameDrop, @@ -75,6 +76,7 @@ import Prelude () import Prelude.Compat import qualified Control.Exception as C import Control.Monad (when) +import Data.Char import Data.Default #if !MIN_VERSION_base(4,8,0) import Data.Functor.Compat ((<$>)) @@ -149,7 +151,7 @@ reportflags = [ ,flagNone ["pending","P"] (setboolopt "pending") "include only pending postings/txns" ,flagNone ["cleared","C"] (setboolopt "cleared") "include only cleared postings/txns" ,flagNone ["real","R"] (setboolopt "real") "include only non-virtual postings" - ,flagReq ["depth"] (\s opts -> Right $ setopt "depth" s opts) "N" "hide accounts/postings deeper than N" + ,flagReq ["depth"] (\s opts -> Right $ setopt "depth" s opts) "NUM" "(or -NUM): hide accounts/postings deeper than this" ,flagNone ["empty","E"] (setboolopt "empty") "show items with zero amount, normally hidden" ,flagNone ["cost","B"] (setboolopt "cost") "convert amounts to their cost at transaction time (using the transaction price, if any)" ,flagNone ["value","V"] (setboolopt "value") "convert amounts to their market value on the report end date (using the most recent applicable market price, if any)" @@ -377,6 +379,14 @@ decodeRawOpts = map (\(name',val) -> (name', fromSystemString val)) defaultWidth :: Int defaultWidth = 80 +-- | Replace any numeric flags (eg -2) with their long form (--depth 2), +-- as I'm guessing cmdargs doesn't support this directly. +replaceNumericFlags :: [String] -> [String] +replaceNumericFlags = map replace + where + replace ('-':ds) | not (null ds) && all isDigit ds = "--depth="++ds + replace s = s + -- | Parse raw option string values to the desired final data types. -- Any relative smart dates will be converted to fixed dates based on -- today's date. Parsing failures will raise an error. diff --git a/hledger/Hledger/Cli/Main.hs b/hledger/Hledger/Cli/Main.hs index f33216edb..0cc59d166 100644 --- a/hledger/Hledger/Cli/Main.hs +++ b/hledger/Hledger/Cli/Main.hs @@ -108,7 +108,7 @@ main = do -- some preliminary (imperfect) argument parsing to supplement cmdargs args <- getArgs let - args' = moveFlagsAfterCommand args + args' = moveFlagsAfterCommand $ replaceNumericFlags args isFlag = ("-" `isPrefixOf`) isNonEmptyNonFlag s = not (isFlag s) && not (null s) rawcmd = headDef "" $ takeWhile isNonEmptyNonFlag args' @@ -201,7 +201,7 @@ main = do argsToCliOpts :: [String] -> [String] -> IO CliOpts argsToCliOpts args addons = do let - args' = moveFlagsAfterCommand args + args' = moveFlagsAfterCommand $ replaceNumericFlags args cmdargsopts = either usageError id $ process (mainmode addons) args' cmdargsopts' = decodeRawOpts cmdargsopts rawOptsToCliOpts cmdargsopts' diff --git a/hledger/doc/options.m4.md b/hledger/doc/options.m4.md index 7dd10cf8d..7254736f8 100644 --- a/hledger/doc/options.m4.md +++ b/hledger/doc/options.m4.md @@ -258,9 +258,11 @@ Group postings from start of wednesday to end of next tuesday (N is start date a ## Depth limiting -With the `--depth N` option, commands like [account](#account), [balance](#balance) +With the `--depth N` option (short form: `-N`), commands like [account](#account), [balance](#balance) and [register](#register) will show only the uppermost accounts in the account tree, down to level N. Use this when you want a summary with less detail. +This flag has the same effect as a `depth:` query argument +(so `-2`, `--depth=2` or `depth:2` are basically equivalent). ## Pivoting