How Python implements case functionality like in the shell

Source: Internet
Author: User
Tags case statement

we know that a case statement is supported in a shell script, and when the positional parameter is empty, we are prompted how to use the scriptSo how do you do it in Python? Do you also use case? Case statements are not supported in Python, but there are ways to implement case.  Here we talk about the Getopt moduledescribes the parse command-line operation . here is the getopt format:getopt.getopt (args, shortopts, longopts=[])args refers to the parameters that are received by the current script, which is a list that can be obtained by SYS.ARGVshortopts is short parameter what is short parameter ah? Similar to this: Python test.py-h # output Help informationlongopts is long parameter what is long parameter ah? Similar to this: Python test.py-help # output Help information return valueThis function returns a list of two tuples (a review, the value of the tuple is not modifiable!) )let me write a little example to give you a little insight.
Getoptgetopt. getopt (sys.argv[1:],'-H', ['help']) print (ARG) 

 The results are as follows:
[Email protected]:~/python# python3. 5 test.py-h ([('-H')], []) [email protected]:~/ python# Python3. 5 test.py--help([('--help')], [])

 you can see that the parameters have been received. And did the processing, why I passed Sys.argv[1:]? because sys.argv in the argv[0] is the file name of the current script, do not need it to participate in, otherwise your options and option values can not match, a lot of problems. What if I want to receive an option for a parameter + parameter value?  Write a small example!
1 #!/usr/bin/env python3.52 Importurllib.request3 Importgetopt4 ImportSYS5 6Opts,args = Getopt.getopt (sys.argv[1:],'-h-f:-v',[' Help','filename=','version'])7  forOpt_name,opt_valueinchopts:8     ifOpt_nameinch('- H','--help'):9         Print("[*] Help info")Ten exit () One     ifOpt_nameinch('- v','--version'): A         Print("[*] Version is 0.01") - exit () -     ifOpt_nameinch('- F','--filename'): theFileName =Opt_value -         Print("[*] Filename is", FileName) -         #Do something -Exit ()

 run the test results as follows:
[Email protected]:~/python# python3.5test.py--filename=test[*] Filename is test[email protected]:~/python# Python3.5test.py--filename=[*] Filename is [email protected]:~/python# Python3.5test.py--help[*] HelpInfo[email protected]:~/python# Python3.5test.py--version[*] Version is0.01[email protected]:~/python# Python3.5test.py-v[*] Version is0.01[email protected]:~/python# Python3.5test.py-F test[*] Filename is test[email protected]:~/python#

 to explain these lines of code in detail.start with the name of the short parameter first. I've defined '-h-f:-v ' everyone found no, there's a ":" Behind-F.This ":" indicates that the current parameter is a value, and is a parameter of parameter name + parameter valueIf I add another-O: Then Proof-O can receive a value, which is the value of-o parameter, will be saved to the OPTs variable. long parameter name is similar to short parameter, the only difference is the long parameter if you want to receive the value, it must be followed by a "="

How Python implements case functionality like in the shell

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.