2013-09-22 12:17:41 +04:00
|
|
|
# hledger command line processing
|
|
|
|
#
|
|
|
|
# Quick guide to terms used here:
|
|
|
|
#
|
2014-04-28 10:33:13 +04:00
|
|
|
# - flag: a synonym for option. Or, just the first part of an option,
|
|
|
|
# which can be either a short flag (hyphen followed by a letter) or
|
|
|
|
# a long flag (double hyphen followed by a word).
|
2013-09-22 12:17:41 +04:00
|
|
|
#
|
|
|
|
# - option: a command modifier. An option consists of a short flag, a
|
|
|
|
# long flag, or both, and possibly an optional or required value.
|
|
|
|
# Each option has some effect on program execution, and is described
|
|
|
|
# in the command line help.
|
|
|
|
#
|
2014-04-28 10:33:13 +04:00
|
|
|
# - raw arguments: everything following the program name on the
|
|
|
|
# command line, ie what is returned by getArgs.
|
2013-09-22 12:17:41 +04:00
|
|
|
#
|
2014-04-28 10:33:13 +04:00
|
|
|
# - parsed arguments: all raw arguments that are not options.
|
|
|
|
#
|
|
|
|
# - command arguments: all parsed arguments after the first, which is
|
|
|
|
# the command name.
|
|
|
|
#
|
|
|
|
# - RawOpts: the command name, options, and arguments parsed by cmdargs,
|
2018-10-05 23:04:12 +03:00
|
|
|
# as an association list of strings. Eg:
|
2013-09-22 12:17:41 +04:00
|
|
|
# [("command","register"),("args","a"),("debug",""),("help","")]
|
|
|
|
#
|
2014-04-28 10:33:13 +04:00
|
|
|
# - CliOpts: a RawOpts, plus the same information with some additional
|
|
|
|
# cleanup in a more convenient data structure. Used throughout the
|
|
|
|
# hledger CLI code.
|
|
|
|
#
|
|
|
|
# - command line, shell command: what you type in the shell or
|
|
|
|
# terminal window to start a program.
|
2013-09-22 12:17:41 +04:00
|
|
|
#
|
2014-04-28 10:33:13 +04:00
|
|
|
# - hledger command, subcommand: one of hledger's modes of operation,
|
|
|
|
# named and selected by the first parsed argument. There are two kinds:
|
|
|
|
# - internal or built-in commands are part of the main hledger executable.
|
|
|
|
# - external or add-on commands are provided by hledger-* executables in
|
|
|
|
# the PATH.
|
2013-09-22 12:17:41 +04:00
|
|
|
#
|
|
|
|
# Description of existing/expected behaviour as of 2013/9/16:
|
|
|
|
#
|
|
|
|
# - general usage is hledger [COMMAND] [OPTIONS] [ARGS]
|
|
|
|
#
|
|
|
|
# - commands are internal (built in to the main hledger executable) or external (any hledger-* executables found in the PATH)
|
|
|
|
# - some internal commands have aliases, which are displayed in the general help
|
|
|
|
# - there are also a few hidden internal commands
|
2014-04-28 10:33:13 +04:00
|
|
|
# - COMMAND is an exact command (balance), an alias (bal), or any unique command prefix (inc)
|
|
|
|
# - when COMMAND is a non-unique prefix, all matched commands will be listed, including hidden ones (eg hledger c)
|
2013-09-22 12:17:41 +04:00
|
|
|
# - an unrecognised command shows an error and gives non-zero exit status
|
|
|
|
#
|
|
|
|
# - usually the command must come first, followed by options and arguments in any order
|
|
|
|
# - a few options may also go before the command: -f, --rules-file, --alias, --help, --version, --debug.
|
|
|
|
# - option flags may be written in full or as a unique prefix, eg --rules for --rules-file
|
|
|
|
# - if the command is external, options and arguments after the command are handled by that executable, not hledger
|
|
|
|
#
|
|
|
|
# - the --help flag has highest priority
|
2014-04-28 10:33:13 +04:00
|
|
|
# - --help before the command (or no command) shows general help, including the commands list
|
2013-09-22 12:17:41 +04:00
|
|
|
# - --help after an internal command shows command-specific help, including command and general flags
|
2014-04-28 10:33:13 +04:00
|
|
|
# - there is no internal "help" command
|
2013-09-22 12:17:41 +04:00
|
|
|
|
|
|
|
# version
|
|
|
|
|
|
|
|
# 1. --version shows version
|
2015-07-12 21:54:08 +03:00
|
|
|
hledger --version
|
2013-09-22 12:17:41 +04:00
|
|
|
>>> /^hledger [0-9]/
|
2017-03-29 16:39:30 +03:00
|
|
|
>>>2
|
2013-09-22 12:17:41 +04:00
|
|
|
>>>=0
|
|
|
|
|
|
|
|
# 2. --version also works after a command, if it's internal
|
2015-07-12 21:54:08 +03:00
|
|
|
hledger balance --version
|
2013-09-22 12:17:41 +04:00
|
|
|
>>> /^hledger [0-9]/
|
2017-03-29 16:39:30 +03:00
|
|
|
>>>2
|
2013-09-22 12:17:41 +04:00
|
|
|
>>>=0
|
|
|
|
|
|
|
|
# help
|
|
|
|
|
2017-03-29 04:39:35 +03:00
|
|
|
# 3. with no command, show commands list
|
2015-07-12 21:54:08 +03:00
|
|
|
hledger
|
2019-01-29 07:25:59 +03:00
|
|
|
>>> /^Commands/
|
2017-03-29 16:39:30 +03:00
|
|
|
>>>2
|
2013-09-22 12:17:41 +04:00
|
|
|
>>>=0
|
|
|
|
|
|
|
|
# 4. no-command help still works if there are flags, at least the common ones
|
2015-07-12 21:54:08 +03:00
|
|
|
hledger -fsomefile
|
2019-01-29 07:25:59 +03:00
|
|
|
>>> /^Commands/
|
2017-03-29 16:39:30 +03:00
|
|
|
>>>2
|
2013-09-22 12:17:41 +04:00
|
|
|
>>>=0
|
|
|
|
|
|
|
|
# 5. and also with a space between flag and value
|
2015-07-12 21:54:08 +03:00
|
|
|
hledger -f somefile
|
2019-01-29 07:25:59 +03:00
|
|
|
>>> /^Commands/
|
2017-03-29 16:39:30 +03:00
|
|
|
>>>2
|
2013-09-22 12:17:41 +04:00
|
|
|
>>>=0
|
|
|
|
|
2016-04-19 20:44:43 +03:00
|
|
|
# 6. with -h, and possibly other common flags present, show general usage
|
|
|
|
hledger -h --version -f /dev/null
|
2017-03-29 04:39:35 +03:00
|
|
|
>>> /^hledger \[CMD\]/
|
2017-03-29 16:39:30 +03:00
|
|
|
>>>2
|
2013-09-22 12:17:41 +04:00
|
|
|
>>>=0
|
|
|
|
|
2016-04-19 20:44:43 +03:00
|
|
|
# 7. with -h before COMMAND, show general usage
|
|
|
|
hledger -h balance --cost
|
2017-03-29 04:39:35 +03:00
|
|
|
>>> /^hledger \[CMD\]/
|
2017-03-29 16:39:30 +03:00
|
|
|
>>>2
|
2013-09-22 12:17:41 +04:00
|
|
|
>>>=0
|
|
|
|
|
2016-04-19 20:44:43 +03:00
|
|
|
# 8. with -h after command, show command usage
|
|
|
|
hledger balance -h
|
2017-03-29 04:39:35 +03:00
|
|
|
>>> /balance \[OPTIONS\]/
|
2017-03-29 16:39:30 +03:00
|
|
|
>>>2
|
2013-09-22 12:17:41 +04:00
|
|
|
>>>=0
|
|
|
|
|
2017-03-29 04:39:35 +03:00
|
|
|
# 9. with an unrecognised command, give an error and non-zero exit status
|
2015-07-12 21:54:08 +03:00
|
|
|
hledger nosuchcommand
|
2013-09-22 12:17:41 +04:00
|
|
|
>>>
|
2017-03-29 04:39:35 +03:00
|
|
|
>>>2 /not recognized.*to see a list/
|
2013-09-22 12:17:41 +04:00
|
|
|
>>>=1
|
|
|
|
|
|
|
|
# flag positions
|
|
|
|
|
2017-03-29 04:39:35 +03:00
|
|
|
# 10. general flags can go before command
|
|
|
|
hledger -f /dev/null --alias somealiases --rules-file -h --help --version --debug 1 --daily register
|
|
|
|
>>> /^hledger \[CMD\]/
|
2017-03-29 16:39:30 +03:00
|
|
|
>>>2
|
2013-09-22 12:17:41 +04:00
|
|
|
>>>=0
|
|
|
|
|
2017-03-29 04:39:35 +03:00
|
|
|
# 11. or after it, and spaces in options are optional
|
2016-04-19 20:44:43 +03:00
|
|
|
hledger register -f/dev/null --alias=somealiases --rules-file -h --version --debug 1 --daily
|
2013-09-22 12:17:41 +04:00
|
|
|
>>> /^register \[OPTIONS\]/
|
2017-03-29 16:39:30 +03:00
|
|
|
>>>2
|
2013-09-22 12:17:41 +04:00
|
|
|
>>>=0
|
|
|
|
|
2017-03-29 04:39:35 +03:00
|
|
|
# 12. general flags before command should work
|
|
|
|
hledger -f /dev/null --daily register
|
|
|
|
>>>
|
2017-03-29 16:39:30 +03:00
|
|
|
>>>2
|
2017-03-29 04:39:35 +03:00
|
|
|
>>>=0
|
|
|
|
|
|
|
|
# 13. command-specific flags can go after command
|
|
|
|
hledger -f /dev/null register --daily
|
|
|
|
>>>
|
2017-03-29 16:39:30 +03:00
|
|
|
>>>2
|
2017-03-29 04:39:35 +03:00
|
|
|
>>>=0
|
|
|
|
|
|
|
|
# 14. but not before it
|
|
|
|
hledger --related register
|
|
|
|
>>>
|
|
|
|
>>>2 /Unknown flag: --related/
|
|
|
|
>>>=1
|
|
|
|
|