Day 24 - Figured Out Argparse

I finally figured out argparse . I didn’t quite understand how to utilize this Python module at first, but I think I’ve got the hang out it now. The point of using it was simply to give brandywine a few command-line options. Have a look:

if __name__ == '__main__':
    parser = argparse.ArgumentParser(description='Rotten Tomatoes cli')
    parser.add_argument('-b', '--boxoffice', \
        action='store_true', help='lists current box office movies.')
    parser.add_argument('-t', '--intheaters', \
        action='store_true', help='lists movies currently in theaters.')
    parser.add_argument('-s', '--search', \
        metavar='movie', help='query the movie database')
    args = parser.parse_args()
    brw = BrandyWine(vars(args))

The learning opportunity was around the ‘vars’ keyword. vars takes the parse arguments and turns them into a Python dictionary. At that point, I had the ability to iterate through the data structure and call the associated methods. There’s more I can do with argparse, but for now, this works.

Jason T Clark

Jason T Clark

Father. Musician. Gamer. Coder.

comments powered by Disqus
rss facebook twitter github youtube mail spotify instagram linkedin