Add Option to format long options with equals sign

This commit is contained in:
Ben Selfridge 2019-03-15 15:48:12 -07:00 committed by Huw Campbell
parent 83e096aa20
commit bc29e669a5
10 changed files with 50 additions and 5 deletions

2
.gitignore vendored
View File

@ -5,3 +5,5 @@
cabal.project.local
.cabal-sandbox/
cabal.sandbox.config
.ghc.environment.*

View File

@ -42,7 +42,7 @@ extra-source-files: CHANGELOG.md
homepage: https://github.com/pcapriotti/optparse-applicative
bug-reports: https://github.com/pcapriotti/optparse-applicative/issues
tested-with:
tested-with:
GHC==7.0.4,
GHC==7.2.2,
GHC==7.4.2,

View File

@ -195,6 +195,7 @@ module Options.Applicative (
noBacktrack,
subparserInline,
columns,
helpLongEquals,
defaultPrefs,
-- * Completions

View File

@ -85,6 +85,7 @@ module Options.Applicative.Builder (
noBacktrack,
subparserInline,
columns,
helpLongEquals,
prefs,
defaultPrefs,
@ -500,6 +501,11 @@ subparserInline = PrefsMod $ \p -> p { prefBacktrack = SubparserInline }
columns :: Int -> PrefsMod
columns cols = PrefsMod $ \p -> p { prefColumns = cols }
-- | Show equals sign, rather than space, in usage and help text for options with
-- long names.
helpLongEquals :: PrefsMod
helpLongEquals = PrefsMod $ \p -> p { prefHelpLongEquals = True }
-- | Create a `ParserPrefs` given a modifier
prefs :: PrefsMod -> ParserPrefs
prefs m = applyPrefsMod m base
@ -510,7 +516,8 @@ prefs m = applyPrefsMod m base
, prefShowHelpOnError = False
, prefShowHelpOnEmpty = False
, prefBacktrack = Backtrack
, prefColumns = 80 }
, prefColumns = 80
, prefHelpLongEquals = False }
-- Convenience shortcuts

View File

@ -22,6 +22,7 @@ module Options.Applicative.Common (
Parser,
liftOpt,
showOption,
showOptionEquals,
-- * Program descriptions
--
@ -65,6 +66,12 @@ showOption :: OptName -> String
showOption (OptLong n) = "--" ++ n
showOption (OptShort n) = '-' : [n]
-- | Like 'showOption', but puts an equals sign or a space after long options if the
-- 'ParserPrefs' indicate we should do so.
showOptionEquals :: ParserPrefs -> OptName -> String
showOptionEquals prefs (OptLong n) = "--" ++ n ++ (if prefHelpLongEquals prefs then "=" else "")
showOptionEquals _ (OptShort n) = '-' : [n]
optionNames :: OptReader a -> [OptName]
optionNames (OptReader names _ _) = names
optionNames (FlagReader names _) = names

View File

@ -258,7 +258,7 @@ parserFailure pprefs pinfo msg ctx = ParserFailure $ \progn ->
-- reader also ensure that it can be immediately
-- reachable from where the error was given.
opt_completions hinfo opt = case optMain opt of
OptReader ns _ _ -> fmap showOption ns
OptReader ns _ _ -> fmap (showOptionEquals pprefs) ns
FlagReader ns _ -> fmap showOption ns
ArgReader _ -> []
CmdReader _ ns _ | hinfoUnreachableArgs hinfo

View File

@ -37,8 +37,16 @@ optDesc :: ParserPrefs -> OptDescStyle -> OptHelpInfo -> Option a -> (Chunk Doc,
optDesc pprefs style info opt =
let ns = optionNames $ optMain opt
mv = stringChunk $ optMetaVar opt
descs = map (string . showOption) (sort ns)
desc = listToChunk (intersperse (descSep style) descs) <<+>> mv
has_arg = case mv of
Chunk Nothing -> True
_ -> False
descs | has_arg = map (string . showOption) (sort ns)
| otherwise = map (string . showOptionEquals pprefs) (sort ns)
isLong (OptLong _) = True
isLong _ = False
has_equals = any isLong ns && prefHelpLongEquals pprefs
desc | has_equals = listToChunk (intersperse (descSep style) descs) <> mv
| otherwise = listToChunk (intersperse (descSep style) descs) <<+>> mv
show_opt
| optVisibility opt == Hidden
= descHidden style

View File

@ -117,6 +117,9 @@ data ParserPrefs = ParserPrefs
-- subcommand fails (default: Backtrack)
, prefColumns :: Int -- ^ number of columns in the terminal, used to
-- format the help page (default: 80)
, prefHelpLongEquals :: Bool -- ^ when displaying long names in usage and help,
-- use an '=' sign for long names, rather than a
-- single space (default: False)
} deriving (Eq, Show)
data OptName = OptShort !Char

View File

@ -0,0 +1,6 @@
Usage: long_equals (-i|-j|--intval=|--intval2=ARG)
Available options:
-i,-j,--intval=,--intval2=ARG
integer value
-h,--help Show this help text

View File

@ -202,6 +202,17 @@ prop_nested_optional_help = once $
i = info (p <**> helper) idm
in checkHelpText "nested_optional" i ["--help"]
prop_long_equals :: Property
prop_long_equals = once $
let p :: Parser String
p = option auto (long "intval"
<> short 'j'
<> long "intval2"
<> short 'i'
<> help "integer value")
i = info (p <**> helper) fullDesc
in checkHelpTextWith ExitSuccess (prefs helpLongEquals) "long_equals" i ["--help"]
prop_nested_fun :: Property
prop_nested_fun = once $
let p :: Parser (String, Maybe (String, Maybe String))