Henning Thielemann | 18 Dec 23:09

getOpt return record of type a rather than [a]


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"

(Continue reading)

Stefan O'Rear | 18 Dec 23:21

Re: getOpt return record of type a rather than [a]

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"
(Continue reading)

Henning Thielemann | 19 Dec 07:55

Re: getOpt return record of type a rather than [a]


On Tue, 18 Dec 2007, Stefan O'Rear wrote:

> 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.

Great idea! One should add this as example to the docs of GetOpt, because
the example given there works the way I didn't like.
Henning Thielemann | 19 Dec 10:56

Re: getOpt return record of type a rather than [a]


On Tue, 18 Dec 2007, Stefan O'Rear wrote:

> 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.

I thought this must be a good article for Category:Idioms, and what do I
find there ...
  http://www.haskell.org/haskellwiki/High-level_option_handling_with_GetOpt
Lemmih | 18 Dec 23:22
Gravatar

Re: getOpt return record of type a rather than [a]

On Dec 18, 2007 11:10 PM, Henning Thielemann
<lemming <at> henning-thielemann.de> 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.
>
(Continue reading)

Bulat Ziganshin | 18 Dec 23:28

Re[2]: getOpt return record of type a rather than [a]

Hello Lemmih,

Wednesday, December 19, 2007, 1:22:43 AM, you wrote:
> Indeed, we to this in HAppS, for instance. Folding the resulting list
> makes the current GetOpt implementation more than adequate.

this is problem important for me too - i have >50 options. can you
please give us a small example of such approach?

--

-- 
Best regards,
 Bulat                            mailto:Bulat.Ziganshin <at> gmail.com
Lemmih | 18 Dec 23:54
Gravatar

Re: Re[2]: getOpt return record of type a rather than [a]

On Dec 18, 2007 11:28 PM, Bulat Ziganshin <bulat.ziganshin <at> gmail.com> wrote:
> Hello Lemmih,
>
> Wednesday, December 19, 2007, 1:22:43 AM, you wrote:
> > Indeed, we to this in HAppS, for instance. Folding the resulting list
> > makes the current GetOpt implementation more than adequate.
>
> this is problem important for me too - i have >50 options. can you
> please give us a small example of such approach?

Henning posted the specialized structure:
data ArgDescr a =
 NoArg (a -> a)
 ReqArg (String -> a -> a) String
 OptArg (String -> a -> a) String

which is a same as:
type DirectArgDescr a = ArgDescr (a -> a)

The "flags" can be combined using: foldr ($) emptyConfig flags.

--

-- 
Cheers,
  Lemmih
Duncan Coutts | 19 Dec 00:56

Re: getOpt return record of type a rather than [a]


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
(Continue reading)


Gmane