Detailed explanation of how to use the python OptionParser module

高洛峰
Release: 2017-03-20 11:20:15
Original
2065 people have browsed it

OptionParser is a module used to process the command line in python. It is a necessary tool in our process development using python

Optparse. It is powerful and easy to use. It can Conveniently generate standard command line instructions that comply with Unix/Posix specifications

Example code

from optparse import OptionParser
parse = OptionParser()

parse.add_option("-l", "--language", action= "store_true",
                dest= "lan",
                default= True,
                help= "write language for Program")

(option, arges) = parse.parse_args()if option.lan == True:    print 'lan is True'
Copy after login

As shown above, first import the OptionParser class and create the OptionParser object

Use add_option () to define command line parameters, and finally use parse_args() to parse the command line

add_option parameter parsing, action includes store (default), store_false, store_false, store_const, append, count, callback

Another function of OptParser is to automatically generate help information for the program, which will be explained later. The above is a preliminary learning

The above is the detailed content of Detailed explanation of how to use the python OptionParser module. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!