前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Python中的 getopt模块

Python中的 getopt模块

作者头像
py3study
发布2020-01-07 19:59:46
8650
发布2020-01-07 19:59:46
举报
文章被收录于专栏:python3python3

python 的 getopt 模块是一个简单实用的命令行参数解析模块。实现命令解析功能的为模块中的getopt 方法。下面主要介绍一下这个getopt方法的使用。

查看getopt 模块的帮助可以得到 getopt方法的所有解释。

代码语言:javascript
复制
    getopt(args, shortopts, longopts=[])
        getopt(args, options[, long_options]) -> opts, args
        
        Parses command line options and parameter list.  args is the
        argument list to be parsed, without the leading reference to the
        running program.  Typically, this means "sys.argv[1:]".  shortopts
        is the string of option letters that the script wants to
        recognize, with options that require an argument followed by a
        colon (i.e., the same format that Unix getopt() uses).  If
        specified, longopts is a list of strings with the names of the
        long options which should be supported.  The leading '--'
        characters should not be included in the option name.  Options
        which require an argument should be followed by an equal sign
        ('=').
        
        The return value consists of two elements: the first is a list of
        (option, value) pairs; the second is the list of program arguments
        left after the option list was stripped (this is a trailing slice
        of the first argument).  Each option-and-value pair returned has
        the option as its first element, prefixed with a hyphen (e.g.,
        '-x'), and the option argument as its second element, or an empty
        string if the option has no argument.  The options occur in the
        list in the same order in which they were found, thus allowing
        multiple occurrences.  Long and short options may be mixed.

getopt 方法的参数解释:

args 参数,在这个方法中会获取 sys.argv[1:] 对应的所有命令行参数。

shortopts 代表短参数。接收字符串类型的参数。若参数后面需要填写参数值,则使用冒号(:)表示。

example:

"hp:u:" 则表示参数 -h 且后面无需参数值。而参数 -p 后面需要参数值。同样参数 -u 也需要参数值。

longopts 代表长参数。 接收list类型的参数。后参数后面需要填写参数值,这使用等号(=)表示。

example:

["help","port=","user="] 则表示参数--help 后面无需填写参数值。而参数--port后面需要填写参数值,同样--user 后面也需要填写参数值。

getopt 方法的返回值解释:

getopt 方法返回两个list

第一个list 以 [(option,value),(option,value)] 的形式表示命令好中定义的参数

第二个参数 也是以list 的方式,它返回的是没有经过定义的参数,且这些参数必须在定义参数值后出现。若在定义参数中间出现,getopt方法会认为这之后的全部是为定义的参数。

使用方法举例:

代码语言:javascript
复制
#! /usr/bin/env python

import getopt
import sys


if __name__ == "__main__":
    try:
        options,args = getopt.getopt(sys.argv[1:],"hp:u:",["help","port=","user="])
    except getopt.GetoptError:
        sys.exit()

    print options
    print args

做如下执行:

代码语言:javascript
复制
$ python testgetopt.py -h --port 80 --user www aa bb cc
[('-h', ''), ('--port', '80'), ('--user', 'www')]
['aa', 'bb', 'cc']

其中 aa bb cc 都是为定义的参数,放置在定义参数值后。

若做如下执行:

代码语言:javascript
复制
$ python testgetopt.py -h --port 80 aa --user www bb cc
[('-h', ''), ('--port', '80')]
['aa', '--user', 'www', 'bb', 'cc']

将为定义的参数 aa 放置到了已定义参数 --user 之前,这getopt认为 aa 及之后的所有参数都是未定义的。这种现象显然是不OK的,因此应该避免使用这样的形式。

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019-09-07 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档