{-|
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.
In most projects I almost always want:
- A header consisting of the program name and a short description
- A helper parser to provide -h, --help
- A version helper to respond to a --version option with shorter output than -h, --help
- Some additional usage info below the generated switch usage
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:
@
{- # 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
@
-}
module Options.Applicative.Dex
(
-- * Option parser functions
parseOpts'
, parseOptsWithWidth
-- * Re-exporting everything from optparse-applicative
, module Options.Applicative
)
where
import Data.Text.Lazy (Text, unpack)
import Data.Version (Version, showVersion)
import Options.Applicative
import Prettyprinter (pretty)
import System.Environment (getProgName)
versionHelper :: String -> Version -> Parser (a -> a)
versionHelper progName version =
infoOption (progName <> " " <> showVersion version) $ mconcat
[ long "version"
, help "Show version information"
, hidden
]
{-|
Main opts parsing entry point
This is named with a ' so the `parseOpts` identifier can be re-used by your
code. For example in an Opts module where you define your parser and other
usage content.
-}
parseOpts'
:: 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
parseOpts' = parseOptsWithWidth 100
{-|
Opts parsing with column limit as an argument
This library defaults to setting the columns to 100 instead of the
optparse-applicative default of 80 columns. I found 80 to be kind of
restrictive, 100 seems like a better default. Use this function to set a
custom width.
-}
parseOptsWithWidth
:: Int -- ^ Wrap column
-> 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
parseOptsWithWidth cols parser headerContent footerContent version = do
pn <- getProgName
customExecParser (prefs $ columns cols)
$ info (parser <**> helper <**> versionHelper pn version)
( header (pn <> " - " <> unpack headerContent)
<> (footerDoc . Just . pretty $ footerContent)
)