Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to implement command line with parameters in Python+getopt

2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

Python+getopt how to achieve command line parameters, I believe that many inexperienced people do not know what to do, so this paper summarizes the causes of the problem and solutions, through this article I hope you can solve this problem.

Python+getopt implements command line with parameters

Getopt module in python, which is specifically used to handle command line arguments

Function getopt (args, shortopts, longopts = [])

The parameter args is generally sys.argv [1:], shortopts short format (-), longopts long format (- -)

Enter on the command line:

# python test.py-I 127.0.0.1-p 80 55 66

# python test.py-ip=127.0.0.1-port=80 55 66

Code:

#! / usr/bin/python

Import getopt

Import sys

Def usage ():

Print'- h help\ n'\

'- i ip address\ n'\

'- p port number\ n'\

''

If _ _ name__ = ='_ _ main__':

Try:

Options, args = getopt.getopt (sys.argv [1:], "hp:i:", ['help', "ip=", "port="])

For name, value in options:

If name in ('- hype,'--help'):

Usage ()

Elif name in ('- iTunes,'--ip'):

Print value

Elif name in ('- paired,'--port'):

Print value

Except getopt.GetoptError:

Usage ()

Rgv [1:], "hp:i:", ["help", "ip=", "port="])

"hp:i:" short format

H is not followed by a colon: it means that there are no parameters, and p: and I: are followed by a colon to indicate that parameters are required.

["help", "ip=", "port="] long format

There is no equal sign after help, which means that there are no parameters, and the other three have =, indicating that parameters are needed later.

The return value options is a list of meta-ancestors, and each meta-ancestor is the format information analyzed, for example, [('- iSequence 127.0.0.1'), ('- pawning meme80')]

Args is a list that contains parameters that do not have'-'or'- -', such as: ['55 'June' 66']

Note: when defining command line parameters, define parameters with the'- 'option first, and then define parameters without' -'.

After reading the above, have you mastered how Python+getopt implements the command line with parameters? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report