The Pythongetopt module processes the command line option instance.

Source: Internet
Author: User
This article mainly introduces the example of the Pythongetopt module processing command line options. This article is relatively simple. if you need it, refer to the getopt module to extract command line options and parameters, that is, sys. argv.
The 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'

The code is as follows:


Import getopt, sys
Shortargs = 'F: t'
Longargs = ['directory-prefix = ', 'format']
Opts, args = getopt. getopt (sys. argv [1:], shortargs, longargs)


Getopt. getopt ([command line parameter list], 'short options', [long optionlist])

Colon after short option name: indicates that this option must have additional parameters
Equal Sign = after long option name indicates that this option must have additional parameters

Returns opts and args
Opts is a parameter option and its value tuples ('-f', 'Hello'), ('-t', ''), ('-- format ', ''), ('-- directory-prefix','/home '))
Args is a command line input ('A', 'B') that removes useful parameters ')

The code is as follows:

# Then, you can retrieve all the command line options and their corresponding parameters by traversing opts.
For opt, val in opts:
If opt in ('-f',' -- format '):
Pass
If ....


Use the dictionary to accept the input of the command line and then transmit the dictionary, which makes the interface of the command line parameters more robust.

# Two examples from python2.5 Documentation

The code is as follows:


>>> Import getopt, sys
>>> Arg = '-a-B-c foo-d bar a1 a2'
>>> Optlist, args = getopt. getopt (sys. argv [1:], 'ABC: d :')
>>> Optlist
[('-A', ''), ('-B ',''), ('-C', 'Foo'), ('-D ', 'bar')]
>>> Args
['A1', 'A2 ']

>>> Arg = '-- condition = foo -- testing -- output-file abc. def-x a1 a2'
>>> Optlist, args = getopt. getopt (sys. argv [1:], 'X', ['condition = ', 'output-file =', 'testing'])
>>> Optlist
[('-- Condition', 'Foo'), ('-- testing', ''), ('-- output-file', 'ABC. def '), ('-X', '')]
>>> Args
['A1', 'A2 ']

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.