[Haskell-cafe] Safe way to parse arguments?

Malcolm Wallace Malcolm.Wallace at cs.york.ac.uk
Mon Jun 23 05:04:14 EDT 2008

> > I'm wondering how usually you parse command line arguments
> > list safely.
> >
> > Prelude.read: no parse
> It's generally not a good idea to call 'read' on user data.

There is (at least one) proper parsing library intended as a direct
replacement for Read:


The basic idea is that the function 'parse' replaces 'read', and to use
it at any specific type T:

    (runParser parse) :: String -> (Either String T, String)

You get back either an error message or the value, along with the
remaining unparsed text.  Instances of the Parse class are defined for
all Prelude types, and you can easily derive instances for any of your
own types, using the DrIFT tool.  Parse is assumed to be an inverse of
Show, that is, the derived instances of Parse will read back a
canonically derived Show value.

Another benefit of using a real parser library is that you can plumb
together several parsers quickly if you need to.  e.g.
    runParser (exactly 3 parse) :: String -> (Either String [T], String)


More information about the Haskell-Cafe mailing list