问题描述
是否存在传递命令行参数的标准,或者每个程序之间有何不同?例如,下面是一些示例:
-
$ script.py -a 2
-
$ script.py -a=2
-
$ script.py a=2
-
$ script.py --all 2
-
$ script.py --all=2
解决方法
如果您使用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