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

Safe Haskell | None

Hledger.Cli.Options

Contents

- cmdargs modes & flags
- raw options
- CLI options
- CLI option accessors
- utilities
- tests

Description

Command-line options for the hledger program, and related utilities.

Synopsis

- mainmode :: [Name] -> Mode RawOpts
- activitymode :: Mode RawOpts
- addmode :: Mode RawOpts
- balancemode :: Mode RawOpts
- balancesheetmode :: Mode RawOpts
- cashflowmode :: Mode RawOpts
- incomestatementmode :: Mode RawOpts
- printmode :: Mode RawOpts
- registermode :: Mode RawOpts
- statsmode :: Mode RawOpts
- testmode :: Mode RawOpts
- convertmode :: Mode RawOpts
- defCommandMode :: [Name] -> Mode RawOpts
- argsFlag :: FlagHelp -> Arg [([Char], [Char])]
- helpflags :: [Flag [([Char], [Char])]]
- inputflags :: [Flag [([Char], [Char])]]
- reportflags :: [Flag [([Char], [Char])]]
- generalflagsgroup1 :: ([Char], [Flag [([Char], [Char])]])
- generalflagsgroup2 :: ([Char], [Flag [([Char], [Char])]])
- generalflagsgroup3 :: ([Char], [Flag [([Char], [Char])]])
- type RawOpts = [(String, String)]
- inRawOpts :: String -> RawOpts -> Bool
- boolopt :: String -> RawOpts -> Bool
- intopt :: String -> RawOpts -> Int
- maybeintopt :: String -> RawOpts -> Maybe Int
- stringopt :: Eq a => a -> [(a, String)] -> [Char]
- maybestringopt :: Eq a => a -> [(a, String)] -> Maybe String
- listofstringopt :: Eq a => a -> [(a, t)] -> [t]
- setopt :: t -> [Char] -> [(t, [Char])] -> [(t, [Char])]
- setboolopt :: t -> [(t, [Char])] -> [(t, [Char])]
- data CliOpts = CliOpts {
  - rawopts_ :: RawOpts
  - command_ :: String
  - file_ :: Maybe FilePath
  - rules_file_ :: Maybe FilePath
  - alias_ :: [String]
  - debug_ :: Int
  - no_new_accounts_ :: Bool
  - width_ :: Maybe String
  - reportopts_ :: ReportOpts
  }
- defcliopts :: CliOpts
- aliasesFromOpts :: CliOpts -> [(AccountName, AccountName)]
- formatFromOpts :: ReportOpts -> Either String [FormatString]
- journalFilePathFromOpts :: CliOpts -> IO String
- rulesFilePathFromOpts :: CliOpts -> IO (Maybe FilePath)
- data OutputWidth
  - = TotalWidth Width
  - | FieldWidths [Width]
- data Width
  - = Width Int
  - | Auto
- defaultWidth :: Int
- defaultWidthWithFlag :: Int
- widthFromOpts :: CliOpts -> Either String OutputWidth
- getHledgerAddonCommands :: IO [String]
- argsToCliOpts :: [String] -> [String] -> IO CliOpts
- moveFlagsAfterCommand :: [String] -> [String]
- decodeRawOpts :: [(t, SystemString)] -> [(t, String)]
- checkCliOpts :: CliOpts -> IO CliOpts
- rawOptsToCliOpts :: RawOpts -> IO CliOpts
- optserror :: [Char] -> c
- showModeHelp :: Mode a -> String
- debugArgs :: [String] -> CliOpts -> IO ()
- getCliOpts :: Mode RawOpts -> IO CliOpts
- tests_Hledger_Cli_Options :: Test

# cmdargs modes & flags

These tell cmdargs how to parse the command line arguments. There's one mode for each internal subcommand, plus a main mode.

mainmode :: [Name] -> Mode RawOpts Source

The top-level cmdargs mode for hledger.

activitymode :: Mode RawOpts Source

addmode :: Mode RawOpts Source

balancemode :: Mode RawOpts Source

balancesheetmode :: Mode RawOpts Source

cashflowmode :: Mode RawOpts Source

incomestatementmode :: Mode RawOpts Source

printmode :: Mode RawOpts Source

registermode :: Mode RawOpts Source

statsmode :: Mode RawOpts Source

testmode :: Mode RawOpts Source

convertmode :: Mode RawOpts Source

defCommandMode :: [Name] -> Mode RawOpts Source

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

argsFlag :: FlagHelp -> Arg [([Char], [Char])]Source

helpflags :: [Flag [([Char], [Char])]]Source

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

inputflags :: [Flag [([Char], [Char])]]Source

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

reportflags :: [Flag [([Char], [Char])]]Source

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

generalflagsgroup1 :: ([Char], [Flag [([Char], [Char])]])Source

generalflagsgroup2 :: ([Char], [Flag [([Char], [Char])]])Source

generalflagsgroup3 :: ([Char], [Flag [([Char], [Char])]])Source

# raw options

To allow the cmdargs modes to be reused and extended by other packages (eg, add-ons which want to mimic the standard hledger options), we parse the command-line arguments to a simple association list, not a fixed ADT.

type RawOpts = [(String, String)]Source

Our cmdargs modes parse arguments into an association list for better reuse.

inRawOpts :: String -> RawOpts -> Bool Source

Is the named option present ?

boolopt :: String -> RawOpts -> Bool Source

intopt :: String -> RawOpts -> Int Source

maybeintopt :: String -> RawOpts -> Maybe Int Source

stringopt :: Eq a => a -> [(a, String)] -> [Char]Source

maybestringopt :: Eq a => a -> [(a, String)] -> Maybe String Source

listofstringopt :: Eq a => a -> [(a, t)] -> [t]Source

setopt :: t -> [Char] -> [(t, [Char])] -> [(t, [Char])]Source

setboolopt :: t -> [(t, [Char])] -> [(t, [Char])]Source

# CLI options

Raw options are converted to a more convenient, package-specific options structure. This is the "opts" used throughout hledger CLI code.

data CliOpts Source

Command line options. Used in the `hledger` package and above.

Constructors

CliOpts |

Fields

rawopts_ :: RawOpts
command_ :: String
file_ :: Maybe FilePath
rules_file_ :: Maybe FilePath
alias_ :: [String]
debug_ :: Int

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

no_new_accounts_ :: Bool
width_ :: Maybe String
reportopts_ :: ReportOpts

Instances

Data CliOpts |
Show CliOpts |
Typeable CliOpts |
Default CliOpts |

defcliopts :: CliOpts Source

# CLI option accessors

Some options require more processing. Possibly these should be merged into argsToCliOpts.

aliasesFromOpts :: CliOpts -> [(AccountName, AccountName)]Source

Get the account name aliases from options, if any.

formatFromOpts :: ReportOpts -> Either String [FormatString]Source

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

journalFilePathFromOpts :: CliOpts -> IO String Source

Get the (tilde-expanded, absolute) journal file path from 1. options, 2. an environment variable, or 3. the default.

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

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

data OutputWidth Source

Output width configuration (for register).

Constructors

TotalWidth Width | specify the overall width
FieldWidths [Width] | specify each field's width

Instances

Show OutputWidth |

data Width Source

A width value.

Constructors

Width Int | set width to exactly this number of characters
Auto | set width automatically from available space

Instances

Show Width |

defaultWidth :: Int Source

Default width of hledger console output.

defaultWidthWithFlag :: Int Source

Width of hledger console output when the -w flag is used with no value.

widthFromOpts :: CliOpts -> Either String OutputWidth Source

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

# utilities

getHledgerAddonCommands :: IO [String]Source

Get the unique suffixes (without hledger-) of hledger-* executables found in the current user's PATH, or the empty list if there is any problem.

argsToCliOpts :: [String] -> [String] -> IO CliOpts Source

Parse hledger CLI options from these command line arguments and add-on command names, or raise any error.

moveFlagsAfterCommand :: [String] -> [String]Source

A hacky workaround for cmdargs not accepting flags before the subcommand name: try to detect and move such flags after the command. This allows the user to put them in either position. The order of options is not preserved, but this should be ok.

Since we're not parsing flags as precisely as cmdargs here, this is imperfect. We make a decent effort to: - move all no-argument help and input flags - move all required-argument help and input flags along with their values, space-separated or not - not confuse things further or cause misleading errors.

decodeRawOpts :: [(t, SystemString)] -> [(t, String)]Source

Convert possibly encoded option values to regular unicode strings.

checkCliOpts :: CliOpts -> IO CliOpts Source

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

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.

optserror :: [Char] -> cSource

Raise an error, showing the specified message plus a hint about --help.

showModeHelp :: Mode a -> String Source

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

debugArgs :: [String] -> CliOpts -> IO ()Source

Print debug info about arguments and options if --debug is present.

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.

# tests

tests_Hledger_Cli_Options :: Test Source