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

时间:2017-09-12 17:45:30

标签: 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 auto似乎是一个问题因为Read应该与Show“反向”,我想保留派生的instance Show BinaryFiles

1 个答案:

答案 0 :(得分:5)

使用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'."