Domanda

I'm interested in having something very similar to Google's flags library for Haskell.

Here is the small introduction to gflags that demonstrates why I love it: http://gflags.googlecode.com/svn/trunk/doc/gflags.html

I looked into the various getopt like libraries on Hackage and I haven't found one that matches the simplicity and flexibility of gflags.

Namely, I'd like to have these features:

  • generates --help (with default values mentioned in the help),
  • besides parsing the options given by the user, it should also err on unmatched options, so the user has a chance to note typos,
  • flags can be declared in any module easily (hopefully at the top-level, Template Haskell hackery acceptable if needed),
  • no need in main to call out to all the modules where I declared flags, instead the flags somehow register themselves at startup/linking/whatever time,
  • it's OK if main has to call a general initialization function, like in gflags'
    google::ParseCommandLineFlags(&argc, &argv, true);
  • flags can be used purely (yeah, I think this is an appropriate usage of unsafePerformIO to make the API more simple).

After looking around without success, I played with the idea of doing this myself (and of course sharing it on Hackage). However, I have absolutely no idea for the implementation of the registration part. I need something similar to GCC's ((constructor)) attribute or to C++'s static initialization, but in Haskell. Standard top-level unsafePerformIO is not enough, because that is lazy, so it won't be called before main starts to run.

È stato utile?

Soluzione

After investigating all the solutions on Hackage (thanks for all the tips!), I went ahead with Don's typeclass implementation idea and created a library named HFlags.

It's on hackage: http://hackage.haskell.org/package/hflags

I also have a blog post, describing it: http://blog.risko.hu/2012/04/ann-hflags-0.html

Altri suggerimenti

You might like CmdArgs, though I haven't used it enough to tell if it satisfies all your constraints.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top