optparse-applicative quick start
Published on ; updated on
When I need to write a command-line program in Haskell, I invariably pick Paolo Capriotti’s optparse-applicative library.
Unfortunately, the minimal working example is complicated enough that I cannot reproduce it from memory, and the example in the README is very different from the style I prefer.
So I decided to put up a template here for a program using optparse-applicative. I am going to copy it into all of my future projects, and you are welcome to do so, too.
import Options.Applicative
import Control.Monad (join)
main :: IO ()
= join . customExecParser (prefs showHelpOnError) $
main <*> parser)
info (helper
( fullDesc<> header "General program title/description"
<> progDesc "What does this thing do?"
)where
parser :: Parser (IO ())
=
parser
work<$> strOption
"string_param"
( long <> short 's'
<> metavar "STRING"
<> help "string parameter"
)<*> option auto
"number_param"
( long <> short 'n'
<> metavar "NUMBER"
<> help "number parameter"
<> value 1
<> showDefault
)
work :: String -> Int -> IO ()
= return () work _ _