I have a script which has certain options that can either be passed on the command line, or from environment variables. The CLI should take precedence if both are present, and an error occur if neither are set.
I could check that the option is assigned after parsing, but I prefer to let argparse to do the heavy lifting and be responsible for displaying the usage statement if parsing fails.
I have come up with a couple of alternative approaches to this (which I will post below as answers so they can be discussed separately) but they feel pretty kludgey to me and I think that I am missing something.
Is there an accepted "best" way of doing this?
(Edit to make the desired behaviour clear when both the CLI option and environment variable are unset)
To add an optional argument, simply omit the required parameter in add_argument() . args = parser. parse_args()if args.
Number of Arguments If you want your parameters to accept a list of items you can specify nargs=n for how many arguments to accept. Note, if you set nargs=1 , it will return as a list not a single value.
2. I would add this: store_true means if true was specified then set param value but otherwise leave it to None. If default was also specified then param is set to that value instead of leaving it to None.
You can set the default=
of the argument to a .get()
of os.environ
with the environment variable you want to grab.
You can also pass a 2nd argument in the .get()
call, which is the default value if .get()
doesn't find an environment variable by that name (by default .get()
returns None
in that case).
import argparse import os parser = argparse.ArgumentParser(description='test') parser.add_argument('--url', default=os.environ.get('URL')) args = parser.parse_args() if not args.url: exit(parser.print_usage())
I use this pattern frequently enough that I have packaged a simple action class to handle it:
import argparse import os class EnvDefault(argparse.Action): def __init__(self, envvar, required=True, default=None, **kwargs): if not default and envvar: if envvar in os.environ: default = os.environ[envvar] if required and default: required = False super(EnvDefault, self).__init__(default=default, required=required, **kwargs) def __call__(self, parser, namespace, values, option_string=None): setattr(namespace, self.dest, values)
I can then call this from my code with:
import argparse from envdefault import EnvDefault parser=argparse.ArgumentParser() parser.add_argument( "-u", "--url", action=EnvDefault, envvar='URL', help="Specify the URL to process (can also be specified using URL environment variable)") args=parser.parse_args()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With