I have 8 variables being loaded in from environment variables. If any of these are none, I'd like to bail and sys.exit
- but I'd also like to alert the user to which variable was missing.
There are a few ways of doing this. Assume the a = os.environ.get('a')
code is prewritten, through h
The most verbose way that works is:
if a is None:
print("A is required")
sys.exit(1)
if b is None:
print("B is required")
sys.exit(1)
if c is None:
print("C is required")
sys.exit(1)
...
so on until the check for h
The cleanest way is:
if not any([a,b,c,d,e,f,g,h]):
print("? is required")
sys.exit(1)
or even:
if None in [a,b,c,d,e,f,g,h]:
print("? is required")
sys.exit(1)
Is it possible to actually get the variable name from one of the more python checks (the latter two) and print it out to the user?
I could get the index of the variable by doing [a,b,c,d,e,f,g,h].index(None)
but I'm not sure how to go from the index to the variable name.