Clean up existing examples, Remove redundant function

mainFromCmdParserWithHelpDesc is no longer required, because
peekCmdDesc can be used at the top-level to access the full
CmdDesc value. The knot-tying implementation is no longer
required either.
devtest
Lennart Spitzner 2020-09-20 14:18:53 +02:00
parent 91d57b07c4
commit 071eacccfc
4 changed files with 3 additions and 72 deletions

View File

@ -5,7 +5,9 @@ import UI.Butcher.Monadic
main :: IO ()
main = mainFromCmdParserWithHelpDesc $ \helpDesc -> do
main = mainFromCmdParser $ do
helpDesc <- peekCmdDesc
addCmdSynopsis "a simple butcher example program"
addCmdHelpStr "a very long help document"

View File

@ -38,9 +38,6 @@ parser = do
putStrLn $ "bar = " ++ show bar
putStrLn $ "x = " ++ show x
leftToMaybe :: Either a b -> Maybe a
leftToMaybe = either Just (const Nothing)
main :: IO ()
main = displayConsoleRegions $ do
withReg $ \reg1 -> withReg $ \reg2 -> withReg $ \reg3 -> do

View File

@ -62,31 +62,6 @@ mainFromCmdParser cmd = do
printErr $ ppUsage desc
(_desc, _remaining, Right out) -> out
-- | Same as mainFromCmdParser, but with one additional twist: You get access
-- to a knot-tied complete CommandDesc for this full command. Useful in
-- combination with 'UI.Butcher.Monadic.BuiltinCommands.addHelpCommand'
-- mainFromCmdParserWithHelpDesc
-- :: (CommandDesc -> CmdParser (IO ()) (IO ())) -> IO ()
-- mainFromCmdParserWithHelpDesc cmdF = do
-- progName <- System.Environment.getProgName
-- args <- System.Environment.getArgs
-- case runCmdParserWithHelpDesc (InputArgs args) cmdF of
-- (desc, Left err) -> do
-- putStrErrLn $ progName ++ ": error parsing arguments: " ++ head
-- (_pe_messages err)
-- putStrErrLn $ case _pe_remaining err of
-- InputString "" -> "at the end of input."
-- InputString str -> case show str of
-- s | length s < 42 -> "at: " ++ s ++ "."
-- s -> "at: " ++ take 40 s ++ "..\"."
-- InputArgs [] -> "at the end of input"
-- InputArgs xs -> case List.unwords $ show <$> xs of
-- s | length s < 42 -> "at: " ++ s ++ "."
-- s -> "at: " ++ take 40 s ++ "..\"."
-- putStrErrLn $ "usage:"
-- printErr $ ppUsage desc
-- (_desc, Right out) -> out
putStrErrLn :: String -> IO ()
putStrErrLn s = hPutStrLn stderr s

View File

@ -1,7 +1,6 @@
-- | Turn your CmdParser into an IO () to be used as your program @main@.
module UI.Butcher.Monadic.IO
( mainFromCmdParser
, mainFromCmdParserWithHelpDesc
)
where
@ -73,48 +72,6 @@ mainFromCmdParser cmd = do
printErr $ ppUsage desc
Just a -> a
-- | Same as mainFromCmdParser, but with one additional twist: You get access
-- to a knot-tied complete CommandDesc for this full command. Useful in
-- combination with 'UI.Butcher.Monadic.BuiltinCommands.addHelpCommand'
mainFromCmdParserWithHelpDesc
:: (CommandDesc -> CmdParser Identity (IO ()) ()) -> IO ()
mainFromCmdParserWithHelpDesc cmdF = do
progName <- System.Environment.getProgName
let (checkResult, optimisticFullDesc) =
( toCmdDesc (Just progName) (cmdF optimisticFullDesc)
, either (const emptyCommandDesc) id $ checkResult
)
-- knot-tying at its finest..
case checkResult of
Left e -> do
putStrErrLn
$ progName
++ ": internal error: failed sanity check for butcher main command parser!"
putStrErrLn $ "(" ++ e ++ ")"
putStrErrLn $ "aborting."
Right fullDesc -> do
args <- System.Environment.getArgs
case runCmdParserCoreFromDesc fullDesc (InputArgs args) (cmdF fullDesc) of
(desc, _, Left err) -> do
putStrErrLn $ progName ++ ": error parsing arguments: " ++ head
(_pe_messages err)
putStrErrLn $ case _pe_remaining err of
InputString "" -> "at the end of input."
InputString str -> case show str of
s | length s < 42 -> "at: " ++ s ++ "."
s -> "at: " ++ take 40 s ++ "..\"."
InputArgs [] -> "at the end of input"
InputArgs xs -> case List.unwords $ show <$> xs of
s | length s < 42 -> "at: " ++ s ++ "."
s -> "at: " ++ take 40 s ++ "..\"."
putStrErrLn $ "usage:"
printErr $ ppUsage desc
(desc, _, Right out) -> case out of
Nothing -> do
putStrErrLn $ "usage:"
printErr $ ppUsage desc
Just a -> a
putStrErrLn :: String -> IO ()
putStrErrLn s = hPutStrLn stderr s