hledger-1.52: Command-line interface for the hledger accounting system
Safe HaskellNone
LanguageGHC2021

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

Documentation

main :: IO () Source #

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

System.Console.CmdArgs.Explicit

process :: Mode a -> [String] -> Either String a #

data Arg a #

Constructors

Arg 

Instances

Instances details
Remap Arg 
Instance details

Defined in System.Console.CmdArgs.Explicit.Type

Methods

remap :: (a -> b) -> (b -> (a, a -> b)) -> Arg a -> Arg b #

Packer a => Packer (Arg a) 
Instance details

Defined in System.Console.CmdArgs.Helper

Methods

pack :: Arg a -> Pack

unpack :: Pack -> Arg a

Show (Arg a) 
Instance details

Defined in System.Console.CmdArgs.Explicit.Help

Methods

showsPrec :: Int -> Arg a -> ShowS Source #

show :: Arg a -> String Source #

showList :: [Arg a] -> ShowS Source #

class Remap (m :: Type -> Type) where #

Methods

remap :: (a -> b) -> (b -> (a, a -> b)) -> m a -> m b #

Instances

Instances details
Remap Arg 
Instance details

Defined in System.Console.CmdArgs.Explicit.Type

Methods

remap :: (a -> b) -> (b -> (a, a -> b)) -> Arg a -> Arg b #

Remap Flag 
Instance details

Defined in System.Console.CmdArgs.Explicit.Type

Methods

remap :: (a -> b) -> (b -> (a, a -> b)) -> Flag a -> Flag b #

Remap Mode 
Instance details

Defined in System.Console.CmdArgs.Explicit.Type

Methods

remap :: (a -> b) -> (b -> (a, a -> b)) -> Mode a -> Mode b #

data Flag a #

Constructors

Flag 

Instances

Instances details
Remap Flag 
Instance details

Defined in System.Console.CmdArgs.Explicit.Type

Methods

remap :: (a -> b) -> (b -> (a, a -> b)) -> Flag a -> Flag b #

Packer a => Packer (Flag a) 
Instance details

Defined in System.Console.CmdArgs.Helper

Methods

pack :: Flag a -> Pack

unpack :: Pack -> Flag a

Show (Flag a) 
Instance details

Defined in System.Console.CmdArgs.Explicit.Help

Methods

showsPrec :: Int -> Flag a -> ShowS Source #

show :: Flag a -> String Source #

showList :: [Flag a] -> ShowS Source #

type Update a = String -> a -> Either String a #

data Mode a #

Constructors

Mode 

Instances

Instances details
Remap Mode 
Instance details

Defined in System.Console.CmdArgs.Explicit.Type

Methods

remap :: (a -> b) -> (b -> (a, a -> b)) -> Mode a -> Mode b #

Packer a => Packer (Mode a) 
Instance details

Defined in System.Console.CmdArgs.Helper

Methods

pack :: Mode a -> Pack

unpack :: Pack -> Mode a

Show (Mode a) 
Instance details

Defined in System.Console.CmdArgs.Explicit.Help

Methods

showsPrec :: Int -> Mode a -> ShowS Source #

show :: Mode a -> String Source #

showList :: [Mode a] -> ShowS Source #

data Group a #

Constructors

Group 

Fields

Instances

Instances details
Functor Group 
Instance details

Defined in System.Console.CmdArgs.Explicit.Type

Methods

fmap :: (a -> b) -> Group a -> Group b Source #

(<$) :: a -> Group b -> Group a Source #

Packer a => Packer (Group a) 
Instance details

Defined in System.Console.CmdArgs.Helper

Methods

pack :: Group a -> Pack

unpack :: Pack -> Group a

Monoid (Group a) 
Instance details

Defined in System.Console.CmdArgs.Explicit.Type

Methods

mempty :: Group a Source #

mappend :: Group a -> Group a -> Group a Source #

mconcat :: [Group a] -> Group a Source #

Semigroup (Group a) 
Instance details

Defined in System.Console.CmdArgs.Explicit.Type

Methods

(<>) :: Group a -> Group a -> Group a Source #

sconcat :: NonEmpty (Group a) -> Group a Source #

stimes :: Integral b => b -> Group a -> Group a Source #

Show a => Show (Group a) 
Instance details

Defined in System.Console.CmdArgs.Explicit.Type

type Help = String #

fromGroup :: Group a -> [a] #

toGroup :: [a] -> Group a #

modeModes :: Mode a -> [Mode a] #

modeFlags :: Mode a -> [Flag a] #

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 #

complete :: Mode a -> [String] -> (Int, Int) -> [Complete] #

data HelpFormat #

Instances

Instances details
Default HelpFormat 
Instance details

Defined in System.Console.CmdArgs.Explicit.Help

Methods

def :: HelpFormat

Bounded HelpFormat 
Instance details

Defined in System.Console.CmdArgs.Explicit.Help

Enum HelpFormat 
Instance details

Defined in System.Console.CmdArgs.Explicit.Help

Read HelpFormat 
Instance details

Defined in System.Console.CmdArgs.Explicit.Help

Show HelpFormat 
Instance details

Defined in System.Console.CmdArgs.Explicit.Help

Eq HelpFormat 
Instance details

Defined in System.Console.CmdArgs.Explicit.Help

Ord HelpFormat 
Instance details

Defined in System.Console.CmdArgs.Explicit.Help

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] #