Python file keyword argument?

邮差的信 提交于 2019-12-05 02:31:59

I think what you're looking for is the argparse module https://docs.python.org/dev/library/argparse.html.

It will allows you to use command line option and argument parsing.

e.g. Assume the following for script.py

import argparse

if __name__ == '__main__':
   parser = argparse.ArgumentParser()
   parser.add_argument('--arg1')
   parser.add_argument('--arg2')
   args = parser.parse_args()

   print args.arg1
   print args.arg2

   my_dict = {'arg1': args.arg1, 'arg2': args.arg2}
   print my_dict

Now, if you try:

  $ python script.py --arg1 3 --arg2 4

you will see:

3
4
{'arg1': '3', 'arg2': '4'}

as output. I think this is what you were after.

But read the documentation, since this is a very watered down example of how to use argparse. For instance the '3' and '4' I passed in are viewed as str's not as integers

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!