2017-09-12 2 views
0

Comment applicatif puis-je créer un analyseur pour cet exemple de grep --help:Parsing "ENUM" options avec optparse-

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

En supposant que je

data BinaryFiles = Binary | Text | WithoutMatch 

Comment puis-je écrire un analyseur syntaxique? option auto semble un kludge puisque Read est censé être un "inverse" à Show, et je voudrais garder le instance Show BinaryFiles dérivé.

Répondre

4

Utilisation str au lieu de 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'."