update example scripts for api, clarify -f support
This commit is contained in:
		
							parent
							
								
									a95bc44345
								
							
						
					
					
						commit
						415cd19a7e
					
				| @ -2,8 +2,15 @@ | |||||||
| -- Show all account names used in the default journal. | -- Show all account names used in the default journal. | ||||||
| 
 | 
 | ||||||
| import Hledger | import Hledger | ||||||
|  | import Hledger.Cli (getCliOpts, mainmode, withJournalDo) | ||||||
| 
 | 
 | ||||||
| main = do | main = do | ||||||
|   j <- defaultJournal |   -- simple way to read ~/.hledger.journal or $LEDGER_FILE | ||||||
|   let l = ledgerFromJournal Any j |   -- j <- defaultJournal | ||||||
|   mapM_ putStrLn $ ledgerAccountNames l | 
 | ||||||
|  |   -- 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 | ||||||
|  | |||||||
| @ -26,7 +26,7 @@ main = getCliOpts argsmode >>= printBalanceCsv | |||||||
| printBalanceCsv opts = withJournalDo opts $ | printBalanceCsv opts = withJournalDo opts $ | ||||||
|   \CliOpts{reportopts_=ropts} j -> do |   \CliOpts{reportopts_=ropts} j -> do | ||||||
|     d <- getCurrentDay |     d <- getCurrentDay | ||||||
|     let (items,_) = accountsReport ropts (queryFromOpts d ropts) j |     let (items,_) = balanceReport ropts (queryFromOpts d ropts) j | ||||||
|     putStrLn $ printCSV $ |     putStrLn $ printCSV $ | ||||||
|       ["account","balance"] : |       ["account","balance"] : | ||||||
|       [[a, showMixedAmountWithoutPrice b] | (a, _, _, b) <- items] |       [[a, showMixedAmountWithoutPrice b] | (a, _, _, b) <- items] | ||||||
|  | |||||||
| @ -17,12 +17,13 @@ import Hledger.Cli | |||||||
| import System.Environment | import System.Environment | ||||||
| 
 | 
 | ||||||
| main = do | main = do | ||||||
|  |   putStrLn "(-f option not supported, see hledger-accountnames.hs for how to add it)" | ||||||
|   j <- defaultJournal |   j <- defaultJournal | ||||||
|   d <- getCurrentDay |   d <- getCurrentDay | ||||||
|   args <- getArgs |   args <- getArgs | ||||||
|   let query = Or $ map Acct args |   let query = Or $ map Acct args | ||||||
|       ropts = defreportopts{flat_=True} |       ropts = defreportopts{flat_=True} | ||||||
|       (acctbals,_) = accountsReport ropts query j |       (acctbals,_) = balanceReport ropts query j | ||||||
|       balancingamt = negate $ sum $ map (\(_,_,_,b) -> b) acctbals |       balancingamt = negate $ sum $ map (\(_,_,_,b) -> b) acctbals | ||||||
|       ps = [posting{paccount=a, pamount=b} | (a,_,_,b) <- acctbals] |       ps = [posting{paccount=a, pamount=b} | (a,_,_,b) <- acctbals] | ||||||
|            ++ [posting{paccount="equity:opening balances", pamount=balancingamt}] |            ++ [posting{paccount="equity:opening balances", pamount=balancingamt}] | ||||||
|  | |||||||
| @ -13,6 +13,7 @@ import Hledger | |||||||
| import Hledger.Cli | import Hledger.Cli | ||||||
| 
 | 
 | ||||||
| main = do | main = do | ||||||
|  |   putStrLn "(-f option not supported)" | ||||||
|   opts <- getCliOpts (defCommandMode ["hledger-print-unique"]) |   opts <- getCliOpts (defCommandMode ["hledger-print-unique"]) | ||||||
|   withJournalDo opts $ |   withJournalDo opts $ | ||||||
|     \opts j@Journal{jtxns=ts} -> print' opts j{jtxns=uniquify ts} |     \opts j@Journal{jtxns=ts} -> print' opts j{jtxns=uniquify ts} | ||||||
|  | |||||||
		Loading…
	
		Reference in New Issue
	
	Block a user