首页 > 其他 > 详细

argparse

时间:2019-02-10 17:54:02      阅读:216      评论:0      收藏:0      [点我收藏+]

该模块的功能主要是从程序外部执行行,获取外部传递进来的参数

import argparse
parser = argparse.ArgumentParser()
parser.add_argument(-s, --server, dest=server, help="ftp server ip")
parser.add_argument(-p, dest=port, help="ftp server port", type=int)
args = parser.parse_args()
print(args)

‘-s’,‘--server‘ 是外部传递参数进来的书写方式

python xxx.py  -s 参数    或者  python xxx.py --server 参数

FTPclient\core>python test.py -s 127.0.0.1
FTPclient\core>python test.py -server 127.0.0.1

 

dest的作用是把传递进来的值赋值给该值

Namespace(port=None, server=127.0.0.1)

 

-h 是打印help里面的信息

FTPclient\core>python test.py -h
usage: test.py [-h] [-server SERVER] [-p PORT]

optional arguments:
  -h, --help      show this help message and exit
  -server SERVER  ftp server ip
  -p PORT         ftp server port

type 是指定传递进来的值的类型

FTPclient\core>python test.py -p asaS
usage: test.py [-h] [-server SERVER] [-p PORT]
test.py: error: argument -p: invalid int value: asaS

 

当如果是这个的格式时,没有 -  参数,有dest  则是必须传递参数进来 

parser.add_argument(dest=server, help="ftp server ip")

 

argparse

原文:https://www.cnblogs.com/zhengyiqun1992/p/10359530.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!