tags: add --values flag

This commit is contained in:
Simon Michael 2019-07-26 23:02:34 +01:00
parent 2aa5bd1653
commit 0b793bca1b
3 changed files with 42 additions and 12 deletions

View File

@ -9,13 +9,16 @@ where
import Data.List
import qualified Data.Text as T
import qualified Data.Text.IO as T
import Safe
import System.Console.CmdArgs.Explicit as C
import Hledger
import Hledger.Cli.CliOptions
tagsmode = hledgerCommandMode
$(embedFileRelative "Hledger/Cli/Commands/Tags.txt")
[] -- [flagNone ["strict"] (setboolopt "strict") "makes date comparing strict"] --
[flagNone ["values"] (setboolopt "values") "list tag values instead of tag names"
]
[generalflagsgroup1]
hiddenflags
([], Just $ argsFlag "[TAGREGEX [QUERY...]]")
@ -24,12 +27,15 @@ tags CliOpts{rawopts_=rawopts,reportopts_=ropts} j = do
d <- getCurrentDay
let
args = listofstringopt "args" rawopts
mtagpats = headMay args
mtagpat = headMay args
queryargs = drop 1 args
values = boolopt "values" rawopts
q = queryFromOpts d $ ropts{query_ = unwords queryargs}
txns = filter (q `matchesTransaction`) $ jtxns $ journalSelectingAmountFromOpts ropts j
tags =
tagsorvalues =
nub $ sort $
(maybe id (filter . regexMatchesCI) mtagpats) $
map (T.unpack . fst) $ concatMap transactionAllTags txns
mapM_ putStrLn tags
[if values then v else t
| (t,v) <- concatMap transactionAllTags txns
, maybe True (`regexMatchesCI` T.unpack t) mtagpat
]
mapM_ T.putStrLn tagsorvalues

View File

@ -1,11 +1,7 @@
tags\
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.
With QUERY arguments, only transactions matching the query are considered.
With --values flag, the tags' unique values are listed instead.
_FLAGS_
There's no direct way to list a tag's values, but there is an indirect way:
--pivot converts a tag's values to accounts, which you can list, like this:
hledger --pivot SOMETAG accounts --used

28
tests/tags.test Normal file
View File

@ -0,0 +1,28 @@
# tags command
2000/1/1 ; ttag:foo
(a) 1 ; ptag:bar
2000/1/2 ; ttag2:foo
(a) 1 ; ptag:qux
# 1. list all tags
$ hledger -f - tags
ptag
ttag
ttag2
# 2. list tag names matching a regex
$ hledger -f - tags ttag
ttag
ttag2
# 3. list tag values
$ hledger -f - tags --values
bar
foo
qux
# 4. list values of tags matching a regex from transactions matching a query
$ hledger -f - tags --values ptag date:2000/1/1
bar