tag: first arg filters tag names, the rest filter transactions (#261)

This commit is contained in:
Simon Michael 2017-10-17 07:05:36 -07:00
parent bf234431ef
commit 24d8336728
2 changed files with 20 additions and 10 deletions

View File

@ -9,26 +9,34 @@ where
import Data.List
import Data.String.Here
import qualified Data.Text.IO as T
import qualified Data.Text as T
import Safe
import Hledger
import Hledger.Cli.CliOptions
tagsmode = hledgerCommandMode
[here| tags
List all the tag names in use.
With a query, only matched transactions' tags are shown.
List all the tag names used in the journal. With a TAGREGEX argument,
only tag names matching the regular expression (case insensitive) are shown.
With QUERY arguments, only transactions matching the query are considered.
Reads the default journal file, or another specified with -f.
FLAGS
|]
[] -- [flagNone ["strict"] (\opts -> setboolopt "strict" opts) "makes date comparing strict"] --
[generalflagsgroup1]
[]
([], Just $ argsFlag "[QUERY]")
([], Just $ argsFlag "[TAGREGEX [QUERY...]]")
tags CliOpts{rawopts_=_rawopts,reportopts_=ropts} j = do
tags CliOpts{rawopts_=rawopts,reportopts_=ropts} j = do
d <- getCurrentDay
let
q = queryFromOpts d ropts
ts = filter (q `matchesTransaction`) $ jtxns $ journalSelectingAmountFromOpts ropts j
tags = nub $ sort $ map fst $ concatMap transactionAllTags ts
mapM_ T.putStrLn tags
args = listofstringopt "args" rawopts
mtagpats = headMay args
queryargs = drop 1 args
q = queryFromOpts d $ ropts{query_ = unwords queryargs}
txns = filter (q `matchesTransaction`) $ jtxns $ journalSelectingAmountFromOpts ropts j
tags =
nub $ sort $
(maybe id (filter . regexMatchesCI) mtagpats) $
map (T.unpack . fst) $ concatMap transactionAllTags txns
mapM_ putStrLn tags

View File

@ -766,7 +766,9 @@ The stats command also supports `-o/--output-file`
for controlling [output destination](#output-destination).
## tags
List all the tag names in use.
List all the tag names used in the journal. With a TAGREGEX argument,
only tag names matching the regular expression (case insensitive) are shown.
With additional QUERY arguments, only transactions matching the query are considered.
## test
Run built-in unit tests.