Python外部参数传入

1. argparse

import argparse

def test():
    parser = argparse.ArgumentParser(description="test")
    parser.add_argument('-n','--name',default="alex")   
    parser.add_argument('-y', '--year',default="18")
    parser.add_argument('-c','--city',default='beijing')  
    args = parser.parse_args()
    name = args.name
    year = args.year
    city = args.city
    print(f'my name is {name}, {year} years old. my city is {city}')
   
if __name__ == '__main__':
    test()

执行命令时:

python test.py -y 23 -c shanghai -n tony 

2. fire

import fire

def hello(name):
  return 'Hello {name}!'.format(name=name)

if __name__ == '__main__':
  fire.Fire(hello)

调用

python test.py test

3. sys

在右键菜单中调用python脚本.

import sys

script,first,second,third = sys.argv

print ("The script is called:{%s}"% script)
print ("The first variable is:{%s}"% first)
print ("The second variable is:{%s}"% second)
print ("The third variable is:{%s}"% third)

4. 右键

pSdFNE8.png

右键菜单的生效位置和注册表对应的路径.
pSdFeHK.png

5. 注册表

pSdFZB6.png

注意参数的设置

pSdFnAO.png
pSdFVnx.png