Python 官方文档:入门教程 => 点击学习
说明 处理可选参数与位置参数 handles both optional and positional arguments 产生标准化的帮助信息 produces highly infORMative usage messages 支持
# 初始化一个实例
parser = argparse.ArgumentParser(
description='sum the integers at the command line')
# 添加位置参数, 类型为int
parser.add_argument(
'integers', metavar='int', nargs='+', type=int,
help='an integer to be summed')
# 添加可选参数,默认为标准输出,类型为FileType文件类
parser.add_argument(
'--log', default=sys.stdout, type=argparse.FileType('w'),
help='the file where the sum should be written')
# 解析
args = parser.parse_args()
# Namespace(count='50', echo='Good', host='172.168.100.1')
args.log.write('%s' % sum(args.integers))
args.log.close()
#coding:utf8
import argparse
class Args(object):
def __init__(self):
parser = argparse.ArgumentParser(
description="A test network port tool"
)
parser.add_argument(
"echo",
help="echo info."
)
parser.add_argument(
"-H", "--host",
help="ipaddr or domain addr."
)
parser.add_argument(
"-c", "--count",
help="connect counts"
)
args = parser.parse_args()
self.args = args
def cc(self):
print self.args
print "args host: ", self.args.host
print "args count: " ,self.args.count
if __name__ == "__main__":
a = Args()
a.cc()
➜ test git:(master) ✗ python argpar.py good -H 172.168.100.1 -c 50
Namespace(count='50', echo='good', host='172.168.100.1')
args host: 172.168.100.1
args count: 50
--结束END--
本文标题: python命令行参数模块argpars
本文链接: https://www.lsjlt.com/news/186791.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-03-01
2024-03-01
2024-03-01
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0