我如何为来自 grep --help
的示例实现解析器:
--binary-files=TYPE assume that binary files are TYPE;
TYPE is 'binary', 'text', or 'without-match'
假设我有
data BinaryFiles = Binary | Text | WithoutMatch
我如何编写解析器?
option auto
自 Read
以来似乎是一团糟应该是 Show
的“逆” ,我想保留派生的 instance Show BinaryFiles
.
最佳答案
使用 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'."
关于haskell - 使用 optparse-applicative 解析 "enum"选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46182591/