为了账号安全,请及时绑定邮箱和手机立即绑定

选项参数的标准命令行分隔

选项参数的标准命令行分隔

子衿沉夜 2023-07-27 16:00:51
命令行参数的传递方式是否有标准,或者程序之间是否有所不同?例如,这里有几个例子:$ 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


查看完整回答
反对 回复 2023-07-27
  • 1 回答
  • 0 关注
  • 107 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信