accounts: make accounts a built-in command, listing posted account names

This commit is contained in:
Simon Michael 2014-04-07 08:23:46 -07:00
parent 7d99ae5033
commit f9c1d94b5f
4 changed files with 49 additions and 15 deletions

View File

@ -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

View File

@ -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 []

View File

@ -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

View File

@ -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