我正在使用 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 asmany 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 argumentsargument'
, 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/