Python module Optparse Get the parameters detailed

Source: Internet
Author: User

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 address: http://docs.python.org/2/library/optparse.html

Before you speak, refer to the default font with an example:


From Optparse import Optionparser

Parser = Optionparser ()
Parser.add_option ("F", "--file", dest= "filename",
help= "Write to File", metavar= "file"
Parser.add_option ("Q", "--quiet",
Action= "Store_false", dest= "verbose", Default=true,
help= "Don ' t print status messages to stdout")

(options, args) = Parser.parse_args ()
Two parameters are introduced here:-F and-Q. You can see adding options through the Add_option function. In the-f argument, the add_option function argument is followed by short options, long options, descriptive information, help information, and the default variable name.
The-q parameter below is used to indicate that no subsequent arguments are required. add_option function parameters are short options, long options, store actions (default is store, and Store_false and store_true), default values, help information.

In action: Store_ture/store_false saves the corresponding Boolean value. These two actions are used to implement a Boolean switch.

In addition to the arguments that appear above, there is type (the data type that represents the parameter: String,int, etc.).

We can also group the parameters, using the method Optparse. OptionGroup.


Group = OptionGroup (parser, "title", "description")
Group.add_option ("G", action= "Store_true", help= "Group option.")
Parser.add_option_group (Group)
Then there are version,usage, description usage, complete with the screenshot below:

Okay, put it on. Run Results:


root@an-bt5:/apps/python# python test.py-h
Usage:test.py [Options] arg1 arg2

This is a test script!!!

Options:
--version Show Program ' s version number and exit
-H,--help show this Help and exit
-F FILE,--file=file write to File
-N Number,--num=number
Define number
-Q,--quiet don ' t print status messages to stdout

Title
Description

-G Group option.
root@an-bt5:/apps/python# python test.py-n 1
{' Number ': 1, ' verbose ': True, ' G ': none, ' filename ': none}
[]
root@an-bt5:/apps/python# python test.py-n 1-f test.txt
{' Number ': 1, ' verbose ': True, ' G ': None, ' filename ': ' Test.txt '}
[]
root@an-bt5:/apps/python# python test.py-n 1-f test.txt-g
{' Number ': 1, ' verbose ': true, ' G ': true, ' filename ': ' Test.txt '}
[]
root@an-bt5:/apps/python# python test.py-n 1-f test.txt-g 1
{' Number ': 1, ' verbose ': true, ' G ': true, ' filename ': ' Test.txt '}
[' 1 ']
root@an-bt5:/apps/python# python test.py-n 1-f test.txt-g
{' Number ': 1, ' verbose ': true, ' G ': true, ' filename ': ' Test.txt '}
[]
root@an-bt5:/apps/python#
There is also a function print_help we often use to output help information at the time of validation. Well, finally, try it in your script.

Related Article

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.