getOpt return record of type a rather than [a]

Stefan O'Rear stefanor at cox.net
Tue Dec 18 17:21:03 EST 2007


On Tue, Dec 18, 2007 at 11:10:58PM +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
>     }
> 
> Then we would need
> 
> data ArgDescr a =
>   NoArg (a -> a)
>   ReqArg (String -> a -> a) String
>   OptArg (String -> a -> a) String
> 
> e.g.
> 
> OptArg (\path flags -> flags {input = Just path}) "FILE"
> 
> 
> 
> I feel this approach is so natural, that someone must have implemented it
> already.

The approach you describe is quite possible with the current GetOpt,
just apply (foldr ($) initialValue) to the return value, and allow a to
be instantiated with a function type.

Stefan
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: Digital signature
Url : http://www.haskell.org/pipermail/libraries/attachments/20071218/1bc7abe5/attachment.bin


More information about the Libraries mailing list