In [1]:
def main():
    from optparse import OptionParser


    parser = OptionParser()
    parser.add_option("-f", "--file", dest="infilename",
                  help="Your input filename")
    parser.add_option("-q", "--quiet",
                  action="store_false", dest="verbose", default=True,
                  help="don't print status messages to stdout")

    (options, args) = parser.parse_args()

    print ('You are going to read in this file: %s' % options.infilename)

If I was running this in a command line, I would omit the main function and call the script like: python ./optparse_example.py -f foo. But since I am trying to show this in a Jupyter notebook, I am faking it with this little call using sys.argv.

In [2]:
import sys
if __name__ == "__main__":
    sys.argv = ['','-f','foo']
    main()
You are going to read in this file: foo
In [ ]: