| Safe Haskell | None |
|---|---|
| Language | GHC2021 |
Hledger.Cli
Description
This is the root module of the hledger package,
providing hledger's command-line interface.
The main function,
commands,
command-line options,
and utilities useful to other hledger command-line programs
are exported.
It also re-exports hledger-lib:Hledger
and cmdargs:System.Concole.CmdArgs.Explicit
See also:
- hledger-lib:Hledger
- The README files
- The high-level developer docs
hledger is a Haskell rewrite of John Wiegley's "ledger". It generates financial reports from a plain text general journal. You can use the command line:
$ hledger
or ghci:
$ make ghci
ghci> Right j <- runExceptT $ readJournalFile definputopts "examples/sample.journal" -- or: j <- defaultJournal
ghci> :t j
j :: Journal
ghci> stats defcliopts j
Main file : examples/sample.journal
Included files :
Transactions span : 2008-01-01 to 2009-01-01 (366 days)
Last transaction : 2008-12-31 (733772 days from now)
Transactions : 5 (0.0 per day)
Transactions last 30 days: 0 (0.0 per day)
Transactions last 7 days : 0 (0.0 per day)
Payees/descriptions : 5
Accounts : 8 (depth 3)
Commodities : 1 ($)
Market prices : 0 ()
Run time (throughput) : 1695276900.00s (0 txns/s)
ghci> balance defcliopts j
$1 assets:bank:saving
$-2 assets:cash
$1 expenses:food
$1 expenses:supplies
$-1 income:gifts
$-1 income:salary
$1 liabilities:debts
--------------------
0
ghci> etc.
SPDX-License-Identifier: GPL-3.0-or-later Copyright (c) 2007-2025 (each year in this range) Simon Michael simon@joyful.com and contributors.
This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see https://www.gnu.org/licenses/.
Synopsis
- main :: IO ()
- mainmode :: [Name] -> Mode RawOpts
- argsToCliOpts :: [String] -> [String] -> IO CliOpts
- module Hledger.Cli.CliOptions
- module Hledger.Cli.Conf
- module Hledger.Cli.Commands
- module Hledger.Cli.DocFiles
- module Hledger.Cli.Utils
- module Hledger.Cli.Version
- module Hledger
- process :: Mode a -> [String] -> Either String a
- data Arg a = Arg {}
- expandArgsAt :: [String] -> IO [String]
- joinArgs :: [String] -> String
- splitArgs :: String -> [String]
- class Remap (m :: Type -> Type) where
- remap :: (a -> b) -> (b -> (a, a -> b)) -> m a -> m b
- data Flag a = Flag {}
- type Update a = String -> a -> Either String a
- data FlagInfo
- data Mode a = Mode {
- modeGroupModes :: Group (Mode a)
- modeNames :: [Name]
- modeValue :: a
- modeCheck :: a -> Either String a
- modeReform :: a -> Maybe [String]
- modeExpandAt :: Bool
- modeHelp :: Help
- modeHelpSuffix :: [String]
- modeArgs :: ([Arg a], Maybe (Arg a))
- modeGroupFlags :: Group (Flag a)
- data Group a = Group {
- groupUnnamed :: [a]
- groupHidden :: [a]
- groupNamed :: [(Help, [a])]
- type FlagHelp = String
- type Help = String
- parseBool :: String -> Maybe Bool
- fromGroup :: Group a -> [a]
- toGroup :: [a] -> Group a
- modeModes :: Mode a -> [Mode a]
- modeFlags :: Mode a -> [Flag a]
- fromFlagOpt :: FlagInfo -> String
- checkMode :: Mode a -> Maybe String
- remap2 :: Remap m => (a -> b) -> (b -> a) -> m a -> m b
- remapUpdate :: (a -> b) -> (b -> (a, a -> b)) -> Update a -> Update b
- modeEmpty :: a -> Mode a
- mode :: Name -> a -> Help -> Arg a -> [Flag a] -> Mode a
- modes :: String -> a -> Help -> [Mode a] -> Mode a
- flagNone :: [Name] -> (a -> a) -> Help -> Flag a
- flagOpt :: String -> [Name] -> Update a -> FlagHelp -> Help -> Flag a
- flagReq :: [Name] -> Update a -> FlagHelp -> Help -> Flag a
- flagArg :: Update a -> FlagHelp -> Arg a
- flagBool :: [Name] -> (Bool -> a -> a) -> Help -> Flag a
- data Complete
- complete :: Mode a -> [String] -> (Int, Int) -> [Complete]
- data HelpFormat
- helpText :: [String] -> HelpFormat -> Mode a -> [Text]
- processArgs :: Mode a -> IO a
- processValue :: Mode a -> [String] -> a
- processValueIO :: Mode a -> [String] -> IO a
- flagHelpSimple :: (a -> a) -> Flag a
- flagHelpFormat :: (HelpFormat -> TextFormat -> a -> a) -> Flag a
- flagVersion :: (a -> a) -> Flag a
- flagNumericVersion :: (a -> a) -> Flag a
- flagsVerbosity :: (Verbosity -> a -> a) -> [Flag a]
Documentation
hledger CLI's main procedure.
Here we will parse the command line, read any config file, and search for hledger-* addon executables in the user's PATH, then choose the appropriate builtin operation or addon operation to run, then run it in the right way, usually reading input data (eg a journal) first.
When making a CLI usable and robust with main command, builtin subcommands, various kinds of addon commands, and config files that add general and command-specific options, while balancing circular dependencies, environment, idioms, legacy, and libraries with their own requirements and limitations: things get crazy, and there is a tradeoff against complexity and bug risk. We try to provide the most intuitive, expressive and robust CLI that's feasible while keeping the CLI processing below sufficiently comprehensible, troubleshootable, and tested. It's an ongoing quest. See also: Hledger.Cli.CliOptions, cli.test, addons.test, --debug and --debug=8.
Probably the biggest source of complexity here is that cmdargs can't parse a command line containing undeclared flags, but this arises often with our addon commands and builtin/custom commands which haven't implemented all options, so we have to work hard to work around this. https://github.com/ndmitchell/cmdargs/issues/36 is the wishlist issue; implementing that would simplify hledger's CLI processing a lot.
mainmode :: [Name] -> Mode RawOpts Source #
The overall cmdargs mode describing hledger's command-line options and subcommands. The names of known addons are provided so they too can be recognised as commands.
argsToCliOpts :: [String] -> [String] -> IO CliOpts Source #
A helper for addons/scripts: this parses hledger CliOpts from these command line arguments and add-on command names, roughly how hledger main does. If option parsing/validating fails, it exits the program with usageError. Unlike main, this does not read extra args from a config file or search for addons; to do those things, mimic the code in main for now.
Re-exports
module Hledger.Cli.CliOptions
module Hledger.Cli.Conf
module Hledger.Cli.Commands
module Hledger.Cli.DocFiles
module Hledger.Cli.Utils
module Hledger.Cli.Version
module Hledger
System.Console.CmdArgs.Explicit
Instances
| Remap Arg | |
Defined in System.Console.CmdArgs.Explicit.Type | |
| Packer a => Packer (Arg a) | |
Defined in System.Console.CmdArgs.Helper | |
| Show (Arg a) | |
expandArgsAt :: [String] -> IO [String] #
class Remap (m :: Type -> Type) where #
Instances
| Remap Arg | |
Defined in System.Console.CmdArgs.Explicit.Type | |
| Remap Flag | |
Defined in System.Console.CmdArgs.Explicit.Type | |
| Remap Mode | |
Defined in System.Console.CmdArgs.Explicit.Type | |
Constructors
| Flag | |
Instances
| Remap Flag | |
Defined in System.Console.CmdArgs.Explicit.Type | |
| Packer a => Packer (Flag a) | |
Defined in System.Console.CmdArgs.Helper | |
| Show (Flag a) | |
Instances
| Packer FlagInfo | |
Defined in System.Console.CmdArgs.Helper | |
| Show FlagInfo | |
| Eq FlagInfo | |
| Ord FlagInfo | |
Defined in System.Console.CmdArgs.Explicit.Type | |
Constructors
| Mode | |
Fields
| |
Instances
| Remap Mode | |
Defined in System.Console.CmdArgs.Explicit.Type | |
| Packer a => Packer (Mode a) | |
Defined in System.Console.CmdArgs.Helper | |
| Show (Mode a) | |
Constructors
| Group | |
Fields
| |
fromFlagOpt :: FlagInfo -> String #
remapUpdate :: (a -> b) -> (b -> (a, a -> b)) -> Update a -> Update b #
Constructors
| CompleteValue String | |
| CompleteFile String FilePath | |
| CompleteDir String FilePath |
Instances
| Show Complete | |
| Eq Complete | |
| Ord Complete | |
Defined in System.Console.CmdArgs.Explicit.Complete | |
data HelpFormat #
Instances
helpText :: [String] -> HelpFormat -> Mode a -> [Text] #
processArgs :: Mode a -> IO a #
processValue :: Mode a -> [String] -> a #
processValueIO :: Mode a -> [String] -> IO a #
flagHelpSimple :: (a -> a) -> Flag a #
flagHelpFormat :: (HelpFormat -> TextFormat -> a -> a) -> Flag a #
flagVersion :: (a -> a) -> Flag a #
flagNumericVersion :: (a -> a) -> Flag a #
flagsVerbosity :: (Verbosity -> a -> a) -> [Flag a] #