From 98ef8f9a600a497183d284914753302a1acc90ba Mon Sep 17 00:00:00 2001 From: Simon Michael Date: Sun, 20 Nov 2016 10:30:38 -0800 Subject: [PATCH] lib, cli: allow a READER: prefix on data file paths This provides a way to override the file format detection logic, useful eg for files with wrong extensions or standard input. --- hledger-lib/Hledger/Read.hs | 130 ++++--- hledger/Hledger/Cli/CliOptions.hs | 16 +- hledger/doc/hledger.1 | 130 ++++--- hledger/doc/hledger.1.info | 230 +++++++------ hledger/doc/hledger.1.txt | 546 +++++++++++++++--------------- hledger/doc/options.m4.md | 52 +-- 6 files changed, 611 insertions(+), 493 deletions(-) diff --git a/hledger-lib/Hledger/Read.hs b/hledger-lib/Hledger/Read.hs index 9e445f54c..60400d6bd 100644 --- a/hledger-lib/Hledger/Read.hs +++ b/hledger-lib/Hledger/Read.hs @@ -11,12 +11,14 @@ to import modules below this one. module Hledger.Read ( -- * Journal files + PrefixedFilePath, defaultJournal, defaultJournalPath, readJournalFiles, readJournalFile, requireJournalFileExists, ensureJournalFileExists, + splitReaderPrefix, -- * Journal parsing readJournal, @@ -33,6 +35,7 @@ module Hledger.Read ( ) where +import Control.Applicative ((<|>)) import qualified Control.Exception as C import Control.Monad.Except import Data.List @@ -61,6 +64,10 @@ import Prelude hiding (getContents, writeFile) import Hledger.Utils.UTF8IOCompat (writeFile) +journalEnvVar = "LEDGER_FILE" +journalEnvVar2 = "LEDGER" +journalDefaultFilename = ".hledger.journal" + -- The available journal readers, each one handling a particular data format. readers :: [Reader] readers = [ @@ -71,9 +78,12 @@ readers = [ ,LedgerReader.reader ] -journalEnvVar = "LEDGER_FILE" -journalEnvVar2 = "LEDGER" -journalDefaultFilename = ".hledger.journal" +readerNames :: [String] +readerNames = map rFormat readers + +-- | A file path optionally prefixed by a reader name and colon +-- (journal:, csv:, timedot:, etc.). +type PrefixedFilePath = FilePath -- | Read the default journal file specified by the environment, or raise an error. defaultJournal :: IO Journal @@ -99,34 +109,58 @@ defaultJournalPath = do home <- getHomeDirectory `C.catch` (\(_::C.IOException) -> return "") return $ home journalDefaultFilename --- | @readJournalFiles mformat mrulesfile assrt fs@ +-- | @readJournalFiles mformat mrulesfile assrt prefixedfiles@ -- --- Call readJournalFile on each specified file path, and combine the --- resulting journals into one. If there are any errors, the first is --- returned, otherwise they are combined per Journal's monoid instance --- (concatenated, basically). Parse context (eg directives & aliases) --- is not maintained across file boundaries, it resets at the start of --- each file (though the final parse state saved in the resulting --- journal is the combination of parse states from all files). -readJournalFiles :: Maybe StorageFormat -> Maybe FilePath -> Bool -> [FilePath] -> IO (Either String Journal) -readJournalFiles mformat mrulesfile assrt fs = do +-- Read a Journal from each specified file path and combine them into one. +-- Or, return the first error message. +-- +-- Combining Journals means concatenating them, basically. +-- The parse state resets at the start of each file, which means that +-- directives & aliases do not cross file boundaries. +-- (The final parse state saved in the Journal does span all files, however.) +-- +-- As with readJournalFile, +-- file paths can optionally have a READER: prefix, +-- and the @mformat@, @mrulesfile, and @assrt@ arguments are supported +-- (and these are applied to all files). +-- +readJournalFiles :: Maybe StorageFormat -> Maybe FilePath -> Bool -> [PrefixedFilePath] -> IO (Either String Journal) +readJournalFiles mformat mrulesfile assrt prefixedfiles = do (either Left (Right . mconcat) . sequence) - <$> mapM (readJournalFile mformat mrulesfile assrt) fs + <$> mapM (readJournalFile mformat mrulesfile assrt) prefixedfiles --- | @readJournalFile mformat mrulesfile assrt f@ +-- | @readJournalFile mformat mrulesfile assrt prefixedfile@ -- --- Read a Journal from this file (or stdin if the file path is -). --- Assume the specified data format, or a format identified from the file path, --- or try all readers. --- A CSV conversion rules file may be specified for better conversion of CSV. --- Also optionally check any balance assertions in the journal. --- If parsing or balance assertions fail, return an error message instead. -readJournalFile :: Maybe StorageFormat -> Maybe FilePath -> Bool -> FilePath -> IO (Either String Journal) -readJournalFile mformat mrulesfile assrt f = do +-- Read a Journal from this file, or from stdin if the file path is -, +-- or return an error message. The file path can have a READER: prefix. +-- +-- The reader (data format) is chosen based on (in priority order): +-- the @mformat@ argument; +-- the file path's READER: prefix, if any; +-- a recognised file name extension (in readJournal); +-- if none of these identify a known reader, all built-in readers are tried in turn. +-- +-- A CSV conversion rules file (@mrulesfiles@) can be specified to help convert CSV data. +-- +-- Optionally, any balance assertions in the journal can be checked (@assrt@). +-- +readJournalFile :: Maybe StorageFormat -> Maybe FilePath -> Bool -> PrefixedFilePath -> IO (Either String Journal) +readJournalFile mformat mrulesfile assrt prefixedfile = do + let + (mprefixformat, f) = splitReaderPrefix prefixedfile + mfmt = mformat <|> mprefixformat requireJournalFileExists f - readFileOrStdinAnyLineEnding f >>= readJournal mformat mrulesfile assrt (Just f) + readFileOrStdinAnyLineEnding f >>= readJournal mfmt mrulesfile assrt (Just f) --- | If the specified journal file does not exist, give a helpful error and quit. +-- | If a filepath is prefixed by one of the reader names and a colon, +-- split that off. Eg "csv:-" -> (Just "csv", "-"). +splitReaderPrefix :: PrefixedFilePath -> (Maybe String, FilePath) +splitReaderPrefix f = + headDef (Nothing, f) + [(Just r, drop (length r + 1) f) | r <- readerNames, (r++":") `isPrefixOf` f] + +-- | If the specified journal file does not exist (and is not "-"), +-- give a helpful error and quit. requireJournalFileExists :: FilePath -> IO () requireJournalFileExists "-" = return () requireJournalFileExists f = do @@ -153,7 +187,7 @@ newJournalContent = do d <- getCurrentDay return $ printf "; journal created %s by hledger\n" (show d) --- | Read a journal from the given text, trying all known formats, or simply throw an error. +-- | Read a Journal from the given text trying all readers in turn, or throw an error. readJournal' :: Text -> IO Journal readJournal' t = readJournal Nothing Nothing True Nothing t >>= either error' return @@ -163,30 +197,42 @@ tests_readJournal' = [ assertBool "" True ] --- | @readJournal mformat mrulesfile assrt mpath t@ +-- | @readJournal mformat mrulesfile assrt mfile txt@ +-- +-- Read a Journal from some text, or return an error message. +-- +-- The reader (data format) is chosen based on (in priority order): +-- the @mformat@ argument; +-- a recognised file name extension in @mfile@ (if provided). +-- If none of these identify a known reader, all built-in readers are tried in turn +-- (returning the first one's error message if none of them succeed). +-- +-- A CSV conversion rules file (@mrulesfiles@) can be specified to help convert CSV data. +-- +-- Optionally, any balance assertions in the journal can be checked (@assrt@). -- --- Try to read a Journal from some text. --- If a format is specified (mformat), try only that reader. --- Otherwise if the file path is provided (mpath), and it specifies a format, try only that reader. --- Otherwise try all readers in turn until one succeeds, or return the first error if none of them succeed. --- A CSV conversion rules file may be specified (mrulesfile) for use by the CSV reader. --- If the assrt flag is true, also check and enforce balance assertions in the journal. readJournal :: Maybe StorageFormat -> Maybe FilePath -> Bool -> Maybe FilePath -> Text -> IO (Either String Journal) -readJournal mformat mrulesfile assrt mpath t = - let rs = maybe readers (:[]) $ findReader mformat mpath - in tryReaders rs mrulesfile assrt mpath t +readJournal mformat mrulesfile assrt mfile txt = + let + rs = maybe readers (:[]) $ findReader mformat mfile + in + tryReaders rs mrulesfile assrt mfile txt -- | @findReader mformat mpath@ -- --- Find the reader for the given format (mformat), if any. --- Or if no format is provided, find the first reader that handles the --- file name's extension, if any. +-- Find the reader named by @mformat@, if provided. +-- Or, if a file path is provided, find the first reader that handles +-- its file extension, if any. findReader :: Maybe StorageFormat -> Maybe FilePath -> Maybe Reader findReader Nothing Nothing = Nothing -findReader (Just fmt) _ = headMay [r | r <- readers, fmt == rFormat r] -findReader Nothing (Just path) = headMay [r | r <- readers, ext `elem` rExtensions r] +findReader (Just fmt) _ = headMay [r | r <- readers, rFormat r == fmt] +findReader Nothing (Just path) = + case prefix of + Just fmt -> headMay [r | r <- readers, rFormat r == fmt] + Nothing -> headMay [r | r <- readers, ext `elem` rExtensions r] where - ext = drop 1 $ takeExtension path + (prefix,path') = splitReaderPrefix path + ext = drop 1 $ takeExtension path' -- | @tryReaders readers mrulesfile assrt path t@ -- diff --git a/hledger/Hledger/Cli/CliOptions.hs b/hledger/Hledger/Cli/CliOptions.hs index 70a1ebfdc..7c781a3d0 100644 --- a/hledger/Hledger/Cli/CliOptions.hs +++ b/hledger/Hledger/Cli/CliOptions.hs @@ -397,14 +397,22 @@ aliasesFromOpts = map (\a -> fromparse $ runParser accountaliasp ("--alias "++qu -- 1. options, 2. an environment variable, or 3. the default. -- Actually, returns one or more file paths. There will be more -- than one if multiple -f options were provided. +-- File paths can have a READER: prefix naming a reader/data format. journalFilePathFromOpts :: CliOpts -> IO [String] journalFilePathFromOpts opts = do f <- defaultJournalPath d <- getCurrentDirectory - mapM (expandPath d) $ ifEmpty (file_ opts) [f] - where - ifEmpty [] d = d - ifEmpty l _ = l + case file_ opts of + [] -> return [f] + fs -> mapM (expandPathPreservingPrefix d) fs + +expandPathPreservingPrefix :: FilePath -> PrefixedFilePath -> IO PrefixedFilePath +expandPathPreservingPrefix d prefixedf = do + let (p,f) = splitReaderPrefix prefixedf + f' <- expandPath d f + return $ case p of + Just p -> p ++ ":" ++ f' + Nothing -> f' -- | Get the expanded, absolute output file path from options, -- or the default (-, meaning stdout). diff --git a/hledger/doc/hledger.1 b/hledger/doc/hledger.1 index bda0df4c4..dffb19eb2 100644 --- a/hledger/doc/hledger.1 +++ b/hledger/doc/hledger.1 @@ -403,10 +403,6 @@ Eg \-p jan \-p feb is equivalent to \-p feb. .PP hledger reads transactions from a data file (and the add command writes to it). -Usually this is in hledger\[aq]s journal format, but it can also be one -of the other supported file types, such as timeclock, timedot, CSV, or a -C++ Ledger journal (partial support). -.PP By default this file is \f[C]$HOME/.hledger.journal\f[] (or on Windows, something like \f[C]C:/Users/USER/.hledger.journal\f[]). You can override this with the \f[C]$LEDGER_FILE\f[] environment @@ -423,51 +419,10 @@ or with the \f[C]\-f/\-\-file\f[] option: .IP .nf \f[C] -$\ hledger\ \-f\ some/file.ext\ stats +$\ hledger\ \-f\ /some/file\ stats \f[] .fi .PP -hledger tries to identify the file format based on the file extension, -as follows: -.PP -.TS -tab(@); -l l. -T{ -File extension: -T}@T{ -Use format: -T} -_ -T{ -\f[C]\&.journal\f[], \f[C]\&.j\f[], \f[C]\&.hledger\f[], -\f[C]\&.ledger\f[] -T}@T{ -journal -T} -T{ -\f[C]\&.timeclock\f[] -T}@T{ -timeclock -T} -T{ -\f[C]\&.timedot\f[] -T}@T{ -timedot -T} -T{ -\f[C]\&.csv\f[] -T}@T{ -CSV -T} -.TE -.PP -If the file name has some other extension, or none, hledger tries each -of these formats in turn. -(Plus one more: the experimental "ledger" format, an alternate parser -for C++ Ledger journals, which we try only as a last resort as it\[aq]s -new and hledger\[aq]s journal parser works better for now.) -.PP The file name \f[C]\-\f[] (hyphen) means standard input, as usual: .IP .nf @@ -476,6 +431,89 @@ $\ cat\ some.journal\ |\ hledger\ \-f\- \f[] .fi .PP +Usually this file is in hledger\[aq]s journal format, but it can also be +one of several other formats, shown below. +hledger tries to identify the format based on the file extension, as +follows: +.PP +.TS +tab(@); +l l l. +T{ +Format: +T}@T{ +Description: +T}@T{ +File extensions: +T} +_ +T{ +journal +T}@T{ +hledger\[aq]s journal format +T}@T{ +\f[C]\&.journal\f[], \f[C]\&.j\f[], \f[C]\&.hledger\f[], +\f[C]\&.ledger\f[] +T} +T{ +timeclock +T}@T{ +timeclock files (precise time logging) +T}@T{ +\f[C]\&.timeclock\f[] +T} +T{ +timedot +T}@T{ +timedot files (approximate time logging) +T}@T{ +\f[C]\&.timedot\f[] +T} +T{ +CSV +T}@T{ +comma\-separated values (data interchange) +T}@T{ +\f[C]\&.csv\f[] +T} +.TE +.PP +hledger identifies the format based on the file extension if possible. +If that does not identify a known format, it tries each format in turn. +.PP +If needed, eg to ensure correct error messages, you can force a specific +format by prepending it to the file path with a colon. +Examples: +.IP +.nf +\f[C] +$\ hledger\ \-f\ csv:/some/csv\-file.dat\ stats +$\ echo\ \[aq]i\ 2009/13/1\ 08:00:00\[aq]\ |\ hledger\ print\ \-ftimeclock:\- +\f[] +.fi +.PP +Some other experimental formats are available but not yet used by +default: +.PP +.TS +tab(@); +l l l. +T{ +Format: +T}@T{ +Description: +T}@T{ +File extensions: +T} +_ +T{ +ledger +T}@T{ +Ledger\[aq]s journal format (incomplete) +T}@T{ +T} +.TE +.PP You can specify multiple \f[C]\-f\f[] options, to read multiple files as one big journal. Directives in one file will not affect subsequent files in this case (if diff --git a/hledger/doc/hledger.1.info b/hledger/doc/hledger.1.info index 52d4a76e0..e5f2359d1 100644 --- a/hledger/doc/hledger.1.info +++ b/hledger/doc/hledger.1.info @@ -324,13 +324,9 @@ File: hledger.1.info, Node: Input files, Next: Depth limiting, Prev: Reportin =============== hledger reads transactions from a data file (and the add command writes -to it). Usually this is in hledger's journal format, but it can also be -one of the other supported file types, such as timeclock, timedot, CSV, -or a C++ Ledger journal (partial support). - - By default this file is `$HOME/.hledger.journal' (or on Windows, -something like `C:/Users/USER/.hledger.journal'). You can override this -with the `$LEDGER_FILE' environment variable: +to it). By default this file is `$HOME/.hledger.journal' (or on +Windows, something like `C:/Users/USER/.hledger.journal'). You can +override this with the `$LEDGER_FILE' environment variable: $ setenv LEDGER_FILE ~/finance/2016.journal @@ -339,29 +335,43 @@ $ hledger stats or with the `-f/--file' option: -$ hledger -f some/file.ext stats - - hledger tries to identify the file format based on the file -extension, as follows: - -File extension: Use format: --------------------------------------------------------- -`.journal', `.j', `.hledger', `.ledger' journal -`.timeclock' timeclock -`.timedot' timedot -`.csv' CSV - - If the file name has some other extension, or none, hledger tries -each of these formats in turn. (Plus one more: the experimental "ledger" -format, an alternate parser for C++ Ledger journals, which we try only -as a last resort as it's new and hledger's journal parser works better -for now.) +$ hledger -f /some/file stats The file name `-' (hyphen) means standard input, as usual: $ cat some.journal | hledger -f- + Usually this file is in hledger's journal format, but it can also be +one of several other formats, shown below. hledger tries to identify the +format based on the file extension, as follows: + +Format: Description: File extensions: +-------------------------------------------------------------------------------------------------- +journal hledger's journal format `.journal', `.j', `.hledger', `.ledger' +timeclock timeclock files (precise time logging) `.timeclock' +timedot timedot files (approximate time logging) `.timedot' +CSV comma-separated values (data interchange) `.csv' + + hledger identifies the format based on the file extension if +possible. If that does not identify a known format, it tries each +format in turn. + + If needed, eg to ensure correct error messages, you can force a +specific format by prepending it to the file path with a colon. +Examples: + + +$ hledger -f csv:/some/csv-file.dat stats +$ echo 'i 2009/13/1 08:00:00' | hledger print -ftimeclock:- + + Some other experimental formats are available but not yet used by +default: + +Format: Description: File extensions: +-------------------------------------------------------------------- +ledger Ledger's journal format (incomplete) + You can specify multiple `-f' options, to read multiple files as one big journal. Directives in one file will not affect subsequent files in this case (if you need that, use the include directive instead). @@ -2165,91 +2175,91 @@ Node: Reporting options7583 Ref: #reporting-options7736 Node: Input files9512 Ref: #input-files9652 -Node: Depth limiting11233 -Ref: #depth-limiting11373 -Node: Smart dates11574 -Ref: #smart-dates11713 -Node: Report intervals12710 -Ref: #report-intervals12863 -Node: Period expressions13199 -Ref: #period-expressions13364 -Node: Regular expressions15699 -Ref: #regular-expressions15841 -Node: QUERIES17324 -Ref: #queries17428 -Node: COMMANDS21067 -Ref: #commands21181 -Node: accounts21854 -Ref: #accounts21954 -Node: activity22936 -Ref: #activity23048 -Node: add23407 -Ref: #add23508 -Node: balance26167 -Ref: #balance26280 -Node: Flat mode29253 -Ref: #flat-mode29380 -Node: Depth limited balance reports29799 -Ref: #depth-limited-balance-reports30002 -Node: Multicolumn balance reports30423 -Ref: #multicolumn-balance-reports30625 -Node: Market value35274 -Ref: #market-value35438 -Node: Custom balance output35931 -Ref: #custom-balance-output36104 -Node: Output destination38208 -Ref: #output-destination38373 -Node: CSV output38643 -Ref: #csv-output38762 -Node: balancesheet39159 -Ref: #balancesheet39287 -Node: cashflow39939 -Ref: #cashflow40056 -Node: help40746 -Ref: #help40858 -Node: incomestatement41695 -Ref: #incomestatement41825 -Node: info42552 -Ref: #info42659 -Node: man43021 -Ref: #man43118 -Node: print43521 -Ref: #print43626 -Node: register44972 -Ref: #register45085 -Node: Custom register output49577 -Ref: #custom-register-output49708 -Node: stats51005 -Ref: #stats51111 -Node: test51987 -Ref: #test52074 -Node: ADD-ON COMMANDS52441 -Ref: #add-on-commands52577 -Node: api53865 -Ref: #api53957 -Node: autosync53991 -Ref: #autosync54106 -Node: diff56421 -Ref: #diff56531 -Node: equity57195 -Ref: #equity57309 -Node: interest58637 -Ref: #interest58754 -Node: irr61838 -Ref: #irr61951 -Node: print-unique64326 -Ref: #print-unique64456 -Node: rewrite64714 -Ref: #rewrite64833 -Node: ui65362 -Ref: #ui65462 -Node: web65503 -Ref: #web65591 -Node: TROUBLESHOOTING65624 -Ref: #troubleshooting65743 -Node: Run-time problems65797 -Ref: #run-time-problems65940 -Node: Known limitations67884 -Ref: #known-limitations68027 +Node: Depth limiting11677 +Ref: #depth-limiting11817 +Node: Smart dates12018 +Ref: #smart-dates12157 +Node: Report intervals13154 +Ref: #report-intervals13307 +Node: Period expressions13643 +Ref: #period-expressions13808 +Node: Regular expressions16143 +Ref: #regular-expressions16285 +Node: QUERIES17768 +Ref: #queries17872 +Node: COMMANDS21511 +Ref: #commands21625 +Node: accounts22298 +Ref: #accounts22398 +Node: activity23380 +Ref: #activity23492 +Node: add23851 +Ref: #add23952 +Node: balance26611 +Ref: #balance26724 +Node: Flat mode29697 +Ref: #flat-mode29824 +Node: Depth limited balance reports30243 +Ref: #depth-limited-balance-reports30446 +Node: Multicolumn balance reports30867 +Ref: #multicolumn-balance-reports31069 +Node: Market value35718 +Ref: #market-value35882 +Node: Custom balance output36375 +Ref: #custom-balance-output36548 +Node: Output destination38652 +Ref: #output-destination38817 +Node: CSV output39087 +Ref: #csv-output39206 +Node: balancesheet39603 +Ref: #balancesheet39731 +Node: cashflow40383 +Ref: #cashflow40500 +Node: help41190 +Ref: #help41302 +Node: incomestatement42139 +Ref: #incomestatement42269 +Node: info42996 +Ref: #info43103 +Node: man43465 +Ref: #man43562 +Node: print43965 +Ref: #print44070 +Node: register45416 +Ref: #register45529 +Node: Custom register output50021 +Ref: #custom-register-output50152 +Node: stats51449 +Ref: #stats51555 +Node: test52431 +Ref: #test52518 +Node: ADD-ON COMMANDS52885 +Ref: #add-on-commands53021 +Node: api54309 +Ref: #api54401 +Node: autosync54435 +Ref: #autosync54550 +Node: diff56865 +Ref: #diff56975 +Node: equity57639 +Ref: #equity57753 +Node: interest59081 +Ref: #interest59198 +Node: irr62282 +Ref: #irr62395 +Node: print-unique64770 +Ref: #print-unique64900 +Node: rewrite65158 +Ref: #rewrite65277 +Node: ui65806 +Ref: #ui65906 +Node: web65947 +Ref: #web66035 +Node: TROUBLESHOOTING66068 +Ref: #troubleshooting66187 +Node: Run-time problems66241 +Ref: #run-time-problems66384 +Node: Known limitations68328 +Ref: #known-limitations68471  End Tag Table diff --git a/hledger/doc/hledger.1.txt b/hledger/doc/hledger.1.txt index 8a5370a5f..c1e78d800 100644 --- a/hledger/doc/hledger.1.txt +++ b/hledger/doc/hledger.1.txt @@ -276,68 +276,82 @@ OPTIONS Input files hledger reads transactions from a data file (and the add command writes - to it). Usually this is in hledger's journal format, but it can also - be one of the other supported file types, such as timeclock, timedot, - CSV, or a C++ Ledger journal (partial support). - - By default this file is $HOME/.hledger.journal (or on Windows, some- - thing like C:/Users/USER/.hledger.journal). You can override this with - the $LEDGER_FILE environment variable: + to it). By default this file is $HOME/.hledger.journal (or on Windows, + something like C:/Users/USER/.hledger.journal). You can override this + with the $LEDGER_FILE environment variable: $ setenv LEDGER_FILE ~/finance/2016.journal $ hledger stats or with the -f/--file option: - $ hledger -f some/file.ext stats - - hledger tries to identify the file format based on the file extension, - as follows: - - - File extension: Use format: - ----------------------------------------- - .journal, .j, .hledger, journal - .ledger - .timeclock timeclock - .timedot timedot - .csv CSV - - If the file name has some other extension, or none, hledger tries each - of these formats in turn. (Plus one more: the experimental "ledger" - format, an alternate parser for C++ Ledger journals, which we try only - as a last resort as it's new and hledger's journal parser works better - for now.) + $ hledger -f /some/file stats The file name - (hyphen) means standard input, as usual: $ cat some.journal | hledger -f- - You can specify multiple -f options, to read multiple files as one big - journal. Directives in one file will not affect subsequent files in + Usually this file is in hledger's journal format, but it can also be + one of several other formats, shown below. hledger tries to identify + the format based on the file extension, as follows: + + + Format: Description: File extensions: + ------------------------------------------------------ + journal hledger's journal .journal, .j, + format .hledger, .ledger + timeclock timeclock files .timeclock + (precise time log- + ging) + timedot timedot files .timedot + (approximate time + logging) + CSV comma-separated .csv + values (data inter- + change) + + hledger identifies the format based on the file extension if possible. + If that does not identify a known format, it tries each format in turn. + + If needed, eg to ensure correct error messages, you can force a spe- + cific format by prepending it to the file path with a colon. Examples: + + $ hledger -f csv:/some/csv-file.dat stats + $ echo 'i 2009/13/1 08:00:00' | hledger print -ftimeclock:- + + Some other experimental formats are available but not yet used by + default: + + + Format: Description: File extensions: + ------------------------------------------------- + ledger Ledger's journal + format (incomplete) + + You can specify multiple -f options, to read multiple files as one big + journal. Directives in one file will not affect subsequent files in this case (if you need that, use the include directive instead). Depth limiting - With the --depth N option, commands like account, balance and register - will show only the uppermost accounts in the account tree, down to + With the --depth N option, commands like account, balance and register + will show only the uppermost accounts in the account tree, down to level N. Use this when you want a summary with less detail. Smart dates hledger's user interfaces accept a flexible "smart date" syntax (unlike - dates in the journal file). Smart dates allow some english words, can - be relative to today's date, and can have less-significant date parts + dates in the journal file). Smart dates allow some english words, can + be relative to today's date, and can have less-significant date parts omitted (defaulting to 1). Examples: - 2009/1/1, 2009/01/01, simple dates, several sep- 2009-1-1, 2009.1.1 arators allowed - 2009/1, 2009 same as above - a missing + 2009/1, 2009 same as above - a missing day or month defaults to 1 - 1/1, january, jan, relative dates, meaning - this year january 1 of the current + 1/1, january, jan, relative dates, meaning + this year january 1 of the current year next year january 1 of next year this month the 1st of the current @@ -350,23 +364,23 @@ OPTIONS Report intervals A report interval can be specified so that commands like register, bal- - ance and activity will divide their reports into multiple subperiods. - The basic intervals can be selected with one of -D/--daily, - -W/--weekly, -M/--monthly, -Q/--quarterly, or -Y/--yearly. More com- + ance and activity will divide their reports into multiple subperiods. + The basic intervals can be selected with one of -D/--daily, + -W/--weekly, -M/--monthly, -Q/--quarterly, or -Y/--yearly. More com- plex intervals may be specified with a period expression. Period expressions - The -p/--period option accepts period expressions, a shorthand way of - expressing a start date, end date, and/or report interval all at once. + The -p/--period option accepts period expressions, a shorthand way of + expressing a start date, end date, and/or report interval all at once. - Here's a basic period expression specifying the first quarter of 2009. - Note, hledger always treats start dates as inclusive and end dates as + Here's a basic period expression specifying the first quarter of 2009. + Note, hledger always treats start dates as inclusive and end dates as exclusive: -p "from 2009/1/1 to 2009/4/1" - Keywords like "from" and "to" are optional, and so are the spaces, as - long as you don't run two dates together. "to" can also be written as + Keywords like "from" and "to" are optional, and so are the spaces, as + long as you don't run two dates together. "to" can also be written as "-". These are equivalent to the above: @@ -374,7 +388,7 @@ OPTIONS -p2009/1/1to2009/4/1 -p2009/1/1-2009/4/1 - Dates are smart dates, so if the current year is 2009, the above can + Dates are smart dates, so if the current year is 2009, the above can also be written as: @@ -390,27 +404,25 @@ OPTIONS 1, 2009 -p "from 2009/1" the same -p "from 2009" the same - -p "to 2009" everything before january + -p "to 2009" everything before january 1, 2009 - A single date with no "from" or "to" defines both the start and end + A single date with no "from" or "to" defines both the start and end date like so: - - - -p "2009" the year 2009; equivalent + -p "2009" the year 2009; equivalent to "2009/1/1 to 2010/1/1" - -p "2009/1" the month of jan; equiva- + -p "2009/1" the month of jan; equiva- lent to "2009/1/1 to 2009/2/1" - -p "2009/1/1" just that day; equivalent + -p "2009/1/1" just that day; equivalent to "2009/1/1 to 2009/1/2" - The argument of -p can also begin with, or be, a report interval - expression. The basic report intervals are daily, weekly, monthly, + The argument of -p can also begin with, or be, a report interval + expression. The basic report intervals are daily, weekly, monthly, quarterly, or yearly, which have the same effect as the -D,-W,-M,-Q, or - -Y flags. Between report interval and start/end dates (if any), the + -Y flags. Between report interval and start/end dates (if any), the word in is optional. Examples: @@ -419,7 +431,7 @@ OPTIONS -p "quarterly" The following more complex report intervals are also supported: - biweekly, bimonthly, every N days|weeks|months|quarters|years, + biweekly, bimonthly, every N days|weeks|months|quarters|years, every Nth day [of month], every Nth day of week. Examples: @@ -429,12 +441,12 @@ OPTIONS -p "every 2 weeks" -p "every 5 days from 1/3" - Show historical balances at end of 15th each month (N is exclusive end + Show historical balances at end of 15th each month (N is exclusive end date): hledger balance -H -p "every 16th day" - Group postings from start of wednesday to end of next tuesday (N is + Group postings from start of wednesday to end of next tuesday (N is start date and exclusive end date): hledger register checking -p "every 3rd day of week" @@ -442,56 +454,56 @@ OPTIONS Regular expressions hledger uses regular expressions in a number of places: - o query terms, on the command line and in the hledger-web search form: + o query terms, on the command line and in the hledger-web search form: REGEX, desc:REGEX, cur:REGEX, tag:...=REGEX o CSV rules conditional blocks: if REGEX ... - o account alias directives and options: alias /REGEX/ = REPLACEMENT, + o account alias directives and options: alias /REGEX/ = REPLACEMENT, --alias /REGEX/=REPLACEMENT - hledger's regular expressions come from the regex-tdfa library. In + hledger's regular expressions come from the regex-tdfa library. In general they: o are case insensitive - o are infix matching (do not need to match the entire thing being + o are infix matching (do not need to match the entire thing being matched) o are POSIX extended regular expressions o also support GNU word boundaries (\<, \>, \b, \B) - o and parenthesised capturing groups and numeric backreferences in + o and parenthesised capturing groups and numeric backreferences in replacement strings o do not support mode modifiers like (?s) Some things to note: - o In the alias directive and --alias option, regular expressions must - be enclosed in forward slashes (/REGEX/). Elsewhere in hledger, + o In the alias directive and --alias option, regular expressions must + be enclosed in forward slashes (/REGEX/). Elsewhere in hledger, these are not required. o To match a regular expression metacharacter like $ as a literal char- acter, prepend a backslash. Eg to search for amounts with the dollar sign in hledger-web, write cur:\$. - o On the command line, some metacharacters like $ have a special mean- + o On the command line, some metacharacters like $ have a special mean- ing to the shell and so must be escaped a second time, with single or - double quotes or another backslash. Eg, to match amounts with the + double quotes or another backslash. Eg, to match amounts with the dollar sign from the command line, write cur:'\$' or cur:\\$. QUERIES - One of hledger's strengths is being able to quickly report on precise - subsets of your data. Most commands accept an optional query expres- - sion, written as arguments after the command name, to filter the data - by date, account name or other criteria. The syntax is similar to a + One of hledger's strengths is being able to quickly report on precise + subsets of your data. Most commands accept an optional query expres- + sion, written as arguments after the command name, to filter the data + by date, account name or other criteria. The syntax is similar to a web search: one or more space-separated search terms, quotes to enclose - whitespace, optional prefixes to match specific fields. Multiple + whitespace, optional prefixes to match specific fields. Multiple search terms are combined as follows: - All commands except print: show transactions/postings/accounts which + All commands except print: show transactions/postings/accounts which match (or negatively match) o any of the description terms AND @@ -518,22 +530,22 @@ QUERIES same as above amt:N, amt:N, amt:>=N - match postings with a single-commodity amount that is equal to, - less than, or greater than N. (Multi-commodity amounts are not + match postings with a single-commodity amount that is equal to, + less than, or greater than N. (Multi-commodity amounts are not tested, and will always match.) The comparison has two modes: if N is preceded by a + or - sign (or is 0), the two signed numbers - are compared. Otherwise, the absolute magnitudes are compared, + are compared. Otherwise, the absolute magnitudes are compared, ignoring sign. code:REGEX match by transaction code (eg check number) cur:REGEX - match postings or transactions including any amounts whose cur- - rency/commodity symbol is fully matched by REGEX. (For a par- + match postings or transactions including any amounts whose cur- + rency/commodity symbol is fully matched by REGEX. (For a par- tial match, use .*REGEX.*). Note, to match characters which are regex-significant, like the dollar sign ($), you need to prepend - \. And when using the command line you need to add one more + \. And when using the command line you need to add one more level of quoting to hide it from the shell, so eg do: hledger print cur:'\$' or hledger print cur:\\$. @@ -542,29 +554,29 @@ QUERIES date:PERIODEXPR match dates within the specified period. PERIODEXPR is a period - expression (with no report interval). Examples: date:2016, - date:thismonth, date:2000/2/1-2/15, date:lastweek-. If the - --date2 command line flag is present, this matches secondary + expression (with no report interval). Examples: date:2016, + date:thismonth, date:2000/2/1-2/15, date:lastweek-. If the + --date2 command line flag is present, this matches secondary dates instead. date2:PERIODEXPR match secondary dates within the specified period. depth:N - match (or display, depending on command) accounts at or above + match (or display, depending on command) accounts at or above this depth real:, real:0 match real or virtual postings respectively status:*, status:!, status: - match cleared, pending, or uncleared/pending transactions + match cleared, pending, or uncleared/pending transactions respectively tag:REGEX[=REGEX] - match by tag name, and optionally also by tag value. Note a - tag: query is considered to match a transaction if it matches - any of the postings. Also remember that postings inherit the + match by tag name, and optionally also by tag value. Note a + tag: query is considered to match a transaction if it matches + any of the postings. Also remember that postings inherit the tags of their parent transaction. not: before any of the above negates the match. @@ -572,24 +584,24 @@ QUERIES inacct:ACCTNAME a special term used automatically when you click an account name in hledger-web, specifying the account register we are currently - in (selects the transactions of that account and how to show - them, can be filtered further with acct etc). Not supported + in (selects the transactions of that account and how to show + them, can be filtered further with acct etc). Not supported elsewhere in hledger. Some of these can also be expressed as command-line options (eg depth:2 - is equivalent to --depth 2). Generally you can mix options and query - arguments, and the resulting query will be their intersection (perhaps + is equivalent to --depth 2). Generally you can mix options and query + arguments, and the resulting query will be their intersection (perhaps excluding the -p/--period option). COMMANDS - hledger provides a number of subcommands; hledger with no arguments + hledger provides a number of subcommands; hledger with no arguments shows a list. If you install additional hledger-* packages, or if you put programs or - scripts named hledger-NAME in your PATH, these will also be listed as + scripts named hledger-NAME in your PATH, these will also be listed as subcommands. - Run a subcommand by writing its name as first argument (eg + Run a subcommand by writing its name as first argument (eg hledger incomestatement). You can also write any unambiguous prefix of a command name (hledger inc), or one of the standard short aliases dis- played in the command list (hledger is). @@ -604,14 +616,14 @@ COMMANDS --drop=N in flat mode: omit N leading account name parts - This command lists all account names that are in use (ie, all the - accounts which have at least one transaction posting to them). With + This command lists all account names that are in use (ie, all the + accounts which have at least one transaction posting to them). With query arguments, only matched account names are shown. - It shows a flat list by default. With --tree, it uses indentation to + It shows a flat list by default. With --tree, it uses indentation to show the account hierarchy. - In flat mode you can add --drop N to omit the first few account name + In flat mode you can add --drop N to omit the first few account name components. Examples: @@ -654,8 +666,8 @@ COMMANDS activity Show an ascii barchart of posting counts per interval. - The activity command displays an ascii histogram showing transaction - counts by day, week, month or other reporting interval (by day is the + The activity command displays an ascii histogram showing transaction + counts by day, week, month or other reporting interval (by day is the default). With query arguments, it counts only matched transactions. $ hledger activity --quarterly @@ -668,24 +680,24 @@ COMMANDS Prompt for transactions and add them to the journal. --no-new-accounts - don't allow creating new accounts; helps prevent typos when + don't allow creating new accounts; helps prevent typos when entering account names - Many hledger users edit their journals directly with a text editor, or - generate them from CSV. For more interactive data entry, there is the - add command, which prompts interactively on the console for new trans- - actions, and appends them to the journal file (if there are multiple + Many hledger users edit their journals directly with a text editor, or + generate them from CSV. For more interactive data entry, there is the + add command, which prompts interactively on the console for new trans- + actions, and appends them to the journal file (if there are multiple -f FILE options, the first file is used.) Existing transactions are not - changed. This is the only hledger command that writes to the journal + changed. This is the only hledger command that writes to the journal file. To use it, just run hledger add and follow the prompts. You can add as - many transactions as you like; when you are finished, enter . or press + many transactions as you like; when you are finished, enter . or press control-d or control-c to exit. Features: - o add tries to provide useful defaults, using the most similar recent + o add tries to provide useful defaults, using the most similar recent transaction (by description) as a template. o You can also set the initial defaults with command line arguments. @@ -693,20 +705,20 @@ COMMANDS o Readline-style edit keys can be used during data entry. o The tab key will auto-complete whenever possible - accounts, descrip- - tions, dates (yesterday, today, tomorrow). If the input area is + tions, dates (yesterday, today, tomorrow). If the input area is empty, it will insert the default value. - o If the journal defines a default commodity, it will be added to any + o If the journal defines a default commodity, it will be added to any bare numbers entered. o A parenthesised transaction code may be entered following a date. o Comments and tags may be entered following a description or amount. - o If you make a mistake, enter < at any prompt to restart the transac- + o If you make a mistake, enter < at any prompt to restart the transac- tion. - o Input prompts are displayed in a different colour when the terminal + o Input prompts are displayed in a different colour when the terminal supports it. Example (see the tutorial for a detailed explanation): @@ -743,7 +755,7 @@ COMMANDS show balance change in each period (default) --cumulative - show balance change accumulated across periods (in multicolumn + show balance change accumulated across periods (in multicolumn reports) -H --historical @@ -757,7 +769,7 @@ COMMANDS account is depth-clipped (default in multicolumn reports) -V --value - convert amounts to current market value in their default valua- + convert amounts to current market value in their default valua- tion commodity -A --average @@ -782,10 +794,10 @@ COMMANDS select the output format. Supported formats: txt, csv. -o FILE --output-file=FILE - write output to FILE. A file extension matching one of the + write output to FILE. A file extension matching one of the above formats selects that format. - The balance command displays accounts and balances. It is hledger's + The balance command displays accounts and balances. It is hledger's most featureful and most useful command. $ hledger balance @@ -802,24 +814,24 @@ COMMANDS -------------------- 0 - More precisely, the balance command shows the change to each account's + More precisely, the balance command shows the change to each account's balance caused by all (matched) postings. In the common case where you - do not filter by date and your journal sets the correct opening bal- + do not filter by date and your journal sets the correct opening bal- ances, this is the same as the account's ending balance. - By default, accounts are displayed hierarchically, with subaccounts + By default, accounts are displayed hierarchically, with subaccounts indented below their parent. "Boring" accounts, which contain a single interesting subaccount and no balance of their own, are elided into the - following line for more compact output. (Use --no-elide to prevent + following line for more compact output. (Use --no-elide to prevent this.) - Each account's balance is the "inclusive" balance - it includes the + Each account's balance is the "inclusive" balance - it includes the balances of any subaccounts. - Accounts which have zero balance (and no non-zero subaccounts) are + Accounts which have zero balance (and no non-zero subaccounts) are omitted. Use -E/--empty to show them. - A final total is displayed by default; use -N/--no-total to suppress + A final total is displayed by default; use -N/--no-total to suppress it: $ hledger balance -p 2008/6 expenses --no-total @@ -829,9 +841,9 @@ COMMANDS Flat mode To see a flat list of full account names instead of the default hierar- - chical display, use --flat. In this mode, accounts (unless + chical display, use --flat. In this mode, accounts (unless depth-clipped) show their "exclusive" balance, excluding any subaccount - balances. In this mode, you can also use --drop N to omit the first + balances. In this mode, you can also use --drop N to omit the first few account name components. $ hledger balance -p 2008/6 expenses -N --flat --drop 1 @@ -839,9 +851,9 @@ COMMANDS $1 supplies Depth limited balance reports - With --depth N, balance shows accounts only to the specified depth. - This is very useful to show a complex charts of accounts in less - detail. In flat mode, balances from accounts below the depth limit + With --depth N, balance shows accounts only to the specified depth. + This is very useful to show a complex charts of accounts in less + detail. In flat mode, balances from accounts below the depth limit will be shown as part of a parent account at the depth limit. $ hledger balance -N --depth 1 @@ -851,12 +863,12 @@ COMMANDS $1 liabilities Multicolumn balance reports - With a reporting interval, multiple balance columns will be shown, one - for each report period. There are three types of multi-column balance + With a reporting interval, multiple balance columns will be shown, one + for each report period. There are three types of multi-column balance report, showing different information: 1. By default: each column shows the sum of postings in that period, ie - the account's change of balance in that period. This is useful eg + the account's change of balance in that period. This is useful eg for a monthly income statement: $ hledger balance --quarterly income expenses -E @@ -871,8 +883,8 @@ COMMANDS -------------------++--------------------------------- || $-1 $1 0 0 - 2. With --cumulative: each column shows the ending balance for that - period, accumulating the changes across periods, starting from 0 at + 2. With --cumulative: each column shows the ending balance for that + period, accumulating the changes across periods, starting from 0 at the report start date: $ hledger balance --quarterly income expenses -E --cumulative @@ -888,8 +900,8 @@ COMMANDS || $-1 0 0 0 3. With --historical/-H: each column shows the actual historical ending - balance for that period, accumulating the changes across periods, - starting from the actual balance at the report start date. This is + balance for that period, accumulating the changes across periods, + starting from the actual balance at the report start date. This is useful eg for a multi-period balance sheet, and when you are showing only the data after a certain start date: @@ -905,26 +917,26 @@ COMMANDS ----------------------++------------------------------------- || 0 0 0 - Multi-column balance reports display accounts in flat mode by default; + Multi-column balance reports display accounts in flat mode by default; to see the hierarchy, use --tree. - With a reporting interval (like --quarterly above), the report - start/end dates will be adjusted if necessary so that they encompass + With a reporting interval (like --quarterly above), the report + start/end dates will be adjusted if necessary so that they encompass the displayed report periods. This is so that the first and last peri- ods will be "full" and comparable to the others. - The -E/--empty flag does two things in multicolumn balance reports: - first, the report will show all columns within the specified report - period (without -E, leading and trailing columns with all zeroes are - not shown). Second, all accounts which existed at the report start - date will be considered, not just the ones with activity during the + The -E/--empty flag does two things in multicolumn balance reports: + first, the report will show all columns within the specified report + period (without -E, leading and trailing columns with all zeroes are + not shown). Second, all accounts which existed at the report start + date will be considered, not just the ones with activity during the report period (use -E to include low-activity accounts which would oth- erwise would be omitted). The -T/--row-total flag adds an additional column showing the total for each row. - The -A/--average flag adds a column showing the average value in each + The -A/--average flag adds a column showing the average value in each row. Here's an example of all three: @@ -947,16 +959,16 @@ COMMANDS Market value The -V/--value flag converts all the reported amounts to their "current - market value" using their default market price. That is the latest - market price (P directive) found in the journal (or an included file), + market value" using their default market price. That is the latest + market price (P directive) found in the journal (or an included file), for the amount's commodity, dated on or before the report end date. Unlike Ledger, hledger's -V only uses the market prices recorded with P - directives, ignoring transaction prices recorded as part of posting + directives, ignoring transaction prices recorded as part of posting amounts (which -B/--cost uses). Using -B and -V together is allowed. Custom balance output - In simple (non-multi-column) balance reports, you can customise the + In simple (non-multi-column) balance reports, you can customise the output with --format FMT: $ hledger balance --format "%20(account) %12(total)" @@ -974,7 +986,7 @@ COMMANDS 0 The FMT format string (plus a newline) specifies the formatting applied - to each account/balance pair. It may contain any suitable text, with + to each account/balance pair. It may contain any suitable text, with data fields interpolated like so: %[MIN][.MAX](FIELDNAME) @@ -985,14 +997,14 @@ COMMANDS o FIELDNAME must be enclosed in parentheses, and can be one of: - o depth_spacer - a number of spaces equal to the account's depth, or + o depth_spacer - a number of spaces equal to the account's depth, or if MIN is specified, MIN * depth spaces. o account - the account's name o total - the account's balance/posted total, right justified - Also, FMT can begin with an optional prefix to control how multi-com- + Also, FMT can begin with an optional prefix to control how multi-com- modity amounts are rendered: o %_ - render on multiple lines, bottom-aligned (the default) @@ -1001,7 +1013,7 @@ COMMANDS o %, - render on one line, comma-separated - There are some quirks. Eg in one-line mode, %(depth_spacer) has no + There are some quirks. Eg in one-line mode, %(depth_spacer) has no effect, instead %(account) has indentation built in. Experimentation may be needed to get pleasing results. @@ -1009,19 +1021,19 @@ COMMANDS o %(total) - the account's total - o %-20.20(account) - the account's name, left justified, padded to 20 + o %-20.20(account) - the account's name, left justified, padded to 20 characters and clipped at 20 characters - o %,%-50(account) %25(total) - account name padded to 50 characters, - total padded to 20 characters, with multiple commodities rendered on + o %,%-50(account) %25(total) - account name padded to 50 characters, + total padded to 20 characters, with multiple commodities rendered on one line - o %20(total) %2(depth_spacer)%-(account) - the default format for the + o %20(total) %2(depth_spacer)%-(account) - the default format for the single-column balance report Output destination - The balance, print, register and stats commands can write their output - to a destination other than the console. This is controlled by the + The balance, print, register and stats commands can write their output + to a destination other than the console. This is controlled by the -o/--output-file option. $ hledger balance -o - # write to stdout (the default) @@ -1029,8 +1041,8 @@ COMMANDS CSV output The balance, print and register commands can write their output as CSV. - This is useful for exporting data to other applications, eg to make - charts in a spreadsheet. This is controlled by the -O/--output-format + This is useful for exporting data to other applications, eg to make + charts in a spreadsheet. This is controlled by the -O/--output-format option, or by specifying a .csv file extension with -o/--output-file. $ hledger balance -O csv # write CSV to stdout @@ -1044,8 +1056,8 @@ COMMANDS --drop=N in flat mode: omit N leading account name parts - This command displays a simple balance sheet. It currently assumes - that you have top-level accounts named asset and liability (plural + This command displays a simple balance sheet. It currently assumes + that you have top-level accounts named asset and liability (plural forms also allowed.) $ hledger balancesheet @@ -1075,9 +1087,9 @@ COMMANDS --drop=N in flat mode: omit N leading account name parts - This command displays a simple cashflow statement It shows the change - in all "cash" (ie, liquid assets) accounts for the period. It cur- - rently assumes that cash accounts are under a top-level account named + This command displays a simple cashflow statement It shows the change + in all "cash" (ie, liquid assets) accounts for the period. It cur- + rently assumes that cash accounts are under a top-level account named asset and do not contain receivable or A/R (plural forms also allowed.) $ hledger cashflow @@ -1097,11 +1109,11 @@ COMMANDS help Show any of the hledger manuals. - The help command displays any of the main hledger man pages. (Unlike - hledger --help, which displays only the hledger man page.) Run it with - no arguments to list available topics (their names are shortened for - easier typing), and run hledger help TOPIC to select one. The output - is similar to a man page, but fixed width. It may be long, so you may + The help command displays any of the main hledger man pages. (Unlike + hledger --help, which displays only the hledger man page.) Run it with + no arguments to list available topics (their names are shortened for + easier typing), and run hledger help TOPIC to select one. The output + is similar to a man page, but fixed width. It may be long, so you may wish to pipe it into a pager. See also info and man. $ hledger help @@ -1130,8 +1142,8 @@ COMMANDS --drop=N in flat mode: omit N leading account name parts - This command displays a simple income statement. It currently assumes - that you have top-level accounts named income (or revenue) and expense + This command displays a simple income statement. It currently assumes + that you have top-level accounts named income (or revenue) and expense (plural forms also allowed.) $ hledger incomestatement @@ -1158,37 +1170,37 @@ COMMANDS info Show any of the hledger manuals using info. - The info command displays any of the hledger reference manuals using - the info hypertextual documentation viewer. This can be a very effi- - cient way to browse large manuals. It requires the "info" program to + The info command displays any of the hledger reference manuals using + the info hypertextual documentation viewer. This can be a very effi- + cient way to browse large manuals. It requires the "info" program to be available in your PATH. - As with help, run it with no arguments to list available topics (manu- + As with help, run it with no arguments to list available topics (manu- als). man Show any of the hledger manuals using man. - The man command displays any of the hledger reference manuals using - man, the standard documentation viewer on unix systems. This will fit - the text to your terminal width, and probably invoke a pager automati- + The man command displays any of the hledger reference manuals using + man, the standard documentation viewer on unix systems. This will fit + the text to your terminal width, and probably invoke a pager automati- cally. It requires the "man" program to be available in your PATH. - As with help, run it with no arguments to list available topics (manu- + As with help, run it with no arguments to list available topics (manu- als). print Show transactions from the journal. -m STR --match=STR - show the transaction whose description is most similar to STR, + show the transaction whose description is most similar to STR, and is most recent -O FMT --output-format=FMT select the output format. Supported formats: txt, csv. -o FILE --output-file=FILE - write output to FILE. A file extension matching one of the + write output to FILE. A file extension matching one of the above formats selects that format. $ hledger print @@ -1213,12 +1225,12 @@ COMMANDS liabilities:debts $1 assets:bank:checking $-1 - The print command displays full transactions from the journal file, - tidily formatted and showing all amounts explicitly. The output of - print is always a valid hledger journal, but it does always not pre- + The print command displays full transactions from the journal file, + tidily formatted and showing all amounts explicitly. The output of + print is always a valid hledger journal, but it does always not pre- serve all original content exactly (eg directives). - hledger's print command also shows all unit prices in effect, or (with + hledger's print command also shows all unit prices in effect, or (with -B/--cost) shows cost amounts. The print command also supports output destination and CSV output. @@ -1230,7 +1242,7 @@ COMMANDS show running total from report start date (default) -H --historical - show historical running total/balance (includes postings before + show historical running total/balance (includes postings before report start date) -A --average @@ -1241,18 +1253,18 @@ COMMANDS show postings' siblings instead -w N --width=N - set output width (default: terminal width or COLUMNS. -wN,M + set output width (default: terminal width or COLUMNS. -wN,M sets description width as well) -O FMT --output-format=FMT select the output format. Supported formats: txt, csv. -o FILE --output-file=FILE - write output to FILE. A file extension matching one of the + write output to FILE. A file extension matching one of the above formats selects that format. The register command displays postings, one per line, and their running - total. This is typically used with a query selecting a particular + total. This is typically used with a query selecting a particular account, to see that account's activity: $ hledger register checking @@ -1261,8 +1273,8 @@ COMMANDS 2008/06/02 save assets:bank:checking $-1 $1 2008/12/31 pay off assets:bank:checking $-1 0 - The --historical/-H flag adds the balance from any undisplayed prior - postings to the running total. This is useful when you want to see + The --historical/-H flag adds the balance from any undisplayed prior + postings to the running total. This is useful when you want to see only recent activity, with a historically accurate running balance: $ hledger register checking -b 2008/6 --historical @@ -1272,23 +1284,23 @@ COMMANDS The --depth option limits the amount of sub-account detail displayed. - The --average/-A flag shows the running average posting amount instead + The --average/-A flag shows the running average posting amount instead of the running total (so, the final number displayed is the average for - the whole report period). This flag implies --empty (see below). It - is affected by --historical. It works best when showing just one + the whole report period). This flag implies --empty (see below). It + is affected by --historical. It works best when showing just one account and one commodity. - The --related/-r flag shows the other postings in the transactions of + The --related/-r flag shows the other postings in the transactions of the postings which would normally be shown. - With a reporting interval, register shows summary postings, one per + With a reporting interval, register shows summary postings, one per interval, aggregating the postings to each account: $ hledger register --monthly income 2008/01 income:salary $-1 $-1 2008/06 income:gifts $-1 $-2 - Periods with no activity, and summary postings with a zero amount, are + Periods with no activity, and summary postings with a zero amount, are not shown by default; use the --empty/-E flag to see them: $ hledger register --monthly income -E @@ -1305,7 +1317,7 @@ COMMANDS 2008/11 0 $-2 2008/12 0 $-2 - Often, you'll want to see just one line per interval. The --depth + Often, you'll want to see just one line per interval. The --depth option helps with this, causing subaccounts to be aggregated: $ hledger register --monthly assets --depth 1h @@ -1313,19 +1325,19 @@ COMMANDS 2008/06 assets $-1 0 2008/12 assets $-1 $-1 - Note when using report intervals, if you specify start/end dates these - will be adjusted outward if necessary to contain a whole number of - intervals. This ensures that the first and last intervals are full + Note when using report intervals, if you specify start/end dates these + will be adjusted outward if necessary to contain a whole number of + intervals. This ensures that the first and last intervals are full length and comparable to the others in the report. Custom register output - register uses the full terminal width by default, except on windows. - You can override this by setting the COLUMNS environment variable (not + register uses the full terminal width by default, except on windows. + You can override this by setting the COLUMNS environment variable (not a bash shell variable) or by using the --width/-w option. - The description and account columns normally share the space equally - (about half of (width - 40) each). You can adjust this by adding a - description width as part of --width's argument, comma-separated: + The description and account columns normally share the space equally + (about half of (width - 40) each). You can adjust this by adding a + description width as part of --width's argument, comma-separated: --width W,D . Here's a diagram: <--------------------------------- width (W) ----------------------------------> @@ -1341,14 +1353,14 @@ COMMANDS $ hledger reg -w 100,40 # set overall width 100, description width 40 $ hledger reg -w $COLUMNS,40 # use terminal width, and set description width - The register command also supports the -o/--output-file and -O/--out- + The register command also supports the -o/--output-file and -O/--out- put-format options for controlling output destination and CSV output. stats Show some journal statistics. -o FILE --output-file=FILE - write output to FILE. A file extension matching one of the + write output to FILE. A file extension matching one of the above formats selects that format. $ hledger stats @@ -1363,8 +1375,8 @@ COMMANDS Accounts : 8 (depth 3) Commodities : 1 ($) - The stats command displays summary information for the whole journal, - or a matched part of it. With a reporting interval, it shows a report + The stats command displays summary information for the whole journal, + or a matched part of it. With a reporting interval, it shows a report for each report period. The stats command also supports -o/--output-file for controlling output @@ -1376,37 +1388,37 @@ COMMANDS $ hledger test Cases: 74 Tried: 74 Errors: 0 Failures: 0 - This command runs hledger's built-in unit tests and displays a quick + This command runs hledger's built-in unit tests and displays a quick report. With a regular expression argument, it selects only tests with matching names. It's mainly used in development, but it's also nice to be able to check your hledger executable for smoke at any time. ADD-ON COMMANDS - Add-on commands are executables in your PATH whose name starts with - hledger- and ends with any of these file extensions: none, - .hs,.lhs,.pl,.py,.rb,.rkt,.sh,.bat,.com,.exe. Also, an add-on's name + Add-on commands are executables in your PATH whose name starts with + hledger- and ends with any of these file extensions: none, + .hs,.lhs,.pl,.py,.rb,.rkt,.sh,.bat,.com,.exe. Also, an add-on's name may not be the same as any built-in command or alias. - hledger will detect these and include them in the command list and let - you invoke them with hledger ADDONCMD. However there are some limita- + hledger will detect these and include them in the command list and let + you invoke them with hledger ADDONCMD. However there are some limita- tions: o Options appearing before ADDONCMD will be visible only to hledger and will not be passed to the add-on. Eg: hledger -h web shows hledger's usage, hledger web -h shows hledger-web's usage. - o Options understood only by the add-on must go after a -- argument to - hide them from hledger, which would otherwise reject them. Eg: + o Options understood only by the add-on must go after a -- argument to + hide them from hledger, which would otherwise reject them. Eg: hledger web -- --server. - Sometimes it may be more convenient to just run the add-on directly, + Sometimes it may be more convenient to just run the add-on directly, eg: hledger-web --server. - Add-ons which are written in haskell can take advantage of the - hledger-lib library for journal parsing, reporting, command-line + Add-ons which are written in haskell can take advantage of the + hledger-lib library for journal parsing, reporting, command-line options, etc. - Here are some hledger add-ons available from Hackage, the extra direc- + Here are some hledger add-ons available from Hackage, the extra direc- tory in the hledger source, or elsewhere: api @@ -1464,11 +1476,11 @@ ADD-ON COMMANDS WF:4303001832 -$6.00 [assets:business:bank:wf:bchecking:banking] $6.00 - ledger-autosync, which includes a hledger-autosync alias, downloads + ledger-autosync, which includes a hledger-autosync alias, downloads transactions from your bank(s) via OFX, and prints just the new ones as journal entries which you can add to your journal. It can also operate - on .OFX files which you've downloaded manually. It can be a nice - alternative to hledger's built-in CSV reader, especially if your bank + on .OFX files which you've downloaded manually. It can be a nice + alternative to hledger's built-in CSV reader, especially if your bank supports OFX download. diff @@ -1494,9 +1506,9 @@ ADD-ON COMMANDS 2015/02/02 (acct:two) $2 - hledger-diff compares two journal files. Given an account name, it - prints out the transactions affecting that account which are in one - journal file but not in the other. This can be useful for reconciling + hledger-diff compares two journal files. Given an account name, it + prints out the transactions affecting that account which are in one + journal file but not in the other. This can be useful for reconciling existing journals with bank statements. equity @@ -1523,14 +1535,14 @@ ADD-ON COMMANDS equity:opening balances 0 This prints a journal entry which zeroes out the specified accounts (or - all accounts) with a transfer to/from "equity:closing balances" (like - Ledger's equity command). Also, it prints an similar entry with oppo- + all accounts) with a transfer to/from "equity:closing balances" (like + Ledger's equity command). Also, it prints an similar entry with oppo- site sign for restoring the balances from "equity:opening balances". These can be useful for ending one journal file and starting a new one, - respectively. By zeroing your asset and liability accounts at the end + respectively. By zeroing your asset and liability accounts at the end of a file and restoring them at the start of the next one, you will see - correct asset/liability balances whether you run hledger on just one + correct asset/liability balances whether you run hledger on just one file, or on several files concatenated with include. interest @@ -1611,11 +1623,11 @@ ADD-ON COMMANDS Liabilities:Bank EUR 3700.00 hledger-interest computes interests for a given account. Using command - line flags, the program can be configured to use various schemes for - day-counting, such as act/act, 30/360, 30E/360, and 30/360isda. Fur- - thermore, it supports a (small) number of interest schemes, i.e. + line flags, the program can be configured to use various schemes for + day-counting, such as act/act, 30/360, 30E/360, and 30/360isda. Fur- + thermore, it supports a (small) number of interest schemes, i.e. annual interest with a fixed rate and the scheme mandated by the German - BGB288 (Basiszins fr Verbrauchergeschfte). See the package page for + BGB288 (Basiszins fr Verbrauchergeschfte). See the package page for more. irr @@ -1673,11 +1685,11 @@ ADD-ON COMMANDS 2011/04/01 - 2011/05/01: 32.24% 2011/05/01 - 2011/06/01: 95.92% - hledger-irr computes the internal rate of return, also known as the - effective interest rate, of a given investment. After specifying what - account holds the investment, and what account stores the gains (or - losses, or fees, or cost), it calculates the hypothetical annual rate - of fixed rate investment that would have provided the exact same cash + hledger-irr computes the internal rate of return, also known as the + effective interest rate, of a given investment. After specifying what + account holds the investment, and what account stores the gains (or + losses, or fees, or cost), it calculates the hypothetical annual rate + of fixed rate investment that would have provided the exact same cash flow. See the package page for more. print-unique @@ -1698,8 +1710,8 @@ ADD-ON COMMANDS entries. hledger-rewrite.hs, in hledger's extra directory (compilation - optional), adds postings to existing transactions, optionally with an - amount based on the existing transaction's first amount. See the + optional), adds postings to existing transactions, optionally with an + amount based on the existing transaction's first amount. See the script for more details. $ hledger rewrite -- [QUERY] --add-posting "ACCT AMTEXPR" ... @@ -1714,26 +1726,26 @@ ADD-ON COMMANDS TROUBLESHOOTING Run-time problems - Here are some issues you might encounter when you run hledger (and - remember you can also seek help from the IRC channel, mail list or bug + Here are some issues you might encounter when you run hledger (and + remember you can also seek help from the IRC channel, mail list or bug tracker): Successfully installed, but "No command 'hledger' found" stack and cabal install binaries into a special directory, which should - be added to your PATH environment variable. Eg on unix-like systems, + be added to your PATH environment variable. Eg on unix-like systems, that is ~/.local/bin and ~/.cabal/bin respectively. I set a custom LEDGER_FILE, but hledger is still using the default file - LEDGER_FILE should be a real environment variable, not just a shell - variable. The command env | grep LEDGER_FILE should show it. You may + LEDGER_FILE should be a real environment variable, not just a shell + variable. The command env | grep LEDGER_FILE should show it. You may need to use export. Here's an explanation. - "Illegal byte sequence" or "Invalid or incomplete multibyte or wide + "Illegal byte sequence" or "Invalid or incomplete multibyte or wide character" errors In order to handle non-ascii letters and symbols (like ), hledger needs an appropriate locale. This is usually configured system-wide; you can also configure it temporarily. The locale may need to be one that sup- - ports UTF-8, if you built hledger with GHC < 7.2 (or possibly always, + ports UTF-8, if you built hledger with GHC < 7.2 (or possibly always, I'm not sure yet). Here's an example of setting the locale temporarily, on ubuntu @@ -1752,7 +1764,7 @@ TROUBLESHOOTING $ echo "export LANG=en_US.UTF-8" >>~/.bash_profile $ bash --login - If we preferred to use eg fr_FR.utf8, we might have to install that + If we preferred to use eg fr_FR.utf8, we might have to install that first: $ apt-get install language-pack-fr @@ -1773,45 +1785,45 @@ TROUBLESHOOTING Known limitations Command line interface - Add-on command options, unless they are also understood by the main - hledger executable, must be written after --, like this: + Add-on command options, unless they are also understood by the main + hledger executable, must be written after --, like this: hledger web -- --server Differences from Ledger - Not all of Ledger's journal file syntax is supported. See file format + Not all of Ledger's journal file syntax is supported. See file format differences. - hledger is slower than Ledger, and uses more memory, on large data + hledger is slower than Ledger, and uses more memory, on large data files. Windows limitations - In a windows CMD window, non-ascii characters and colours are not sup- + In a windows CMD window, non-ascii characters and colours are not sup- ported. In a windows Cygwin/MSYS/Mintty window, the tab key is not supported in hledger add. ENVIRONMENT - COLUMNS The screen width used by the register command. Default: the + COLUMNS The screen width used by the register command. Default: the full terminal width. LEDGER_FILE The journal file path when not specified with -f. Default: - ~/.hledger.journal (on windows, perhaps C:/Users/USER/.hledger.jour- + ~/.hledger.journal (on windows, perhaps C:/Users/USER/.hledger.jour- nal). FILES - Reads data from one or more files in hledger journal, timeclock, time- - dot, or CSV format specified with -f, or $LEDGER_FILE, or - $HOME/.hledger.journal (on windows, perhaps + Reads data from one or more files in hledger journal, timeclock, time- + dot, or CSV format specified with -f, or $LEDGER_FILE, or + $HOME/.hledger.journal (on windows, perhaps C:/Users/USER/.hledger.journal). BUGS - The need to precede options with -- when invoked from hledger is awk- + The need to precede options with -- when invoked from hledger is awk- ward. - hledger can't render non-ascii characters when run from a Windows com- + hledger can't render non-ascii characters when run from a Windows com- mand prompt (up to Windows 7 at least). When input data contains non-ascii characters, a suitable system locale @@ -1821,7 +1833,7 @@ BUGS REPORTING BUGS - Report bugs at http://bugs.hledger.org (or on the #hledger IRC channel + Report bugs at http://bugs.hledger.org (or on the #hledger IRC channel or hledger mail list) @@ -1835,7 +1847,7 @@ COPYRIGHT SEE ALSO - hledger(1), hledger-ui(1), hledger-web(1), hledger-api(1), + hledger(1), hledger-ui(1), hledger-web(1), hledger-api(1), hledger_csv(5), hledger_journal(5), hledger_timeclock(5), hledger_time- dot(5), ledger(1) diff --git a/hledger/doc/options.m4.md b/hledger/doc/options.m4.md index f7d4490b1..e14504746 100644 --- a/hledger/doc/options.m4.md +++ b/hledger/doc/options.m4.md @@ -75,13 +75,6 @@ Eg -p jan -p feb is equivalent to -p feb. ## Input files hledger reads transactions from a data file (and the add command writes to it). -Usually this is in hledger's journal format, -but it can also be one of the other supported file types, such as -timeclock, -timedot, -CSV, -or a C++ Ledger journal (partial support). - By default this file is `$HOME/.hledger.journal` (or on Windows, something like `C:/Users/USER/.hledger.journal`). You can override this with the `$LEDGER_FILE` environment variable: @@ -91,30 +84,41 @@ $ hledger stats ``` or with the `-f/--file` option: ```bash -$ hledger -f some/file.ext stats +$ hledger -f /some/file stats ``` -hledger tries to identify the file format based on the file extension, -as follows: - -| File extension: | Use format: -|-------------------------------------------|---------------- -| `.journal`, `.j`, `.hledger`, `.ledger` | journal -| `.timeclock` | timeclock -| `.timedot` | timedot -| `.csv` | CSV - -If the file name has some other extension, or none, -hledger tries each of these formats in turn. -(Plus one more: the experimental "ledger" format, an alternate - parser for C++ Ledger journals, which we try only as a last resort - as it's new and hledger's journal parser works better for now.) - The file name `-` (hyphen) means standard input, as usual: ```bash $ cat some.journal | hledger -f- ``` +Usually this file is in hledger's journal format, +but it can also be one of several other formats, shown below. +hledger tries to identify the format based on the file extension, as follows: + +| Format: | Description: | File extensions: +|------------|---------------------------------------------|------------------------------------------- +| journal | hledger's journal format | `.journal`, `.j`, `.hledger`, `.ledger` +| timeclock | timeclock files (precise time logging) | `.timeclock` +| timedot | timedot files (approximate time logging) | `.timedot` +| CSV | comma-separated values (data interchange) | `.csv` + +hledger identifies the format based on the file extension if possible. +If that does not identify a known format, it tries each format in turn. + +If needed, eg to ensure correct error messages, you can force a specific format +by prepending it to the file path with a colon. Examples: +```bash +$ hledger -f csv:/some/csv-file.dat stats +$ echo 'i 2009/13/1 08:00:00' | hledger print -ftimeclock:- +``` + +Some other experimental formats are available but not yet used by default: + +| Format: | Description: | File extensions: +|------------|---------------------------------------------|------------------------------------------- +| ledger | Ledger's journal format (incomplete) | + You can specify multiple `-f` options, to read multiple files as one big journal. Directives in one file will not affect subsequent files in this case (if you need that, use the [include directive](#including-other-files) instead).