optparse-applicative-dex

Extra functions for working with optparse-applicative

Stackage Nightly 2026-11-30:1.0.2
Latest on Hackage:1.0.2

See all snapshots optparse-applicative-dex appears in

ISC licensed by Dino Morelli
Maintained by [email protected]
This version can be pinned in stack with:optparse-applicative-dex-1.0.2@sha256:84dcbb9267fbfc8ae2297cd340e655ecbee2bd9a65ae058a4c8b1c027d0ce410,1303

Module documentation for 1.0.2

Used by 1 package in nightly-2026-11-30(full list with versions):

optparse-applicative-dex

Synopsis

Extra functions for working with optparse-applicative

Description

optparse-applicative is a fantastic library

My usage of it has often used the same features wrapped in a parseOpts function along with a version helper.

This package abstracts that code into a re-usable library, minimizing pasting between projects. It’s a drop-in replacement for optparse-applicative.

In most CLI projects I almost always want some things beyond the basics:

  • A header consisting of the program name and a short description
  • A helper to provide -h, –help
  • A helper to provide –version, that outputs the name and version only
  • A default of 100 for wrapping usage instead of the default of 80
  • Some additional usage info below the generated switch usage
  • Less boring boilerplate to paste from other projects

The parseOpts’ and parseOptsWithWidth functions in this library take a parser and arguments for header, footer and the app version as generated by cabal and do all these things automatically.

A simple example using optparse-applicative-dex:

{-# LANGUAGE OverloadedStrings #-}

import Options.Applicative.Dex
import Paths_your_app (version)

data Options = Options { optA :: String , optB :: Bool }
  deriving Show

parser :: Parser Options
parser = Options
  <$> option str ( short 'a' <> metavar "STR" <> help "The a option, a string" )
  <*> switch ( short 'b' <> help "The b option, a switch" )

main :: IO ()
main = do
  opts <- parseOpts' parser "header-info" "footer-info" version
  print opts

The same behavior above with vanilla optparse-applicative

{-# LANGUAGE OverloadedStrings #-}

-- Note these library dependencies: optparse-applicative prettyprinter text

import Data.Text.Lazy (Text, unpack)
import Data.Version (Version, showVersion)
import Options.Applicative
import Paths_your_app (version)
import Prettyprinter (pretty)
import System.Environment (getProgName)

data Options = Options { optA :: String , optB :: Bool }
  deriving Show

parser :: Parser Options
parser = Options
  <$> option str ( short 'a' <> metavar "STR" <> help "The a option, a string" )
  <*> switch ( short 'b' <> help "The b option, a switch" )

versionHelper :: String -> Version -> Parser (a -> a)
versionHelper progName version' =
  infoOption (progName <> " " <> showVersion version') $ mconcat
  [ long "version"
  , help "Show version information"
  , hidden
  ]

myParseOpts
  :: Parser a   -- ^ CLI arguments parser made with optparse-applicative API
  -> Text       -- ^ One-line app description for the usage header
  -> Text       -- ^ Content for usage footer (after switches)
  -> Version    -- ^ Application version
  -> IO a
myParseOpts parser' headerContent footerContent version' = do
  pn <- getProgName
  customExecParser (prefs $ columns 100)
    $ info (parser' <**> helper <**> versionHelper pn version')
    (  header (pn <> " - " <> unpack headerContent)
    <> (footerDoc . Just . pretty $ footerContent)
    )

main :: IO ()
main = do
  opts <- myParseOpts parser "header-info" "footer-info" version
  print opts

Getting source

Source code is available from codeberg at the optparse-applicative-dex project page.

Handy command to generate Haddock docs during development if using stack. This constrains doc generation to modules in the library:

$ stack haddock --no-haddock-deps

Note this project has a generative AI/LLM policy

Contact

Dino Morelli [email protected]

Made by a human badge This work is hand-crafted with no LLM/AI involved

Changes

1.0.2 (2026-11-29)

  • Updated lots of documentation
  • Added genai policy documents
  • Fixed sample code formatting

1.0.1 (2025-09-05)

  • Fixed build dependency issue with Stackage nightly snapshot

1.0.0 (2025-09-05)

  • Initial release