packages feed

optparse-applicative-dex-1.0.2: README.md

# 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](https://codeberg.org/dinofp/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](generative-ai-policy.md)


## Contact

Dino Morelli <dino@ui3.info>

[![Made by a human badge](doc/MadeByAHuman_01.png)](https://ko-fi.com/s/4662b19f61)
This work is hand-crafted with no LLM/AI involved