我们说我有这样的功能:
def fun(n, m, verbose=0, use_fib_heap=False):
'''blah blah'''
pass
然后我解析命令行参数并尝试按指定运行它:
from sys import argv
opts = dict()
for arg in argv[1:]:
if arg.startswith('--'):
if '=' in arg:
p, v = arg[2:].split('=')[:2]
opts[p] = v
else:
p = arg[2:]
opts[p] = True
如果我将代码设为fun
,我该如何致电python3 code.py --verbose=3 --use_fib_heap
?
答案 0 :(得分:0)
只需正常调用该功能
from sys import argv
opts = dict()
for arg in argv[1:]:
if arg.startswith('--'):
if '=' in arg:
p, v = arg[2:].split('=')[:2]
opts[p] = v
else:
p = arg[2:]
opts[p] = True
fun(with_params)