optparse-applicative:如何处理 Arrow 语法中的无参数情况

问题描述 投票:0回答:1

有示例:https://github.com/pcapriotti/optparse-applicative/blob/master/tests/Examples/Cabal.hs#L46-L62

parser :: Parser Args
parser = runA $ proc () -> do
  opts <- asA commonOpts -< ()
  cmds <- (asA . hsubparser)
            ( command "install"
              (info installParser
                    (progDesc "Installs a list of packages"))
           <> command "update"
              (info updateParser
                    (progDesc "Updates list of known packages"))
           <> command "configure"
              (info configureParser
                    (progDesc "Prepare to build the package"))
           <> command "build"
              (info buildParser
                    (progDesc "Make this package ready for installation")) ) -< ()
  A version >>> A helper -< Args opts cmds

...

pinfo :: ParserInfo Args
pinfo = info parser
  ( progDesc "An example modelled on cabal" )

main :: IO ()
main = do
  r <- execParser pinfo
  print r

因此,默认情况下,当我不使用参数时,它会显示使用信息。我想使用不带参数的情况,也想使用一个[自定义参数](使用自定义参数时我收到错误:

Invalid argument 'regreg'

如何在这里处理空参数和自定义参数?

haskell command-line-arguments applicative arrow-abstraction optparse-applicative
1个回答
4
投票

https://hackage.haskell.org/package/optparse-applicative-0.11.0.2/docs/Options-Applicative-Arrows.html

中的
A不是ArrowPlus。我不确定是否可以(假设
f
Alternative
),但现在你必须使用
Alternative
语法:

subparser (command "foo" ... ...) <|> pure DefaultCommand
© www.soinside.com 2019 - 2024. All rights reserved.