cli: make --help/--version always CMD-specific

This simplifies the --help/-h and --version flags by making them
position independent: no matter where they appear in the command line,
if there is a command argument they will show the command's help or
version (where applicable), instead of the hledger executable's.
This commit is contained in:
Simon Michael 2020-12-26 15:59:03 -08:00
parent 12447ffab7
commit 4330427f6b
3 changed files with 12 additions and 11 deletions

View File

@ -55,16 +55,16 @@ m4_dnl Should be kept synced with CliOptions.hs etc.
m4_define({{_helpoptions_}}, {{
`-h --help`
: show general help (or after COMMAND, command help)
: show general or COMMAND help
`--man`
: show user manual with man
: show general or COMMAND user manual with man
`--info`
: show user manual with info
: show general or COMMAND user manual with info
`--version`
: show version
: show general or ADDONCOMMAND version
`--debug[=N]`
: show debug output (levels 1-9, default: 1)

View File

@ -164,12 +164,14 @@ main = do
journallesserror = error $ cmd++" tried to read the journal but is not supposed to"
runHledgerCommand
-- high priority flags and situations. -h, then --help, then --info are highest priority.
| hasHelpFlag argsbeforecmd = dbgIO "" "-h/--help before command, showing general usage" >> printUsage
| hasInfoFlag argsbeforecmd = dbgIO "" "--info before command, showing general info manual" >> runInfoForTopic "hledger" Nothing
| hasManFlag argsbeforecmd = dbgIO "" "--man before command, showing general man page" >> runManForTopic "hledger" Nothing
| not (hasHelpFlag argsaftercmd || hasInfoFlag argsaftercmd || hasManFlag argsaftercmd) && (hasVersion argsbeforecmd || (hasVersion argsaftercmd && isInternalCommand))
| isNullCommand && hasHelpFlag args = dbgIO "" "-h/--help with no command, showing general help" >> printUsage
| isNullCommand && hasInfoFlag args = dbgIO "" "--info with no command, showing general info manual" >> runInfoForTopic "hledger" Nothing
| isNullCommand && hasManFlag args = dbgIO "" "--man with no command, showing general man page" >> runManForTopic "hledger" Nothing
| not (isExternalCommand || hasHelpFlag args || hasInfoFlag args || hasManFlag args)
&& (hasVersion args) -- || (hasVersion argsaftercmd && isInternalCommand))
= putStrLn prognameandversion
| not (hasHelpFlag argsaftercmd || hasInfoFlag argsaftercmd || hasManFlag argsaftercmd) && (hasDetailedVersion argsbeforecmd || (hasDetailedVersion argsaftercmd && isInternalCommand))
| not (isExternalCommand || hasHelpFlag args || hasInfoFlag args || hasManFlag args)
&& (hasDetailedVersion argsbeforecmd) -- || (hasDetailedVersion argsaftercmd && isInternalCommand))
= putStrLn prognameanddetailedversion
-- \| (null externalcmd) && "binary-filename" `inRawOpts` rawopts = putStrLn $ binaryfilename progname
-- \| "--browse-args" `elem` args = System.Console.CmdArgs.Helper.execute "cmdargs-browser" mainmode' args >>= (putStr . show)

View File

@ -1357,8 +1357,7 @@ $ hledger web --serve
```
(because the `--serve` flag belongs to `hledger-web`, not `hledger`).
The `-h/--help` and `--version` flags work without `--`, with their position deciding which program they refer to.
Eg `hledger -h web` shows hledger's help, `hledger web -h` shows hledger-web's help.
The `-h/--help` and `--version` flags don't require `--`.
If you have any trouble with this, remember you can always run the add-on program directly, eg:
```shell