haskell - optparse-applicative : Require at least one argument

标签 haskell command-line-arguments

我正在使用 optparse-applicative应用程序中的库,它在命令行上接受多个字符串并对每个字符串执行一个操作。我的第一次尝试是这样的:

arguments Just
    ( metavar "EXPR"
    & help "Expressions to render, in zero-based De Bruijn index notation" )

不幸的是,这允许在没有参数的情况下运行程序,即使它没有多大意义。

我的第二次尝试涉及分别解析第一个参数,然后将其转换为列表的其余部分:
(:) <$> argument Just ( metavar "EXPR" )
    <*> arguments Just ( metavar "EXPR" )

这应该有效,但它没有:当用 --help 调用时,解析器将其吞噬并处理它,而不是显示帮助文本。

所以我的问题是:如何配置 optparse 以要求至少一个参数?

最佳答案

好的——我有 reported this issue致图书馆的作者 (Paolo Capriotti)。他回答:

The problem here is that arguments has some special logic to:

  • initially ignore arguments starting with '-'
  • accept '--'
  • accept arguments starting with '-' after '--' is encountered

Given this desired behavior, arguments cannot simply be implemented as many argument.

What we can do to make common use cases like the one in this Issue easier to deal with, is to add a bunch of convenience builders, like:

  • arguments1, non-empty argument list, with the same behavior as arguments
  • argument', parse 1 argument, ignoring things starting with '-'

This way, many argument' would be similar to arguments (without the special handling of --), and some argument' to arguments1.

Suggestions for better names are welcome. :)



也就是说,他增加了一个新功能 arguments1 做我所描述的。该功能从 0.5 版开始可用。

所以现在我的代码如下所示:
arguments1 Just
    ( metavar "EXPR"
   <> help "Expressions to render, in zero-based De Bruijn index notation" )

谢谢,保罗!

关于haskell - optparse-applicative : Require at least one argument,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12472566/

相关文章:

c++ - 在 Win32 控制台应用程序中读取命令行参数时出错?

java - 如何在 Java 中解析命令行参数?

bash - 将可变数量的 bash 命令行参数传递给 MATLAB 函数

haskell - 为什么绑定(bind)到 "unrestricted"类型变量会使变量变得刚性?

haskell - type 和 newtype 的编译时间和运行时间差异

perl - 几个 fork 进程中的第二个不在 Haskell 中运行 (GHC 7.8.4)

haskell - Haskell 中有哪些符号命名空间?

Haskell:具有类类型参数的类实例

c++ - 打开一个通过命令参数做某事的函数

python - python3 中的解析器不通过 argparse 从命令行获取定界符值