[Haskell-cafe] Avoiding boilerplate retrieving GetOpt cmd line args

Andrea Rossato mailing_list at istitutocolli.org
Fri Jul 27 02:59:24 EDT 2007

On Thu, Jul 26, 2007 at 10:25:06PM -0700, Dave Bayer wrote:
>  Ok, I'm writing a command line tool, using System.Console.GetOpt to handle 
>  command line arguments. My Flags structure so far is
> > data Flag
> >     = Filter String
> >     | DateFormat String
> >     | DocStart String
> >     | DocEnd String
>  ...
>  and I want to write accessor functions that return the strings if specified, 
>  otherwise returning a default. The best I've been able to do is 

I don't know if my reply is going to be helpful to you. This is what I
would suggest: why don't you create a data type with label records,
and than you store that data type in a IORef and update the IORef. At
the end you just read the IORef with your updated data:

data Config = { filter :: String
	      , dateFormat :: String
	      ,	etc ...

Then you create a new IOref with config, and, with getOpt, you update
the IOref with modifyIORef. At the very end you read the modified

This way you can have default options to be modified with command line

I don't know if it is clear, but I adopted this approach in a program
I'm writing. A program with 14 command line options.

Have a look at this part here (starting from data Opts=):

This way I can load a configuration file and change some of the
options, configured in that file, with the given command line options.

I hope this is going to help you.

More information about the Haskell-Cafe mailing list