简体   繁体   English

使用optparse-applicative解析“枚举”选项

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM