haskell - 使用 optparse-applicative 解析 "enum"选项

标签 haskell optparse-applicative

我如何为来自 grep --help 的示例实现解析器:

 --binary-files=TYPE   assume that binary files are TYPE;
                       TYPE is 'binary', 'text', or 'without-match'

假设我有
data BinaryFiles = Binary | Text | WithoutMatch

我如何编写解析器? option autoRead 以来似乎是一团糟应该是 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/

相关文章:

haskell - 从免费的替代仿函数生成 optparse-applicative 解析器

haskell - 依赖类型的 ghc-7.6 类实例

haskell - 余数据类型真的是终端代数吗?

haskell - 基准测试和惰性评估

haskell - GHCI 从不加载编译文件

haskell - 在 Haskell 中,处理守卫时 where 子句的范围是什么?

haskell - 是否可以有一个带有多个参数的 optparse-applicative 选项?

haskell - 如何将可选标志解析为 Maybe 值?

haskell - optparse-applicative 子命令帮助文本