From f9c1d94b5f5b89ed9202f1a4a79452ed6fcde7a8 Mon Sep 17 00:00:00 2001 From: Simon Michael Date: Mon, 7 Apr 2014 08:23:46 -0700 Subject: [PATCH] accounts: make accounts a built-in command, listing posted account names --- extra/hledger-accounts.hs | 15 ----------- hledger/Hledger/Cli/Accounts.hs | 44 +++++++++++++++++++++++++++++++++ hledger/Hledger/Cli/Main.hs | 3 +++ hledger/hledger.cabal | 2 ++ 4 files changed, 49 insertions(+), 15 deletions(-) delete mode 100755 extra/hledger-accounts.hs create mode 100644 hledger/Hledger/Cli/Accounts.hs diff --git a/extra/hledger-accounts.hs b/extra/hledger-accounts.hs deleted file mode 100755 index 2b2c56b44..000000000 --- a/extra/hledger-accounts.hs +++ /dev/null @@ -1,15 +0,0 @@ -#!/usr/bin/env runhaskell --- Show all account names used in the default journal. - -import Hledger.Cli - -main = do - -- simple way to read ~/.hledger.journal or $LEDGER_FILE - -- j <- defaultJournal - - -- but we'd better handle -f as well: - opts <- getCliOpts $ mainmode [] - withJournalDo opts $ \_opts j -> do - - -- query the journal for all account names and print each one - mapM_ putStrLn $ journalAccountNames j diff --git a/hledger/Hledger/Cli/Accounts.hs b/hledger/Hledger/Cli/Accounts.hs new file mode 100644 index 000000000..56ee973d8 --- /dev/null +++ b/hledger/Hledger/Cli/Accounts.hs @@ -0,0 +1,44 @@ +{-| + +The @accounts@ command lists the full names of the (query-restricted) +accounts posted to . This is similar to ledger accounts -E. + +-} + +module Hledger.Cli.Accounts ( + accountsmode + ,accounts + ,tests_Hledger_Cli_Accounts +) where + +import Data.List +import System.Console.CmdArgs.Explicit as C +import Test.HUnit + +import Hledger +import Prelude hiding (putStrLn) +import Hledger.Utils.UTF8IOCompat (putStrLn) +import Hledger.Cli.Options + + +-- | Command line options for this command. +accountsmode = (defCommandMode $ ["accounts"] ++ aliases ++ hiddenaliases) { + modeHelp = "show names of accounts posted to" `withAliases` aliases + ,modeGroupFlags = C.Group { + groupUnnamed = [ + flagReq ["drop"] (\s opts -> Right $ setopt "drop" s opts) "N" "with --flat, omit this many leading account name components" + ] + ,groupHidden = [] + ,groupNamed = [generalflagsgroup1] + } + } + where (aliases, hiddenaliases) = (["a"],["acc"]) + +-- | The accounts command. +accounts :: CliOpts -> Journal -> IO () +accounts CliOpts{reportopts_=ropts} j = do + d <- getCurrentDay + let q = queryFromOpts d ropts + mapM_ putStrLn $ filter (q `matchesAccount`) $ nub $ sort $ map paccount $ journalPostings j + +tests_Hledger_Cli_Accounts = TestList [] diff --git a/hledger/Hledger/Cli/Main.hs b/hledger/Hledger/Cli/Main.hs index 1750e4cba..1b748bdae 100644 --- a/hledger/Hledger/Cli/Main.hs +++ b/hledger/Hledger/Cli/Main.hs @@ -50,6 +50,7 @@ import Text.Printf import Hledger (ensureJournalFileExists) import Hledger.Cli.Add +import Hledger.Cli.Accounts import Hledger.Cli.Balance import Hledger.Cli.Balancesheet import Hledger.Cli.Cashflow @@ -82,6 +83,7 @@ mainmode addons = defMode { ]) ,("\nReporting commands", [ printmode + ,accountsmode ,balancemode ,registermode ,incomestatementmode @@ -239,6 +241,7 @@ main = do -- internal commands | cmd == "activity" = withJournalDo opts histogram `orShowHelp` activitymode | cmd == "add" = (journalFilePathFromOpts opts >>= ensureJournalFileExists >> withJournalDo opts add) `orShowHelp` addmode + | cmd == "accounts" = withJournalDo opts accounts `orShowHelp` accountsmode | cmd == "balance" = withJournalDo opts balance `orShowHelp` balancemode | cmd == "balancesheet" = withJournalDo opts balancesheet `orShowHelp` balancesheetmode | cmd == "cashflow" = withJournalDo opts cashflow `orShowHelp` cashflowmode diff --git a/hledger/hledger.cabal b/hledger/hledger.cabal index c2f288d9f..f19822976 100644 --- a/hledger/hledger.cabal +++ b/hledger/hledger.cabal @@ -53,6 +53,7 @@ library Hledger.Cli.Utils Hledger.Cli.Version Hledger.Cli.Add + Hledger.Cli.Accounts Hledger.Cli.Balance Hledger.Cli.Balancesheet Hledger.Cli.Cashflow @@ -108,6 +109,7 @@ executable hledger Hledger.Cli.Utils Hledger.Cli.Version Hledger.Cli.Add + Hledger.Cli.Accounts Hledger.Cli.Balance Hledger.Cli.Balancesheet Hledger.Cli.Cashflow