From 54af71a0b14ee8c394b8333aef56dbb54295cbe2 Mon Sep 17 00:00:00 2001 From: Simon Michael Date: Fri, 25 Dec 2020 21:12:22 -0800 Subject: [PATCH] cli: general --man, --info flags show the manual --- doc/common.m4 | 8 +++++++- hledger/Hledger/Cli/CliOptions.hs | 9 +++++++-- hledger/Hledger/Cli/Main.hs | 14 +++++++++++--- 3 files changed, 25 insertions(+), 6 deletions(-) diff --git a/doc/common.m4 b/doc/common.m4 index bbdf57cdf..527d25c74 100644 --- a/doc/common.m4 +++ b/doc/common.m4 @@ -55,7 +55,13 @@ m4_dnl Should be kept synced with CliOptions.hs etc. m4_define({{_helpoptions_}}, {{ `-h --help` -: show general usage (or after COMMAND, command usage) +: show general help (or after COMMAND, command help) + +`--man` +: show user manual with man + +`--info` +: show user manual with info `--version` : show version diff --git a/hledger/Hledger/Cli/CliOptions.hs b/hledger/Hledger/Cli/CliOptions.hs index 092597105..ef8031b97 100644 --- a/hledger/Hledger/Cli/CliOptions.hs +++ b/hledger/Hledger/Cli/CliOptions.hs @@ -106,7 +106,10 @@ import Hledger.Cli.Version -- | Common help flags: --help, --debug, --version... helpflags :: [Flag RawOpts] helpflags = [ - flagNone ["help","h"] (setboolopt "help") "show general usage (or after CMD, command usage)" + -- XXX why are these duplicated in defCommandMode below ? + flagNone ["help","h"] (setboolopt "help") "show general help (or after CMD, command help)" + ,flagNone ["man"] (setboolopt "man") "Show user manual with man" + ,flagNone ["info"] (setboolopt "info") "Show info manual with info" -- ,flagNone ["browse-args"] (setboolopt "browse-args") "use a web UI to select options and build up a command line" ,flagReq ["debug"] (\s opts -> Right $ setopt "debug" s opts) "[N]" "show debug output (levels 1-9, default: 1)" ,flagNone ["version"] (setboolopt "version") "show version information" @@ -273,8 +276,10 @@ defCommandMode names = defMode { ,modeGroupFlags = Group { groupNamed = [] ,groupUnnamed = [ - flagNone ["help"] (setboolopt "help") "Show usage." + flagNone ["help"] (setboolopt "help") "Show command-line help" -- ,flagNone ["help"] (setboolopt "help") "Show long help." + ,flagNone ["man"] (setboolopt "man") "Show user manual with man" + ,flagNone ["info"] (setboolopt "info") "Show info manual with info" ] ,groupHidden = [] -- flags not displayed in the usage } diff --git a/hledger/Hledger/Cli/Main.hs b/hledger/Hledger/Cli/Main.hs index f7c58d92f..c6e3a7d78 100644 --- a/hledger/Hledger/Cli/Main.hs +++ b/hledger/Hledger/Cli/Main.hs @@ -142,9 +142,15 @@ main = do printUsage = putStr $ showModeUsage $ mainmode addons badCommandError = error' ("command "++rawcmd++" is not recognized, run with no command to see a list") >> exitFailure -- PARTIAL: hasHelpFlag args = any (`elem` args) ["-h","--help"] + hasManFlag args = any (`elem` args) ["--man"] + hasInfoFlag args = any (`elem` args) ["--info"] f `orShowHelp` mode | hasHelpFlag args = putStr $ showModeUsage mode + | hasInfoFlag args = runInfoForTopic "hledger" + | hasManFlag args = runManForTopic "hledger" | otherwise = f + -- where + -- lastdocflag dbgIO "processed opts" opts dbgIO "command matched" cmd dbgIO "isNullCommand" isNullCommand @@ -158,10 +164,12 @@ 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 before command, showing general usage" >> printUsage - | not (hasHelpFlag argsaftercmd) && (hasVersion argsbeforecmd || (hasVersion argsaftercmd && isInternalCommand)) + | hasHelpFlag argsbeforecmd = dbgIO "" "-h/--help before command, showing general usage" >> printUsage + | hasInfoFlag argsbeforecmd = dbgIO "" "--info before command, showing general info manual" >> runInfoForTopic "hledger" + | hasManFlag argsbeforecmd = dbgIO "" "--man before command, showing general man page" >> runManForTopic "hledger" + | not (hasHelpFlag argsaftercmd || hasInfoFlag argsaftercmd || hasManFlag argsaftercmd) && (hasVersion argsbeforecmd || (hasVersion argsaftercmd && isInternalCommand)) = putStrLn prognameandversion - | not (hasHelpFlag argsaftercmd) && (hasDetailedVersion argsbeforecmd || (hasDetailedVersion argsaftercmd && isInternalCommand)) + | not (hasHelpFlag argsaftercmd || hasInfoFlag argsaftercmd || hasManFlag argsaftercmd) && (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)