[英]Parsing “enum” options with optparse-applicative
How do I implement a parser for this example from grep --help
: 如何从
grep --help
实现此示例的解析器:
--binary-files=TYPE assume that binary files are TYPE;
TYPE is 'binary', 'text', or 'without-match'
Assuming that I have 假设我有
data BinaryFiles = Binary | Text | WithoutMatch
How do I write a parser? 如何编写解析器?
option auto
seems a kludge since Read
is supposed to be an "inverse" to Show
, and I'd like to keep the derived instance Show BinaryFiles
. option auto
似乎有点麻烦,因为应该将Read
视为Show
的“逆过程”,而我想保留派生instance Show BinaryFiles
。
Use str
instead of auto
: 使用
str
代替auto
:
binFile :: ReadM BinaryFiles
binFile = str >>= \s -> case s of
"binary" -> return Binary
"text" -> return Text
"without-match" -> return WithoutMatch
_ -> readerError "Accepted binary file types are 'binary', 'text', and 'without-match'."
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.