24

For the python program I am writing I would like to give the opportunity of configuring it in three different ways. Environment variables, configuration files and command line arguments.

Logically I think command line arguments should always have the highest priority. I am a bit doubting whether environment variables should have precedence over configuration files? And will it matter whether configuration files are system wide, user specific or given as argument on the command line?

(Note that my platform is Unix/Linux)

Peter Smit
  • 27,696
  • 33
  • 111
  • 170

2 Answers2

42

The standard that I know is first look for a command line parameter, if not found environment var, then local config file then global config file.

So when a package is installed somewhere. It will have a default config file. This can be changed with a local config file. Which can be overrridden with a environ parameter and then the command line param has the highest precedence.

If a config file is declared on the command line then its contents will take precedence over environ params or any other config files. But command line params will take precedence over it. But remember that the search path still exists. If the package is looking for a var it looks for.

Command line.
Config file thats name is declared on the command line.
Environment vars
Local config file (if exists)
Global config file (if exists)

I think many command line compilers and the Boost library config pak works in a similar fashion

kingchris
  • 1,677
  • 22
  • 29
  • I don't know if your given order is official or always meaningful, but it sounds pretty good to me :) +1 – Mecki Jan 30 '14 at 20:18
  • Well we could always spend lots of money and make an IEEE standard. Na. Too much time and effort for little reward. Microsoft and Google and the other usual suspects would have their thousand and one reasons why they can't possibly conform to the Mecki/King guideline. – kingchris Jan 31 '14 at 07:08
  • 2
    The "configuration file on the command line" position is arguable. From my experience, it typically replaces the stock files. – ivan_pozdeev Oct 09 '15 at 18:35
  • 2
    I couldn't find any standard for this in its own right, but POSIX enforces the "configuration->environment->command line" application order systematically for the utilities covered by it. – ivan_pozdeev Oct 09 '15 at 18:37
  • @ivan_pozdeev "configuration file on the command line" allows for special cases where you don't want to modify standard config files or plug in tens of command line parameters. Very useful while developing or testing later. – kingchris Oct 10 '15 at 05:36
  • 1
    @kingchris Aah, that looks like a special option, `@file`, that inserts options from the file and is equivalent to specifying them on the command line in the same place. UNIX doesn't need this since there are shell backticks, but portable programs make use of this sometimes. I wouldn't call this "configuration file" since configuration files typically have a different format than a command line. – ivan_pozdeev Oct 10 '15 at 09:58
  • 1
    @ivan_pozdeev Do you have examples of the POSIX enforcement? It seems quite odd to have a tool favour an environment variable over a configuration file, as that would be quite confusing if there was an env variable set that the user wasn't aware of. So I'd love an example or three. – Chris Leishman Nov 24 '15 at 11:44
  • 1
    @ChrisLeishman [Search over the POSIX utility specs](https://www.google.ru/search?q=%22%2Fetc%22+environment+variable+site%3Ahttp%3A%2F%2Fpubs.opengroup.org%2Fonlinepubs%2F9699919799%2Futilities%2F) shows that few of them actually use both environment variables and configuration files. For `mailx`, they do not intersect. [`sh`](http://pubs.opengroup.org/onlinepubs/9699919799/utilities/sh.html) inherits environment from parent and _then_ executes a file specified in `ENV`. But, a shell treats env vars and its own the same, so it's only natural. I couldn't find an example of a different program. – ivan_pozdeev Nov 24 '15 at 18:10
8

AWS CLI is in line with the accepted answer:

Precedence of options:

  • If you specify an option by using one of the environment variables described in this topic, it overrides any value loaded from a profile in the configuration file.

  • If you specify an option by using a parameter on the CLI command line, it overrides any value from either the corresponding environment variable or a profile in the configuration file.

Community
  • 1
  • 1
saaj
  • 23,253
  • 3
  • 104
  • 105