keskiviikko 16. maaliskuuta 2011

Parsing command-line arguments with Haskell

I'm writing this because I found yet another great thing about Haskell: it's actually a nice language for writing command-line tools! A command-line tool written in Haskell reads like the manual for the tool. Pattern matching makes it easier and cleaner than in any other language (prove me wrong).

You can match exact strings for commands like "init", "update", "status", as well as any number of following arguments that you can use in the implementation of that command.

    main = getArgs >>= rebass

rebass ["init", name] = do
-- init using given name, ignore rest of arguments
rebass ["update"] = do
-- update, ignore arguments
rebass ["status"] = do
-- show status, ignore arguments
rebass _ = do
-- show usage as none of the patterns matched

Here's what I'm working on btw:

2 kommenttia:

  1. Looks pretty clean.

    One thing comes to mind: what about printing out all the possible arguments with e.g. ./rebass --help. Do you know any way of achieving this with your approach?

  2. Aki, that might not be trivial, as according to my understanding, Haskell doesn't particularly excel in things reflective. I'd guess you've have to create a Command abstraction for this.