Skip to content

Commit

Permalink
Add basic version parser.
Browse files Browse the repository at this point in the history
Adds a simple expansion of infoOption both for documentation
and the basic use case.
  • Loading branch information
HuwCampbell authored Sep 13, 2022
2 parents 4d1e740 + 6cd1dda commit 9399fd0
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 6 deletions.
1 change: 1 addition & 0 deletions src/Options/Applicative.hs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ module Options.Applicative (
abortOption,
infoOption,
helper,
simpleVersioner,

-- ** Modifiers
--
Expand Down
14 changes: 14 additions & 0 deletions src/Options/Applicative/Extra.hs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ module Options.Applicative.Extra (
helper,
helperWith,
hsubparser,
simpleVersioner,
execParser,
customExecParser,
execParserPure,
Expand Down Expand Up @@ -93,6 +94,19 @@ hsubparser m = mkParser d g rdr
add_helper pinfo = pinfo
{ infoParser = infoParser pinfo <**> helper }

-- | A hidden \"--version\" option that displays the version.
--
-- > opts :: ParserInfo Sample
-- > opts = info (sample <**> simpleVersioner "v1.2.3") mempty
simpleVersioner :: String -- ^ Version string to be shown
-> Parser (a -> a)
simpleVersioner version = infoOption version $
mconcat
[ long "version"
, help "Show version information"
, hidden
]

-- | Run a program description.
--
-- Parse command line arguments. Display help text and exit if any parse error
Expand Down
6 changes: 1 addition & 5 deletions tests/Examples/Cabal.hs
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,6 @@ data BuildOpts = BuildOpts
{ buildDir :: FilePath }
deriving Show

version :: Parser (a -> a)
version = infoOption "0.0.0"
( long "version"
<> help "Print version information" )

parser :: Parser Args
parser = runA $ proc () -> do
Expand All @@ -60,7 +56,7 @@ parser = runA $ proc () -> do
<> command "build"
(info buildParser
(progDesc "Make this package ready for installation")) ) -< ()
A version >>> A helper -< Args opts cmds
A (simpleVersioner "0.0.0") >>> A helper -< Args opts cmds

commonOpts :: Parser CommonOpts
commonOpts = CommonOpts
Expand Down
2 changes: 1 addition & 1 deletion tests/cabal.err.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ Available options:

Global options:
-v,--verbose LEVEL Set verbosity to LEVEL
--version Print version information
--version Show version information

0 comments on commit 9399fd0

Please sign in to comment.