Default flags from environment variables
darrengarvey opened this issue · comments
Darren Garvey commented
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?
Yilei Yang commented
IMHO:
- 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 inabsl.testing
. argparse
doesn't support it.- 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. - 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.