nponeccop
nponeccop

Reputation: 13677

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.

Upvotes: 4

Views: 563

Answers (1)

Daniel Wagner
Daniel Wagner

Reputation: 153112

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

Upvotes: 9

Related Questions