Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parsing "enum" options with optparse-applicative

How do I implement a parser for this example from grep --help:

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

Assuming that I have

data BinaryFiles = Binary | Text | WithoutMatch

How do I write a parser? option auto seems a kludge since Read is supposed to be an "inverse" to Show, and I'd like to keep the derived instance Show BinaryFiles.

like image 621
nponeccop Avatar asked Jan 30 '23 14:01

nponeccop


1 Answers

Use str instead of 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'."
like image 92
Daniel Wagner Avatar answered Feb 08 '23 13:02

Daniel Wagner