Add addHelpCommandWith; Improve layouting for help/docs

pull/5/head
Lennart Spitzner 2018-01-12 23:16:20 +01:00
parent 9337ab8d40
commit a495e13e53
5 changed files with 35 additions and 26 deletions

View File

@ -29,6 +29,7 @@ module UI.Butcher.Monadic
-- * Builtin commands -- * Builtin commands
, addHelpCommand , addHelpCommand
, addHelpCommand2 , addHelpCommand2
, addHelpCommandWith
, addButcherDebugCommand , addButcherDebugCommand
, addShellCompletionCommand , addShellCompletionCommand
, addShellCompletionCommand' , addShellCompletionCommand'

View File

@ -2,6 +2,7 @@
module UI.Butcher.Monadic.BuiltinCommands module UI.Butcher.Monadic.BuiltinCommands
( addHelpCommand ( addHelpCommand
, addHelpCommand2 , addHelpCommand2
, addHelpCommandWith
, addHelpCommandShallow , addHelpCommandShallow
, addButcherDebugCommand , addButcherDebugCommand
, addShellCompletionCommand , addShellCompletionCommand
@ -33,22 +34,12 @@ import System.IO
-- | Adds a proper full help command. To obtain the 'CommandDesc' value, see -- | Adds a proper full help command. To obtain the 'CommandDesc' value, see
-- 'UI.Butcher.Monadic.cmdRunParserWithHelpDesc' or -- 'UI.Butcher.Monadic.cmdRunParserWithHelpDesc' or
-- 'UI.Butcher.Monadic.IO.mainFromCmdParserWithHelpDesc'. -- 'UI.Butcher.Monadic.IO.mainFromCmdParserWithHelpDesc'.
--
-- > addHelpCommand = addHelpCommandWith
-- > (pure . PP.renderStyle PP.style { PP.ribbonsPerLine = 1.0 } . ppHelpShallow)
addHelpCommand :: Applicative f => CommandDesc a -> CmdParser f (IO ()) () addHelpCommand :: Applicative f => CommandDesc a -> CmdParser f (IO ()) ()
addHelpCommand desc = addCmd "help" $ do addHelpCommand = addHelpCommandWith
addCmdSynopsis "print help about this command" (pure . PP.renderStyle PP.style { PP.ribbonsPerLine = 1.0 } . ppHelpShallow)
rest <- addParamRestOfInput "SUBCOMMAND(s)" mempty
addCmdImpl $ do
let restWords = List.words rest
let
descent :: [String] -> CommandDesc a -> CommandDesc a
descent [] curDesc = curDesc
descent (w:wr) curDesc =
case
List.lookup (Just w) $ Data.Foldable.toList $ _cmd_children curDesc
of
Nothing -> curDesc
Just child -> descent wr child
print $ ppHelpShallow $ descent restWords desc
-- | Adds a proper full help command. In contrast to 'addHelpCommand', -- | Adds a proper full help command. In contrast to 'addHelpCommand',
-- this version is a bit more verbose about available subcommands as it -- this version is a bit more verbose about available subcommands as it
@ -57,8 +48,21 @@ addHelpCommand desc = addCmd "help" $ do
-- To obtain the 'CommandDesc' value, see -- To obtain the 'CommandDesc' value, see
-- 'UI.Butcher.Monadic.cmdRunParserWithHelpDesc' or -- 'UI.Butcher.Monadic.cmdRunParserWithHelpDesc' or
-- 'UI.Butcher.Monadic.IO.mainFromCmdParserWithHelpDesc'. -- 'UI.Butcher.Monadic.IO.mainFromCmdParserWithHelpDesc'.
--
-- > addHelpCommand2 = addHelpCommandWith
-- > (pure . PP.renderStyle PP.style { PP.ribbonsPerLine = 1.0 } . ppHelpDepthOne)
addHelpCommand2 :: Applicative f => CommandDesc a -> CmdParser f (IO ()) () addHelpCommand2 :: Applicative f => CommandDesc a -> CmdParser f (IO ()) ()
addHelpCommand2 desc = addCmd "help" $ do addHelpCommand2 = addHelpCommandWith
(pure . PP.renderStyle PP.style { PP.ribbonsPerLine = 1.0 } . ppHelpDepthOne)
-- | Adds a proper full help command, using the specified function to turn
-- the relevant subcommand's 'CommandDesc' into a String.
addHelpCommandWith
:: Applicative f
=> (CommandDesc a -> IO String)
-> CommandDesc a
-> CmdParser f (IO ()) ()
addHelpCommandWith f desc = addCmd "help" $ do
addCmdSynopsis "print help about this command" addCmdSynopsis "print help about this command"
rest <- addParamRestOfInput "SUBCOMMAND(s)" mempty rest <- addParamRestOfInput "SUBCOMMAND(s)" mempty
addCmdImpl $ do addCmdImpl $ do
@ -72,7 +76,8 @@ addHelpCommand2 desc = addCmd "help" $ do
of of
Nothing -> curDesc Nothing -> curDesc
Just child -> descent wr child Just child -> descent wr child
print $ ppHelpDepthOne $ descent restWords desc s <- f $ descent restWords desc
putStrLn s
-- | Adds a help command that prints help for the command currently in context. -- | Adds a help command that prints help for the command currently in context.
-- --

View File

@ -95,7 +95,8 @@ flagHelp h = mempty { _flag_help = Just h }
-- | Create a 'Flag' with just a help text. -- | Create a 'Flag' with just a help text.
flagHelpStr :: String -> Flag p flagHelpStr :: String -> Flag p
flagHelpStr s = mempty { _flag_help = Just $ PP.text s } flagHelpStr s =
mempty { _flag_help = Just $ PP.fsep $ fmap PP.text $ List.words s }
-- | Create a 'Flag' with just a default value. -- | Create a 'Flag' with just a default value.
flagDefault :: p -> Flag p flagDefault :: p -> Flag p

View File

@ -349,7 +349,8 @@ checkCmdParser mTopLevel cmdParser =
processMain next processMain next
Free (CmdParserSynopsis s next) -> do Free (CmdParserSynopsis s next) -> do
cmd :: CommandDesc out <- mGet cmd :: CommandDesc out <- mGet
mSet $ cmd { _cmd_synopsis = Just $ PP.text s } mSet
$ cmd { _cmd_synopsis = Just $ PP.fsep $ fmap PP.text $ List.words s }
processMain next processMain next
Free (CmdParserPeekDesc nextF) -> do Free (CmdParserPeekDesc nextF) -> do
processMain $ nextF monadMisuseError processMain $ nextF monadMisuseError
@ -546,7 +547,8 @@ runCmdParserAExt mTopLevel inputInitial cmdParser =
processMain next processMain next
Free (CmdParserSynopsis s next) -> do Free (CmdParserSynopsis s next) -> do
cmd :: CommandDesc out <- mGet cmd :: CommandDesc out <- mGet
mSet $ cmd { _cmd_synopsis = Just $ PP.text s } mSet
$ cmd { _cmd_synopsis = Just $ PP.fsep $ fmap PP.text $ List.words s }
processMain next processMain next
Free (CmdParserPeekDesc nextF) -> do Free (CmdParserPeekDesc nextF) -> do
parser <- mGet parser <- mGet
@ -1017,7 +1019,8 @@ runCmdParserAExt mTopLevel inputInitial cmdParser =
next next
CmdParserSynopsis s next -> do CmdParserSynopsis s next -> do
cmd :: CommandDesc out <- mGet cmd :: CommandDesc out <- mGet
mSet $ cmd { _cmd_synopsis = Just $ PP.text s } mSet
$ cmd { _cmd_synopsis = Just $ PP.fsep $ fmap PP.text $ List.words s }
next next
CmdParserPeekDesc nextF -> do CmdParserPeekDesc nextF -> do
mGet >>= nextF . fmap (\(_ :: out) -> ()) mGet >>= nextF . fmap (\(_ :: out) -> ())

View File

@ -320,11 +320,10 @@ ppPartDescUsage = \case
PartSeq ps -> [ PP.fsep ds | let ds = Maybe.mapMaybe rec ps, not (null ds) ] PartSeq ps -> [ PP.fsep ds | let ds = Maybe.mapMaybe rec ps, not (null ds) ]
PartDefault _ p -> PP.brackets <$> rec p PartDefault _ p -> PP.brackets <$> rec p
PartSuggestion sgs p -> rec p <&> \d -> PartSuggestion sgs p -> rec p <&> \d ->
PP.parens case [ PP.text s | CompletionString s <- sgs ] of
$ PP.fcat [] -> d
$ PP.punctuate (PP.text "|") sgsDocs ->
$ [ PP.text s | CompletionString s <- sgs ] PP.parens $ PP.fcat $ PP.punctuate (PP.text "|") $ sgsDocs ++ [d]
++ [d]
PartRedirect s _ -> Just $ PP.text s PartRedirect s _ -> Just $ PP.text s
PartMany p -> rec p <&> (<> PP.text "+") PartMany p -> rec p <&> (<> PP.text "+")
PartWithHelp _ p -> rec p PartWithHelp _ p -> rec p