What order of reading configuration values?

2019-04-04 23:17发布

问题:

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)

回答1:

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



回答2:

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.