hledger-0.26: The main command-line interface for the hledger accounting tool.

Safe Haskell | None
Language | Haskell2010

Hledger.Cli.Options

Contents

- cmdargs flags & modes
- CLI options
- CLI option accessors
- Other utils
- Tests

Description

Common cmdargs modes and flags, a command-line options type, and related utilities used by hledger commands.

Synopsis

- helpflags :: [Flag RawOpts]
- detailedversionflag :: Flag RawOpts
- inputflags :: [Flag RawOpts]
- reportflags :: [Flag RawOpts]
- outputflags :: [Flag RawOpts]
- generalflagsgroup1 :: (String, [Flag RawOpts])
- generalflagsgroup2 :: (String, [Flag RawOpts])
- generalflagsgroup3 :: (String, [Flag RawOpts])
- defMode :: Mode RawOpts
- defCommandMode :: [Name] -> Mode RawOpts
- defAddonCommandMode :: Name -> Mode RawOpts
- argsFlag :: FlagHelp -> Arg RawOpts
- showModeHelp :: Mode a -> String
- withAliases :: String -> [String] -> String
- data CliOpts = CliOpts {
  - rawopts_ :: RawOpts
  - command_ :: String
  - file_ :: [FilePath]
  - rules_file_ :: Maybe FilePath
  - output_file_ :: Maybe FilePath
  - output_format_ :: Maybe String
  - alias_ :: [String]
  - ignore_assertions_ :: Bool
  - debug_ :: Int
  - no_new_accounts_ :: Bool
  - width_ :: Maybe String
  - available_width_ :: Int
  - reportopts_ :: ReportOpts
  }
- defcliopts :: CliOpts
- getCliOpts :: Mode RawOpts -> IO CliOpts
- decodeRawOpts :: RawOpts -> RawOpts
- rawOptsToCliOpts :: RawOpts -> IO CliOpts
- checkCliOpts :: CliOpts -> IO CliOpts
- outputFormats :: [[Char]]
- defaultOutputFormat :: [Char]
- aliasesFromOpts :: CliOpts -> [AccountAlias]
- journalFilePathFromOpts :: CliOpts -> IO [String]
- rulesFilePathFromOpts :: CliOpts -> IO (Maybe FilePath)
- outputFileFromOpts :: CliOpts -> IO FilePath
- outputFormatFromOpts :: CliOpts -> String
- defaultWidth :: Int
- widthFromOpts :: CliOpts -> Int
- registerWidthsFromOpts :: CliOpts -> (Int, Maybe Int)
- maybeAccountNameDrop :: ReportOpts -> AccountName -> AccountName
- lineFormatFromOpts :: ReportOpts -> Either String [OutputFormat]
- hledgerAddons :: IO ([String], [String])
- tests_Hledger_Cli_Options :: Test

# cmdargs flags & modes

helpflags :: [Flag RawOpts] Source

Common help flags: --help, --debug, --version...

detailedversionflag :: Flag RawOpts Source

A hidden flag, just for the hledger executable.

inputflags :: [Flag RawOpts] Source

Common input-related flags: --file, --rules-file, --alias...

reportflags :: [Flag RawOpts] Source

Common report-related flags: --period, --cost, etc.

outputflags :: [Flag RawOpts] Source

Common output-related flags: --output-file, --output-format...

generalflagsgroup1 :: (String, [Flag RawOpts]) Source

generalflagsgroup2 :: (String, [Flag RawOpts]) Source

generalflagsgroup3 :: (String, [Flag RawOpts]) Source

defMode :: Mode RawOpts Source

A basic mode template.

defCommandMode :: [Name] -> Mode RawOpts Source

A basic subcommand mode with the given command name(s).

defAddonCommandMode :: Name -> Mode RawOpts Source

A basic subcommand mode suitable for an add-on command.

argsFlag :: FlagHelp -> Arg RawOpts Source

showModeHelp :: Mode a -> String Source

Get a mode's help message as a nicely wrapped string.

withAliases :: String -> [String] -> String Source

Add command aliases to the command's help string.

# CLI options

data CliOpts Source

Command line options, used in the `hledger` package and above. This is the "opts" used throughout hledger CLI code. representing the options that arguments that were provided at startup on the command-line.

Constructors

CliOpts |

Fields

rawopts_ :: RawOpts
command_ :: String
file_ :: [FilePath]
rules_file_ :: Maybe FilePath
output_file_ :: Maybe FilePath
output_format_ :: Maybe String
alias_ :: [String]
ignore_assertions_ :: Bool
debug_ :: Int

  debug level, set by `--debug[=N]`. See also `debugLevel`.

no_new_accounts_ :: Bool
width_ :: Maybe String

  the --width value provided, if any

available_width_ :: Int

  estimated usable screen width, based on 1. the COLUMNS env var, if set 2. the width reported by the terminal, if supported 3. the default (80)

reportopts_ :: ReportOpts

Instances

Data CliOpts Source |
Show CliOpts Source |
Default CliOpts Source |

defcliopts :: CliOpts Source

getCliOpts :: Mode RawOpts -> IO CliOpts Source

Parse hledger CLI options from the command line using the given cmdargs mode, and either return them or, if a help flag is present, print the mode help and exit the program.

decodeRawOpts :: RawOpts -> RawOpts Source

Convert possibly encoded option values to regular unicode strings.

rawOptsToCliOpts :: RawOpts -> IO CliOpts Source

Parse raw option string values to the desired final data types. Any relative smart dates will be converted to fixed dates based on today's date. Parsing failures will raise an error. Also records the terminal width, if supported.

checkCliOpts :: CliOpts -> IO CliOpts Source

Do final validation of processed opts, raising an error if there is trouble.

outputFormats :: [[Char]] Source

defaultOutputFormat :: [Char] Source

# CLI option accessors

These do the extra processing required for some options.

aliasesFromOpts :: CliOpts -> [AccountAlias] Source

Get the account name aliases from options, if any.

journalFilePathFromOpts :: CliOpts -> IO [String] Source

Get the (tilde-expanded, absolute) journal file path from 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.

rulesFilePathFromOpts :: CliOpts -> IO (Maybe FilePath) Source

Get the (tilde-expanded) rules file path from options, if any.

outputFileFromOpts :: CliOpts -> IO FilePath Source

Get the expanded, absolute output file path from options, or the default (-, meaning stdout).

outputFormatFromOpts :: CliOpts -> String Source

Get the output format from the --output-format option, otherwise from a recognised file extension in the --output-file option, otherwise the default (txt).

defaultWidth :: Int Source

Default width for hledger console output, when not otherwise specified.

widthFromOpts :: CliOpts -> Int Source

Get the width in characters to use for console output. This comes from the --width option, or the COLUMNS environment variable, or (on posix platforms) the current terminal width, or 80. Will raise a parse error for a malformed --width argument.

For register:

registerWidthsFromOpts :: CliOpts -> (Int, Maybe Int) Source

Get the width in characters to use for the register command's console output, and also the description column width if specified (following the main width, comma-separated). The widths will be as follows: `no --width flag - overall width is the available width (COLUMNS, or posix terminal width, or 80); description width is unspecified (auto) --width W - overall width is W, description width is auto --width W,D - overall width is W, description width is D` Will raise a parse error for a malformed --width argument.

maybeAccountNameDrop :: ReportOpts -> AccountName -> AccountName Source

Drop leading components of accounts names as specified by --drop, but only in --flat mode.

For balance:

lineFormatFromOpts :: ReportOpts -> Either String [OutputFormat] Source

Parse the format option if provided, possibly returning an error, otherwise get the default value.

# Other utils

hledgerAddons :: IO ([String], [String]) Source

Get the sorted unique precise names and display names of hledger add-ons found in the current user's PATH. The precise names are the add-on's filename with the "hledger-" prefix removed. The display names have the file extension removed also, except when it's needed for disambiguation.

- - Also when there are exactly two similar names, one with the .hs or
- - .lhs extension and the other with the .exe extension or no
- - extension - presumably source and compiled versions of a haskell
- - script - we exclude the source version.

This function can return add-on names which shadow built-in command names, but hledger will ignore these.

# Tests

tests_Hledger_Cli_Options :: Test Source