npo*_*cop 2 haskell optparse-applicative
如何从以下方面为此示例实现解析器grep --help:
--binary-files=TYPE assume that binary files are TYPE;
TYPE is 'binary', 'text', or 'without-match'
Run Code Online (Sandbox Code Playgroud)
假设我有
data BinaryFiles = Binary | Text | WithoutMatch
Run Code Online (Sandbox Code Playgroud)
我该如何编写解析器?option auto似乎是一个kludge,因为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'."
Run Code Online (Sandbox Code Playgroud)