[Haskell-cafe] Safe way to parse arguments?
Neil Mitchell
ndmitchell at gmail.com
Sat Jun 21 15:19:31 EDT 2008
Hi
> I'm wondering how usually you parse command line arguments
> list safely. If the given argument is wrong, the program
> can still print out some error information instead of giving
> something like
Either use reads instead, and deal with the case where there is no
parse. Or use the safe library, on hackage, and something like:
import Safe
readNote "please enter a number" x
Where if x is "test", you'll get:
Program error: Prelude.read: no parse, please enter a number, on "test"
Or use readDef:
readDef (error "Please enter a number") x
Which will just give you:
Program error: Please enter a number
Or, readMay, which will return a Nothing if you fail, which you can
deal with as you wish.
Thanks
Neil
More information about the Haskell-Cafe
mailing list