4

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.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
errge
  • 309
  • 1
  • 6
  • What's the question? How to ensure all instances of declared flags are visible? Did you consider a type class? – Don Stewart Apr 09 '12 at 12:02
  • 1
    It's not a particularly Haskellish way to go about things. Haskell modules generally don't automatically do magic that mucks about with global state, largely because Haskell programmes don't actually _have_ global state. At a minimum you'd need modules to be able to run code when they're imported, and there's no cause for that in pure functional programming, again because there is no such thing as state. – Matthew Walton Apr 10 '12 at 12:44
  • Don: sorry for not being explicit about the question. I would like to know if there is anything like this available that I haven't found. If not, then I need ideas to implement my flag registration mechanism between modules. I played with the idea of using type classes for this hack, my main concern is with the following (made up example): Main imports ProtocolServer, ProtocolServer imports TCPServer, TCPServer defines the flag tcp_soreuseaddr :: Bool. Now, Main won't see this flag and neither the TH hack. This is why I would like to have some kind of static initialization. – errge Apr 10 '12 at 15:49
  • Matthew: thanks for your comment. I know that what I'm asking for is not usual Haskell, but Google GFlags library was neither usual in it's thinking at the time when getopt was the standard. Still, if you try gflags for your next c/c++ project, you will never go back for getopt. Command line parsing is a special thing, where some unsafePerformIO makes life easier, not harder. Command line values don't change during one run of a program, after all. – errge Apr 10 '12 at 15:56
  • Don: sorry, you were right. My issue can be handled with type classes, just :info misled me. I reported an issue about my confusion (http://hackage.haskell.org/trac/ghc/ticket/5998). If I finish my implementation with the type class hack, I will share it here, thanks for the idea! – errge Apr 11 '12 at 18:27
  • I note that the GFlags URL (http://gflags.googlecode.com/svn/trunk/doc/gflags.html) is AWOL as of 2022-07-19. You can find information at https://gflags.github.io/gflags/ which seems likely to be a durable URL. A Google search 'google gflags software' finds other relevant links. – Jonathan Leffler Jul 19 '22 at 19:54

2 Answers2

3

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

errge
  • 309
  • 1
  • 6
2

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

Daniel Wagner
  • 145,880
  • 9
  • 220
  • 380