The following is an example of how the Python getopt module processes command line options.
In python programming, the getopt module is as flexible and practical as the getopt parameter module in shell.
The getopt module is used t
The getopt module is used to extract command line options and parameters, that is, sys. argvThe command line option makes program parameters more flexible. Supports short and long options.For example, python scriptname. py-f 'hello' -- directory-prefix =/home-t -- format 'A'Copy codeThe Code is as follows:Import getopt, sysShortargs = 'f: t'Longargs = ['Directory
Sometimes we need to write some scripts to handle some tasks, it is often necessary to provide some command line parameters, according to different parameters for different processing, in Python, command line parameters and C language very similar (because standard Python is implemented in C language). In C, the main function of the prototype is int main (int argc, char **argv), here mainly refers to the Li
When running a program, you may need to enter different command line options based on different conditions to implement different functions. Currently, there are two formats: short option and long option. The short option format is "-" plus a single letter.The length option adds a word. The long format is introduced in Linux. Many Linux programs support these two formats. The getopt module is provided in Python.Now we support these two usage methods,
Python in the getopt module,This module is designed to handle the command line parameters.function getopt (args, shortopts, longopts = [])Parameter args is generally sys.argv[1:]Shortopts short Format (-)Longopts long Format (--)TryOptions,args = Getopt.getopt (sys.argv[1:], "HP:I:", ["Help", "ip=", "port="])Except Getopt
This article mainly introduces python's use of getopt to Parse command line input parameter instances, which is of great practical value, for more information about how to use getopt to parse parameters in the command line, see the example in this article.
The specific instance code is as follows:
Import getopt import sys config = {"input": "", "output ":". ",}
This article mainly introduces the Python getopt detailed and simple examples of relevant information, the need for friends can refer to the following
Python getopt detailed
Function Prototypes:
Getopt.getopt (args, shortopts, longopts=[])
Parameter explanation:
Args:args is a list of parameters that need to b
In the daily work of the need to write a lot of Python script, when the function of the script is more, or the need for external input parameters, if the parametersName and parameter values can be executed in a way that makes the script seem more intuitive and convenient for you.Python has a getopt module that handles command-line arguments.function getopt (args,
Python getopt and simple examples
Python getopt
Function prototype:
getopt.getopt(args, shortopts, longopts=[])
Parameter description:
Args: args is the list of parameters to be parsed. Sys. argv [1:] is generally used to filter out the first parameter (ps: the first parameter is the Script Name and should not be pa
Python getopt module, which is a function specifically designed to handle command-line arguments getopt (args, shortopts, longopts = []) parameters args are generally sys.argv[1:]shortopts short format (-)On the command line, enter:Python check_naginx.py-w5--warnint=5 --critical=The following code:Try: Options,args= Getopt.getopt (sys.argv[1:],"Hw:c:",[" Help","
The example of this article is about Python using getopt to parse command line input parameters and share them for everyone's reference.
The specific example code is as follows:
Import getopt
import sys
config = {"
input": "",
"Output": ".",
}
#getopt三个选项, the first is generally SYS.ARGV [1:]
In this paper, we describe Python's method of using getopt to parse command line input parameters and share them for your reference.
The specific instance code is as follows:
Import getopt Import sys config = { "input": "", "Output": ".", } #getopt三个选项, the first one is generally sys.argv[1:], The second parameter is a short parameter, if the argument m
The Getopt module is used to extract command-line options and parameters, i.e. SYS.ARGVcommand-line options make the program more flexible in its parameters. Supports short option mode and long option modeFor example python scriptname.py-f ' hello '--directory-prefix=/home-t--format ' a ' B '
Copy Code code as follows:
Import getopt, sys
Shortarg
In the project, especially a slightly larger project will basically use the configuration, will involve the configuration file read, configuration parameters read.The common parsing configuration file is Configparser, and the parse command-line argument is getopt.The parameters of getopt can be divided into two types: Long mode and short mode.The long mode is:--arg1 arg_value on the command line.Short mode is a common space-delimited parameter outside
For details about the use of the getopt function in Python, pythongetopt
Function prototype:
getopt.getopt(args, shortopts, longopts=[])
Parameter description:
Args: args is the list of parameters to be parsed. Sys. argv [1:] is generally used to filter out the first parameter (ps: the first parameter is the Script Name and should not be parsed as a parameter)
Shortopts: abbreviated parameter list
Lo
1 Importgetopt2 ImportSYS3 defusage ():4 Print 'This is a usage.'5 defMain ():6 Try:7 PrintSYS.ARGV8 #Sys.argv[0], representing the file path, sys.argv[1:], which indicates that the user entered all parameters9 #hp:i, with: Indicates the need for parameters, followed by long parameters need to take ' = 'TenOptions,args = Getopt.getopt (sys.argv[1:],"hp:i:",[" Help","ip=","port="]) One Print 'Options:', Options A Print 'args:', args - exceptgeto
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.