Q: Linux system commands such as LS, it has dozens of parameters, can take one or more parameters, in no particular order, used to be very professional. But you write the script, generally only a parameter, if the number of multiple, is also a fixed order, then how to use Python to write a more professional communication script it?
Answer: Use the Getopt module that comes with Python.
1 , Syntax:
Import getopt
Getopt.getopt (args,shortopts, longopts=[])
# Examples of functions: Getopt.getopt (sys.argv[1:], ' u:p:p:h ', ["username=", "password=", "port=", "help"])
# output format: [('-P ', ' 123 '), ('-U ', ' root ')] [] #后面中括号包含没有 "-" or "--" parameters
2 , parameter description:
args All parameters of the parameter, generally by sys.argv[1:], that is, all the content of the parameters;
shortopts short format, general parameters such as-u,-p,-h (a "-") is a short format, that is written in the function is "u:p:p:h", there is a colon is a parameter, no colon represents no parameters.
longopts long format, general parameters such as--username,--password,--help and so on (two "-") is the long format, which is written in the function is ["usrname=", ' password= ', "help"], Where--help is not a value, so there is no equal number. The other has an equals sign, which indicates that parameters are required later.
3 , demo effect:
Short format parameters:
[[email protected] scripts]# python getopt_test.py-u yangyun-p 123456-p 2222
Username:yangyun
password:123456
port:2222
Long format parameters: (can also add = number)
[[email protected] scripts]# python getopt_test.py--username yangyun--password 123456--port 2222
Username:yangyun
password:123456
port:2222
Long and short formats are used:
[[email protected] scripts]# python getopt_test.py--username=yangyun-p 123456--port 2222
Username:yangyun
password:123456
port:2222
Only a single parameter is passed, and the other is the default value:
[[email protected] scripts]# python getopt_test.py-p 123456
Username:root
password:123456
Port:22
# This is the default value for port and user, and the default value is specified in the function .
4 ,python example of a reference script:
# cat getopt_test.py
#!/usr/bin/python#by yangyun 2015-1-11 import getopt import sys# Importing Getopt,sys Modules #定义帮助函数def help (): print "usage error!" sys.exit () #输出用户名def username (username): print ' username: ',username #输出密码def password (password): if not password: help () else: print ' Password: ',password #输出端口def port (port): print ' port: ',port #获取传参内容, short format is-u,-p,-p,-h, where-H does not need to pass value. #长格式为--username,--Password,--port,--help, long format--help does not need to pass value. Opts,args=getopT.getopt (sys.argv[1:], ' u:p:p:h ', ["username=", "password=", "port=", "help"]) #print opts, ' ' ,args# sets the default value variable, and the default value is used when no parameters are passed. Username_value= "root" port_value= ' password_value= ' #密码不使用默认值, so the definition is empty. #循环参数列表, the output format is: [('-P ', ' 123 '), ('-U ', ' root ')] [] for opt , Value in opts: if opt in ("-U", "--username" ): username_ value=value # If there is a pass, the value is re-assigned. if opt in ("-P", "--password"): password_value=value if opt in ("-P", "--port"): port_value=value if opt in ("-H", "--help"): help () #执行输出用户名, password, port function, if there is no value for the variable, the default value is used. Username (username_value) password (password_value) port (port_value)
This article from "Yang Cloud" blog, declined reprint!
Write a professional reference script in Python