Skip to content

Commit

Permalink
optionsExt: Extended version of 'options' with additional information…
Browse files Browse the repository at this point in the history
… in 'help'-flag (#374)
  • Loading branch information
michelk authored Feb 5, 2020
1 parent d148477 commit 51fb4bc
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 1 deletion.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
1.5.17

* Add `optionsExt`: Extended version of `options` with header, footer,
porgram-description and version information in `--help` flag

1.5.16

* Add `cptreeL`
Expand Down
40 changes: 40 additions & 0 deletions src/Turtle/Options.hs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ module Turtle.Options
, subcommand
, subcommandGroup
, options
, optionsExt

) where

Expand Down Expand Up @@ -107,6 +108,30 @@ options desc parser = liftIO
prefs = Opts.showHelpOnError
#endif

{-| Parse the given options from the command line and add additional information
Extended version of @options@ with program version header and footer information
-}
optionsExt :: MonadIO io => Header -> Footer -> Description -> Version -> Parser a -> io a
optionsExt header footer desc version parser = liftIO
$ Opts.customExecParser (Opts.prefs prefs)
$ Opts.info (Opts.helper <*> versionOption <*> parser)
(Opts.headerDoc (Just (getHeader header)) <>
Opts.footerDoc (Just (getFooter footer)) <>
Opts.progDescDoc (Just (getDescription desc)))
where
versionOption =
Opts.infoOption
(Text.unpack version)
(Opts.long "version" <> Opts.help "Show version")
prefs :: Opts.PrefsMod
#if MIN_VERSION_optparse_applicative(0,13,0)
prefs = Opts.showHelpOnError <> Opts.showHelpOnEmpty
#else
prefs = Opts.showHelpOnError
#endif


{-| The name of a command-line argument
This is used to infer the long name and metavariable for the command line
Expand Down Expand Up @@ -135,6 +160,21 @@ newtype CommandName = CommandName { getCommandName :: Text }
newtype Description = Description { getDescription :: Doc }
deriving (IsString)

{-| Header of the program
This description will appear in the header of the @--help@ output
-}
newtype Header = Header { getHeader :: Doc }
deriving (IsString)
{-| Footer of the program
This description will appear in the footer of the @--help@ output
-}
newtype Footer = Fotter { getFooter :: Doc }
deriving (IsString)

-- | Program Version
type Version = Text
{-| A helpful message explaining what a flag does
This will appear in the @--help@ output
Expand Down
2 changes: 1 addition & 1 deletion turtle.cabal
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
Name: turtle
Version: 1.5.16
Version: 1.5.17
Cabal-Version: >=1.10
Build-Type: Simple
License: BSD3
Expand Down

0 comments on commit 51fb4bc

Please sign in to comment.