getOpt return record of type a rather than [a]

Duncan Coutts duncan.coutts at
Tue Dec 18 18:56:49 EST 2007

On Tue, 2007-12-18 at 23:10 +0100, Henning Thielemann wrote:
> The current version of System.Console.GetOpt.getOpt returns a list of
> values, where the element type has usually one constructor per option.
>  data Flag
>      = Verbose | Version
>      | Input String | Output String | LibDir String
> What I more like to receive is a record consisting of one constructor and
> many fields, where optional options are of type Maybe, options with
> multiple occurrence are of type list.
>  data Flag = Flag {
>       verbose :: Bool,
>       version :: Bool,
>       input  :: Maybe FilePath,
>       output :: Maybe FilePath,
>       libdir :: FilePath
>     }

> OptArg (\path flags -> flags {input = Just path}) "FILE"

> I feel this approach is so natural, that someone must have implemented it
> already.

Yes it's implemented in Cabal which has commands with loads and loads of
flags. As Lemmih says, it can be layered on top of GetOpt quite easily.

Indeed Cabal goes one step further and makes the conversion two way so
that one can take a value of a type like your Flag above and convert it
back into [String]. This is pretty useful when calling the program.

Another interesting thing is that all flag types are Monoids:

Cabal now implements this idea in the modules:


More information about the Libraries mailing list