[Haskell-cafe] GetOpt

Mirko Rahn rahn at ira.uka.de
Thu Apr 27 06:32:23 EDT 2006

Tomasz Zielonka wrote:
> On Thu, Apr 27, 2006 at 02:26:22AM +0300, Einar Karttunen wrote:

>>and handle options as functions from Config to Config:
>>>Option ['i']   ["input"]   (ReqArg (\x c -> c { infile = Just x }) "file") "input file name"

> I find this approach very convenient, but I push it a bit further. Some
> time ago I wrote a small article about this:
>     http://www.haskell.org/pipermail/haskell/2004-January/013412.html
> I was not the first one to use the approach but I felt that it should be
> made more popular. Perhaps I should make a wiki page from it, but I seem
> to never do such things and can't promise to do it this time :-/

You are dealing with more convenient option handling, validating and 
defaulting on top of Sven Pannes famous GetOpt module. Nice stuff but 
there is another important point: Your approach still needs a central 
definition of an option list (or record) in the main (user) program. But 
suppose you write some libraries that are used by a couple of user 
programs. It becomes tedious and error prone to define the same lists of 
options with descriptions and validating functions in all user programs 
just to give it to the library. Moreover the user program in general 
even don't know about the right validating function or option description.

So it would be much better to define the options in the library and to 
provide this definitions to the user program somehow. I tought about 
this topic several times and came up with a solution that works for me 
but is far from being perfect. It uses existentials and a main 
disadvantage is the need of explicit traversing. Moreover some new 
boilerplate code is necessary.

You can find the interface in


Sample library definitions of options are in


These definitions are combined in


and finally used for example in the user programs


Note, that the user programs just define options that are specific for 
the program, e.g. both programs have options to define some search 
bounds without definition.

As stated: Far from being perfect. Looking forward to get some new ideas!

Best regards, Mirko Rahn

-- Mirko Rahn -- Tel +49-721 608 7504 --
--- http://liinwww.ira.uka.de/~rahn/ ---

More information about the Haskell-Cafe mailing list