From 4330427f6b1c4fcafe628fab9107218ba32febcb Mon Sep 17 00:00:00 2001 From: Simon Michael Date: Sat, 26 Dec 2020 15:59:03 -0800 Subject: [PATCH] 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. --- doc/common.m4 | 8 ++++---- hledger/Hledger/Cli/Main.hs | 12 +++++++----- hledger/hledger.m4.md | 3 +-- 3 files changed, 12 insertions(+), 11 deletions(-) diff --git a/doc/common.m4 b/doc/common.m4 index 527d25c74..28113c2a3 100644 --- a/doc/common.m4 +++ b/doc/common.m4 @@ -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) diff --git a/hledger/Hledger/Cli/Main.hs b/hledger/Hledger/Cli/Main.hs index 8658bafe1..711d52175 100644 --- a/hledger/Hledger/Cli/Main.hs +++ b/hledger/Hledger/Cli/Main.hs @@ -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) diff --git a/hledger/hledger.m4.md b/hledger/hledger.m4.md index 36a0a40c1..954856e27 100644 --- a/hledger/hledger.m4.md +++ b/hledger/hledger.m4.md @@ -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