| 1 |
{-| |
| 2 |
|
| 3 |
The help command. |
| 4 |
|
| 5 |
|-} |
| 6 |
--TODO rename manuals |
| 7 |
--TODO substring matching |
| 8 |
|
| 9 |
{-# LANGUAGE PackageImports #-} |
| 10 |
{-# LANGUAGE QuasiQuotes #-} |
| 11 |
{-# LANGUAGE TemplateHaskell #-} |
| 12 |
|
| 13 |
module Hledger.Cli.Commands.Help ( |
| 14 |
|
| 15 |
helpmode |
| 16 |
,help' |
| 17 |
|
| 18 |
) where |
| 19 |
|
| 20 |
import Data.Maybe |
| 21 |
import Safe (headMay) |
| 22 |
import System.Console.CmdArgs.Explicit |
| 23 |
import System.Environment |
| 24 |
import System.IO |
| 25 |
|
| 26 |
import Hledger.Cli.CliOptions |
| 27 |
import Hledger.Cli.DocFiles |
| 28 |
import Hledger.Data.RawOptions |
| 29 |
import Hledger.Data.Types |
| 30 |
import Hledger.Utils (embedFileRelative) |
| 31 |
--import Hledger.Utils.Debug |
| 32 |
|
| 33 |
helpmode = hledgerCommandMode |
| 34 |
$(embedFileRelative "Hledger/Cli/Commands/Help.txt") |
| 35 |
-- The help-* names avoid a clash with the --info and --man flags handled in Cli.hs. |
| 36 |
[flagNone ["i"] (setboolopt "help-i") "show the manual with info" |
| 37 |
,flagNone ["m"] (setboolopt "help-m") "show the manual with man" |
| 38 |
,flagNone ["p"] (setboolopt "help-p") "show the manual with $PAGER or less\n(less is always used if TOPIC is specified)" |
| 39 |
] |
| 40 |
[(helpflagstitle, helpflags)] |
| 41 |
[] |
| 42 |
([], Just $ argsFlag "[TOPIC]") |
| 43 |
|
| 44 |
-- | Display the hledger manual in various formats. |
| 45 |
-- You can select a docs viewer with one of the `--info`, `--man`, `--pager` flags. |
| 46 |
-- Otherwise it will use the first available of: info, man, $PAGER, less, stdout |
| 47 |
-- (and always stdout if output is non-interactive). |
| 48 |
help' :: CliOpts -> Journal -> IO () |
| 49 |
help' opts _ = do |
| 50 |
exes <- likelyExecutablesInPath |
| 51 |
pagerprog <- fromMaybe "less" <$> lookupEnv "PAGER" |
| 52 |
interactive <- hIsTerminalDevice stdout |
| 53 |
let |
| 54 |
args = take 1 $ listofstringopt "args" $ rawopts_ opts |
| 55 |
mtopic = headMay args |
| 56 |
[info, man, pager, cat] = |
| 57 |
[runInfoForTopic, runManForTopic, runPagerForTopic, printHelpForTopic] |
| 58 |
viewer |
| 59 |
| boolopt "help-i" $ rawopts_ opts = info |
| 60 |
| boolopt "help-m" $ rawopts_ opts = man |
| 61 |
| boolopt "help-p" $ rawopts_ opts = pager |
| 62 |
| not interactive = cat |
| 63 |
| "info" `elem` exes = info |
| 64 |
| "man" `elem` exes = man |
| 65 |
| pagerprog `elem` exes = pager |
| 66 |
| "less" `elem` exes = pager |
| 67 |
| otherwise = cat |
| 68 |
|
| 69 |
viewer "hledger" mtopic |