[Haskell-cafe] Parsing arguments and reading configuration
derek.a.elkins at gmail.com
Fri Sep 19 19:29:51 EDT 2008
On Fri, 2008-09-19 at 23:24 +0100, Magnus Therning wrote:
> Hi all,
> I'm looking for some inspiration for an elegant solution to a silly
> little problem I have. This might have a general well-known solution,
> or maybe there's something particularly elegant possible in Haskell. I
> just thought I'd ask.
> When writing a command line tool I want to use a configuration file and
> then have the ability to override the configuration using command line
> arguments. When I've worked with command line arguments before I've
> used the trick of folding (>>=) over a list of functions that modify the
> "members" of a type, using the default values as the starting point. I
> like that, it's cute.
> First I thought I'd treat the configuration in a similar way, but then I
> noticed a slight ordering problem. The command line arguments should
> take priority over the contents of the configuration file, but the
> location of the configuration can be given as an argument. I could read
> the arguments twice, first to get the correct location of the config
> file, then load the config, and then read the arguments again to make
> sure they take priority. But that feels a little silly. Are there any
> more elegant solutions people are using?
You could build a monoid,
data Option a = Unspecified | Default a | Config a | CommandLine a
With Unspecified being the identity and the multiplication being
Default a * Config b = Config b
Default a * CommandLine b = CommandLine b
Config a * CommandLine b = CommandLine b
and symmetrically and break ties to the right e.g.
CommandLine a * CommandLine b = CommandLine b
More information about the Haskell-Cafe