diff --git a/hledger/Hledger/Cli/Commands/Tags.hs b/hledger/Hledger/Cli/Commands/Tags.hs index dae289c70..665c93933 100755 --- a/hledger/Hledger/Cli/Commands/Tags.hs +++ b/hledger/Hledger/Cli/Commands/Tags.hs @@ -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 diff --git a/hledger/doc/commands.m4.md b/hledger/doc/commands.m4.md index ac7cba9b4..f040dfa18 100644 --- a/hledger/doc/commands.m4.md +++ b/hledger/doc/commands.m4.md @@ -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.