Reference: http://docs.python.org/2/library/optparse.html
Python has two built-in modules for processing command line parameters:
One is getopt, and getopt can only process command line parameters.
The other is optparse, which is a Python module that allows program designers to easily design simple, easy to use, and compliant with standard Unix Command columns. Generate usage and help information.
The following is a simple example script optparse_exam
Python has two built-in modules for handling command-line arguments:One is that getopt,getopt can simply handle command-line arguments.The other is optparse, a Python module that allows programmers to easily design simple, easy-to-use, standard UNIX command-line Programs. Generate usage and help Information.The following is a simple example script optparse_exampl_1.py:[email protected] python]# Vim optparse_exampl_1.py
#!/usr/bin/env
Reference:
Http://www.cnblogs.com/captain_jack/archive/2011/01/11/1933366.html
Https://docs.python.org/2/library/optparse.html
eg#This is the blocking Get poetry now! client.Importdatetime, Optparse, SocketdefParse_args (): Usage="""usage:%prog [options] [hostname]:p ort ... This is the Get poetry now! Client, blocking edition. Run it like This:python get-poetry.py port1 port2 port3 ... If you is in the base directory of the Twisted-
Python as the important language of our dry operation, we are sure that we have written a lot, we write things not for ourselves, but for our team or more people.
Then you have to say the method of getting the parameters under Python. Python's own module optparse is what we have always liked and used (of course there are getopt, but not this easy to use). Nonsense do not say, the following say how to use it!!
First, paste the official document addre
Reprint: http://www.jb51.net/article/59296.htmRecently encountered a problem is to specify parameters to run a particular process, which is very similar to some of the parameters of Linux commands, such as ls-a, why the plus-a option will respond. The Optparse module implements a similar function, which is to pass command parameters to the script.Before using this module, you first need to import the class Optionparser in the module and then create an
The Optparse module is primarily used to pass command parameters to scripts and to parse command-line arguments with predefined options.You first need to introduce the Optparser module, then perform the initialization, instantiate a Optionparser object (with or without parameters), and then add options to the command line, for example: fromOptparseImportOptionparserusage="Show something Usefull-- forExample:how to use the program"Parser= Optionparser
Installation:
1. Direct installation on the line WINDOW10 the Python 2.7 feels no problem.
Document:
Official documents:
Https://docs.python.org/2/library/optparse.html
Use:
#coding: Utf-8 import optparse def main (): Usage = "Usage:%prog [options] arg" parser = Optparse.
Optionparser (usage) parser.add_option ("F", dest= "filename", help= "read data from filename") Parser.add_option ("-V", "--verb
Introduction to the optparse Module
Recently, a problem occurs when you specify parameters to run a specific process. This is similar to the parameters of some commands in Linux, such as LS-A. Why does the-A option respond when it is added. The optparse module Implements similar functions. It transmits command parameters for scripts.
Before using this module, you must first import the optionparser class in
Recently encountered a problem is to specify parameters to run a particular process, which is very similar to some of the parameters of Linux commands, such as ls-a, why the plus-a option will respond. The Optparse module implements a similar function, which is to pass command parameters to the script.
Before using this module, you first need to import the class Optionparser in the module and then create an instance (object) of it:Copy the Code code
Python optparse module Usage Analysis, pythonoptparse
Recently, a problem occurs when you specify parameters to run a specific process. This is similar to the parameters of some commands in Linux, such as ls-a. Why does the-a option respond when it is added. The optparse module Implements similar functions. It transmits command parameters for scripts.
Before using this module, you must first import the Opti
A recent problem is to specify parameters to run a particular process, which is similar to the parameters of some of the commands in Linux, such as Ls-a, why the plus-a option responds. The Optparse module implements a similar function, which is to pass command parameters for the script.
Before using this module, you first need to import the class Optionparser in the module, and then create an instance of it (object):
Copy Code code as follows:
When you use a command line, if you want to add an option, Python 2.3 Adds a new module called Optparse, which is also dedicated to handling command-line options.
Copy Code code as follows:
From Optparse import Optionparser
Parser = Optionparser ()
Parser.add_option ("P", "--PDBK", action= "Store_true",
Dest= "PDCL",
Default=false,
help= "Write PDBK data to Oracle DB")
Parser.add_option ("Z
Python optparse module instance, pythonoptparse
If you want to add options when using command lines, a new module named optparse is added in python 2.3, which is also used to process command line options.Copy codeThe Code is as follows:From optparse import OptionParserParser = OptionParser ()Parser. add_option ("-p", "-- pdbk", action = "store_true ",Dest = "pdcl
A brief introduction to the Optparse modulePython has two built-in modules for handling command-line arguments:One is that getopt can only handle command-line arguments easily;The other is Optparse, which is powerful and easy to use, and makes it easy to generate standard, UNIX/POSIX-compliant command-line instructions. will automatically take care of the-H help option for you.To customize the parameters of
Optparse is a module that is specifically designed to add options at the command line.First look at a sample code
From Optparse import OptionparserMsg_usage = "myprog[-f][-s] arg1[,arg2 ...]"Optparser = Optionparser (msg_usage)Optparser.add_option ("-F", "--file", action = "store", type= "string", Dest = "FileName")Ooptparser.add_option ("-V", "--vison", action= "Store_false", dest= "verbose",
Optparse = OptionParser () # The class parameters can also be defined here.
attr=value, ...)
Parser. add_option (,...) #-f and -- file are tags used as parameters during the call and will be recognized
(Options, args) = parser. parse_args () # parse_args can have parameters. If not defined, use the default sys. argv [1:]
Storage Methods: store, store_false, and store_true. If the action value is not specified, the default valu
When you use Python for scripting, it is often necessary to invoke the Python script module in the form of a terminal interaction. At this timeSupport for providing the underlying command-line parameters in a Python module is essential. So, in Python, how do we implement command-line arguments and parse it, the following is a brief introduction to this.Python support for command-line argument parsingSupport for command-line argument parsing via two built-in modules in Python: getopt and Optparse
Optparse First, you must import the Optionparser class to create a Optionparser object: use add_option to define command-line arguments: -F or –file are the long and short parameter names: Finally, once you have defined all the command-line arguments, call Parse_args () to resolve the program's command line: You can also pass a command-line argument list to Parse_args (); otherwise, the default is to use Sys.argv[:1]. two values retur
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.