How do I decide whether an option belongs in an environment variable, command-line option, or both?

I am working on an executable program - an application or utility. At some point, I want to introduce a new option (to be considered at run-time rather than build-time); the option may be boolean, having a yes/no, on/off semantic; or it may have multiple values, in which case let us consider its value to be a string (which the app could parse into something else, perhaps). Now, I can: Read an environment variable from within the program, and invoke it with MY_OPTION=value my_program Parse a command-line option in the program, and invoke it with my_program --my-option=value Do both, i.e. accept either a command-line option or an environment varialbe (but then be left with the question of priority of one method over the other) Let's ignore, for the sake of discussion, the question of whether the executalbe can also take a configuration file on the command-line and parse it. What, in your experience, should this decision dependon? What criteria would you suggest for choosing between these alternatives?

May 20, 2025 - 18:30
 0

I am working on an executable program - an application or utility. At some point, I want to introduce a new option (to be considered at run-time rather than build-time); the option may be boolean, having a yes/no, on/off semantic; or it may have multiple values, in which case let us consider its value to be a string (which the app could parse into something else, perhaps).

Now, I can:

  • Read an environment variable from within the program, and invoke it with MY_OPTION=value my_program
  • Parse a command-line option in the program, and invoke it with my_program --my-option=value
  • Do both, i.e. accept either a command-line option or an environment varialbe (but then be left with the question of priority of one method over the other)

Let's ignore, for the sake of discussion, the question of whether the executalbe can also take a configuration file on the command-line and parse it.

What, in your experience, should this decision dependon? What criteria would you suggest for choosing between these alternatives?