3

I'm trying to rewrite the automatically generated help output from optparse-applicative. Following code lets me replace the help output:

import Options.Applicative
import Data.Semigroup ((<>))

data Opts = Opts { optFast :: Bool, optHelp :: Bool }


sample :: Parser Opts
sample = Opts
  <$> switch
      ( long "fast"
     <> short 'f'
     <> help "Whether to be fast" )
  <*> switch
      ( long "help"
     <> short 'h'
     <> help $ info )


doSomething :: Opts -> IO ()
doSomething (Opts { optFast = True }) = putStrLn $ "Very fast execution!"
doSomething (Opts { optHelp = True }) = putStrLn $ "Some help for you!"
doSomething _ = putStrLn $ "Rather slow execution."


main :: IO ()
main =
  let
    opts = info (sample)
      ( fullDesc
     <> progDesc "Print execution speed"
     <> header "Hello - A test for optparse-applicative" )
  in
    doSomething =<< execParser opts

(Can be executed with stack script --resolver=lts-16 main.hs -- --help)

However, I don't want to replace it completely, but only replace certain parts of the automatically generated help text. How is this possible?

adius
  • 13,685
  • 7
  • 45
  • 46
  • Can you give an example of the kind of modification you want to make? You could do a few things: parameterise `sample` & `opts` to use different text for `help` / `progDesc` / &c.; or use [`style`](https://hackage.haskell.org/package/optparse-applicative-0.15.1.0/docs/Options-Applicative.html#v:style) to rewrite help text; or generate your own full help output by running the parser with `execParserPure` and rendering the [`ParserHelp`](https://hackage.haskell.org/package/optparse-applicative-0.15.1.0/docs/Options-Applicative.html#t:ParserHelp) yourself; or just change the `header` or `footer` – Jon Purdy Jun 15 '20 at 23:49

0 Answers0