30

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)

2 Answers 2

48

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

8
  • 3
    The "configuration file on the command line" position is arguable. From my experience, it typically replaces the stock files. Commented Oct 9, 2015 at 18:35
  • 3
    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. Commented Oct 9, 2015 at 18:37
  • 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. Commented Oct 10, 2015 at 9: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. Commented Nov 24, 2015 at 11:44
  • 1
    @ChrisLeishman Search over the POSIX utility specs shows that few of them actually use both environment variables and configuration files. For mailx, they do not intersect. sh 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. Commented Nov 24, 2015 at 18:10
10

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.