内置模块 - optparse

 1 # -*- coding:utf-8 -*-
 2 import sys
 3 import optparse
 4 
 5 def fun():
 6     print('hello world')
 7     print(sys.argv)
 8 
 9 fun()
10 
11 """
12 终端执行:
13     python test_argument.py alice lily
14         hello world
15         ['test_argument.py', 'alice', 'lily'] 
16 """
17 
18 def opt():
19     parser = optparse.OptionParser()
20     parser.add_option("-s", "--server", dest="server", help="ftp server ip_addr")
21     parser.add_option("-o", "--port", type="int", dest="port", help="ftp server port")
22     parser.add_option("-u", "--username", dest="username", help="username info")
23     parser.add_option("-p", "--password", dest="password", help="password info")
24 
25     options, args = parser.parse_args()
26     print(options,args)
27     print(options.server,options.port,options.username,options.password)
28 
29 opt()
30 
31 """
32 终端执行:
33     python test_argument.py -s 127.0.0.1 -o 8080 -u alice -p 123
34         {'server': '127.0.0.1', 'port': 8080, 'username': 'alice', 'password': '123'} []
35         127.0.0.1 8080 alice 123
36         
37     python test_argument.py -help
38         Usage: test_argument.py [options]
39         Options:
40           -h, --help            show this help message and exit
41           -s SERVER, --server=SERVER
42                                 ftp server ip_addr
43           -o PORT, --port=PORT  ftp server port
44           -u USERNAME, --username=USERNAME
45                                 username info
46           -p PASSWORD, --password=PASSWORD
47                                 password info
48 """
49 
50 """
51 参考:https://www.cnblogs.com/captain_jack/archive/2011/01/11/1933366.html
52      https://docs.python.org/3/library/optparse.html
53      http://www.jb51.net/article/59296.htm
54 """
posted @ 2018-03-27 08:54  Alice的小屋  阅读(215)  评论(0编辑  收藏  举报