你如何表示默认值strOption
?optparse-applicative库的文档没有显示如何为strOption创建默认值,例如,
data Sample = Sample
{ hello :: String
, quiet :: Bool }
sample :: Parser Sample
sample = Sample
<$> strOption
( long "hello"
<> metavar "TARGET"
<> help "Target for the greeting" )
<*> switch
( long "quiet"
<> help "Whether to be quiet" )
Run Code Online (Sandbox Code Playgroud)
虽然它确实显示了如何为标志选项提供默认值.例如,Normal
是默认值
data Verbosity = Normal | Verbose
flag Normal Verbose
( long "verbose"
<> short 'v'
<> help "Enable verbose mode"
Run Code Online (Sandbox Code Playgroud)
编辑
我想我在value
修饰符中找到了答案Options.Applicative.Builder
.
Rei*_*chs 12
这确实存在,value
尽管在文档中很难找到.
例:
strOption ( long "some-opt"
<> value "default value"
<> metavar "SOMEOPT"
<> help "an option demonstrating the use of `value'" )
Run Code Online (Sandbox Code Playgroud)
引用自述文件:
解析器是Applicative和Alternative的实例
这意味着你应该能够像这样指定默认值:
someOption <|> pure "thisIsUsedIfSomeOptionWasn'tPassed"
Run Code Online (Sandbox Code Playgroud)
你可以像这样创建一个默认的组合子
defaultValue :: Alternative f => a -> f a -> f a
defaultValue = flip (<|>) . pure
Run Code Online (Sandbox Code Playgroud)
并像这样使用它
optionWithDefault :: Parser String
optionWithDefault = defaultValue "defaultValue" option
Run Code Online (Sandbox Code Playgroud)
如果不是已经以某种形式存在,我会感到惊讶.