命令行參數的傳遞方式是否有標準,或者程序之間是否有所不同?例如,這里有幾個例子:$ script.py -a 2$ script.py -a=2$ script.py a=2$ script.py --all 2$ script.py --all=2
1 回答

千萬里不及你
TA貢獻1784條經驗 獲得超9個贊
如果您使用argparse,您將發現對上述大多數選項的支持。例如:
# test.py
import argparse
parser = argparse.ArgumentParser(description='Dedupe library.')
parser.add_argument( '-a', '--all', nargs='+', type = int, help='(Optional) Enter one or more Master IDs.')
跑步:
df$ python test.py -a 2
# {'all': [2]}
df$ python test.py -a=2
# {'all': [2]}
df$ python test.py a=2
# test.py: error: unrecognized arguments: a=2
$ python test.py --all 2
# {'all': [2]}
$ python test.py --all=2
# {'all': [2]}
如您所見,除了以下形式之外,所有內容均受支持script.py a=2
添加回答
舉報
0/150
提交
取消