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

Parsing "enum" options with 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 应该是 "inverse" 到 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'."