Is there a pythonic way of assigning values to variables when passed in from the command line?
Question: I have written to following code but it feels very clunky and I was wondering
if there was a pythonic way of writing the following code:

    
    
    import argparse
    
    foo = 0
    bar = 1
    
    parser = argparse.ArgumentParser()
    parser.add_argument("-a", "--foo", type=int,
                        help="foo")
    parser.add_argument("-b", "--bar", type=int,
                        help="bar")
    
    args = parser.parse_args()
    if args.foo:        # This is the bit that I think is clunky
        foo = args.foo  #
    if args.bar:        #
        bar = args.bar  #
    

In my code I have about 7 different arguments and having a list of `if`
statements doesn't seem like the best method. Is there a better way of writing
this section?


Answer: `argparse` have default arguments, so there is no need for the `if`s. Also,
you should seperate argument parsing and processing, therefore you don't need
local variables for your args, but can use them as parameters. So you would
finally get to something like this:

    
    
    import argparse
    
    def some_function(foo, bar):
        pass
    
    def main():
        parser = argparse.ArgumentParser()
        parser.add_argument("-a", "--foo", type=int, default=0,
                            help="foo")
        parser.add_argument("-b", "--bar", type=int, default=1,
                            help="bar")
    
        args = parser.parse_args()
        some_function(args.foo, args.bar)
    
    
    if __name__ == '__main__':
        main()
    

