12

So this is a funny little thing I thing Stack Overflow has not discussed before. Not really a life and death question, but something I'd like to hear your input.

In short: my program is a bit over engineered in this regard: it takes configuration options from four different places:

  • Command line parameters
  • Configuration file
  • Environment variable
  • Windows Registry
  • (And hardcoded default values)

The question is, in what order should these be evaluated? I think it's clear that the command line options have the last say, but what about the three others? If the same option is set in both environment and ini-file, which should take precedence?

What about registry, does registry override the ini assuming I use that to change the runtime settings of the program, should I rewrite changes applied to registry into the configuration file as well? Should I set it so that registry settings can't override settings read from environment?

(If you wonder how on earth this is possible, one word: X-macro.)

VITTUIX-MAN
  • 346
  • 2
  • 8
  • Well, things have certainly happened, so I guess I should tell what I have learned in a month: I use environment variable for install time configuration, so that must override the configuration file and I use registry for runtime-configuration (with ugly MS Forms gui!), so that has the final say, unless a commandline flag is set, which overrides registry for the duration of that session. Almost acceptable, I feel I'm almost getting a hand in this "software development" thing. – VITTUIX-MAN Sep 28 '15 at 04:55
  • Related: https://stackoverflow.com/questions/11077223/what-order-of-reading-configuration-values – Pedro Feb 19 '16 at 15:21

1 Answers1

13

Ordered by priority i would go this way: cli > envvars > registry > config > defaults

  • cli: it takes the options that you surely want to use when you manually start the program
  • envvar: generaly used to manually override configuration file options at runtime (often also used today to manage configuration options in containers environments, cf. 12factor app)
  • registry: specific to Microsoft Windows world, but according to what you said it have almost the same purpose that environment variables
  • config: defined at installation/configuration time so these are the "user wanted default options"
  • defaults: minimal config to run securely (for example listen on localhost only) when nothing else is provided
Nicolas Karolak
  • 345
  • 3
  • 8