diff --git a/hledger/Hledger/Cli/Commands/Tags.hs b/hledger/Hledger/Cli/Commands/Tags.hs index 7ae366947..87c6d7f05 100755 --- a/hledger/Hledger/Cli/Commands/Tags.hs +++ b/hledger/Hledger/Cli/Commands/Tags.hs @@ -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 diff --git a/hledger/Hledger/Cli/Commands/Tags.md b/hledger/Hledger/Cli/Commands/Tags.md index c8a408b0d..0eda38f71 100644 --- a/hledger/Hledger/Cli/Commands/Tags.md +++ b/hledger/Hledger/Cli/Commands/Tags.md @@ -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 diff --git a/tests/tags.test b/tests/tags.test new file mode 100644 index 000000000..1c53feabe --- /dev/null +++ b/tests/tags.test @@ -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