4

我如何为此示例实现解析器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.

4

1 回答 1

8

使用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'."
于 2017-09-12T18:19:30.530 回答