I have a set (4 flags) of flags which control the nature of the output that my python script generates and ones which control how much information the script prints to the terminal (the way a verbose
flag generally does). These flags are used by multiple functions. The values of the flags are set by optional command line arguments which I parse using argparse
.
At present I have these flags in global scope outside of any of the functions that use the flags rather than have the flags passed as arguments to the functions. Like so:
import argparse
flag1=True
flag2=True
flag3=True
def fun1():
...
if flag1:
..something..
else:
..something else..
#fun1 also uses the other flags in a similar way
...
def fun2():
...
if flag2:
..something..
else:
..something else..
#fun1 also uses the other flags in a similar way
...
def main()
...
args=parser.parse_args()
...
global flag1
flag1=args.flag1
# and so on for the other flags
fun1()
fun2()
I have been lead to believe that it is bad practice to use global variables in python from many of the answers here (this, for example). Does that apply here too? I am a little reluctant to pass the flags as arguments since I'll be passing too many variables as arguments.
What is the best way to handle flags set by command line arguments?