vendredi 27 mai 2016

Passing around parsed arguments makes no fun

I use arparse in Python to parse arguments from the command line:

def main():
    parser = argparse.ArgumentParser(usage=usage)
    parser.add_argument('-v', '--verbose', dest='verbose', action='store_true')
    parser.add_argument(...)
    args = parser.parse_args()

I use object args only in few places of the code.

There are three methods and the call stack looks like this

def first_level(args):
    second_level()

def second_level():
    third_level()

def third_level():
    ### here I want to add some logging if args.verbose is True

I want to add some logging to third_level().

I don't like to change the signature of the method second_level().

How can I make the arg object available in third_lelvel()?

I could store arg as global variable, but I was told to not use global variables in a developer training some years ago ....

What is common way to handle this?

Aucun commentaire:

Enregistrer un commentaire