abseil / abseil-py

Abseil Common Libraries (Python)

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Default flags from environment variables

darrengarvey opened this issue · comments

It's common to default some command line arguments from the environment, where command line overrides the environment variables, which in turn overrides any config file.

Absl doesn't support this but I don't see an explanation of why - is it a design choice?

It is of course possible to do something like this:

flags.DEFINE_string('foo', os.getenv('FOO', 'Some default'), '')

but it's also quite common to have multiple environment variables, which starts getting quite repetitive. Is there any fundamental argument against adding some support for using environment variables, regardless of how that surfaces in the API?

IMHO:

  1. We haven't seen a case where all flags need this. We do have several flags that do get default value from environment variables, but we just explicitly call os.getenv (or equivalent). e.g. the --test_srcdir flag in absl.testing.
  2. argparse doesn't support it.
  3. Prefer explicit than implicit. If a flag / application needs to get value from environment variables, it's better to explicitly do that. If repetition is a concern, one can always define wrapper functions on top of absl.flags in their own application/library. absl.flags can be viewed as a lower level API.
  4. Flags are case sensitive and it's not uncommon to define different flags with same letters but different cases (common with single letter flags). But environment variables are usually all upper case. There isn't an undebatable way to map between flag names and environment names and avoid conflicts.

Hope this explains things.