当前位置: 首页 > news >正文

安卓app大全下载安徽网站推广优化

安卓app大全下载,安徽网站推广优化,淮安网站设计公司,好的做网站的菜鸟教程《Python 3 教程》笔记 EX 01:命令行参数 1 命令行参数1.1 基础用法1.2 getopt 模块1.2.1 getopt.getopt 方法1.2.2 getopt.gnu_getopt 方法1.2.3 Exception getopt.GetoptError1.2.4 exception getopt.error 笔记带有个人侧重点,不追求面面俱到…

菜鸟教程《Python 3 教程》笔记 EX 01:命令行参数

  • 1 命令行参数
    • 1.1 基础用法
    • 1.2 getopt 模块
      • 1.2.1 getopt.getopt 方法
      • 1.2.2 getopt.gnu_getopt 方法
      • 1.2.3 Exception getopt.GetoptError
      • 1.2.4 exception getopt.error

笔记带有个人侧重点,不追求面面俱到。

1 命令行参数

出处: 菜鸟教程 - Python3 命令行参数

1.1 基础用法

Python 中可以所用 syssys.argv 来获取命令行参数:

注意: sys.argv[0] 为脚本名。

实例:

test.py 文件:

#!/usr/bin/python3import sysprint ('参数个数为:', len(sys.argv), '个参数。')
print ('参数列表:', str(sys.argv))
print ('脚本名:', str(sys.argv[0]))

运行结果:

>> python test.py arg1 arg2 arg3
参数个数为: 4 个参数。
参数列表: ['test.py', 'arg1', 'arg2', 'arg3']
脚本名: test.py

1.2 getopt 模块

getopt 模块是专门处理命令行参数的模块,用于获取命令行选项和参数。该模块提供了两个方法及一个异常处理来解析命令行参数。

1.2.1 getopt.getopt 方法

语法:

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

参数:

  • args:要解析的命令行参数列表;
  • shortopts:接收字符串,解析为“短选项”。shortopts 后的冒号 : 表示该选项必须有附加的参数,不带冒号表示该选项不附加参数;
  • longopts:接收列表,解析为“长选项”。longopts 后的等号 = 表示如果设置该选项,必须有附加的参数,否则就不附加参数。

返回值:

  • opts:由元组 (opt, value) 组成的列表。例如输入"-h -i inputfile -o outputfile" 返回值就是 [(‘-h’, ‘’), (‘-i’, ‘inputfile’), (‘-o’, ‘outputfile’)];
  • args:参数(value)列表,包含除了长选项和短选项以及各自选项的参数以外的其他未知的参数。

注意:

  1. 长选项和短选项以及各自的参数都会按先后次序放在 opts 中;
  2. 返回的 opt 里面,--- 都被保留下来了;
  3. 长选项没有写完的时候,会被自动补全。比如用户输入的是 --u,通过 getopt 会被自动补全--user

实例:

# test.pyimport sys
import getoptdef site():argv = sys.argv[1:]  # 以空格分割的字符串列表try:opts, args = getopt.getopt(argv, "abn:u:", ["name=", "url="])# 长选项模式except Exception as err:print(err)print("opts: ", opts)print("args: ", args)if __name__ == "__main__":site()

输出:

1)正常使用

>>> python test.py -a -b -u item1 -n item2 --n item3 --url item4
opts:  [('-a', ''), ('-b', ''), ('-u', 'item1'), ('-n', 'item2'), ('--name', 'item3'), ('--url', 'item4')]
args:  []

2)只输入参数

>>> python test.py item1 item2
opts:  []
args:  ['item1', 'item2']

3)只输入选项

>>> python test.py -a
opts:  [('-a', '')]
args:  []>>> python test.py -n
option -n requires argument  # 报错

4)错误输入选项

>>> python test.py -
opts:  []
args:  ['-']>>> python test.py --
opts:  []
args:  []>>> python test.py ---
option --- not recognized  # 报错>>> python test.py -c
option -c not recognized  # 报错>>> python test.py ---b
option ---b not recognized  # 报错>>> python test.py -n-a
opts:  [('-n', '-a')]
args:  []>>> python test.py -a-n
option -- not recognized  # 报错>>> python test.py -a--n
option -- not recognized  # 报错

5)错误输入参数

>>> python test.py -a item
opts:  [('-a', '')]
args:  ['item']>>> python test.py -aitem
option -i not recognized  # 报错>>> python test.py -antem
opts:  [('-a', ''), ('-n', 'tem')]
args:  []>>> python test.py -abntem
opts:  [('-a', ''), ('-b', ''), ('-n', 'tem')]
args:  []>>> python test.py -acntem
option -c not recognized>>> python test.py -n item1 -a item2 --n item3
opts:  [('-n', 'item1'), ('-a', '')]
args:  ['item2', '--n', 'item3']>>> python test.py -n item1 item2 -a
opts:  [('-n', 'item1')]
args:  ['item2', '-a']>>> python test.py item1 item2 -a
opts:  []
args:  ['item1', 'item2', '-a']

总结:欢迎指正、补充

  1. 命令行参数从左到右、依次解析
  2. 一般情况下以空格分割选项和参数,附加参数的短选项后紧跟的参数也可以被解析,例如:-nitem
  3. 解析时,先碰到未定义的选项时,会报错,先碰到多余的参数时,之后的参数都被放入返回值 args 中;
  4. 短选项可以采用 -abn 的样式输入,无附加参数的选项必须在前,否则会报错。

1.2.2 getopt.gnu_getopt 方法

语法:

getopt.gnu_getopt(args, shortopts, longopts=[])

此函数与 getopt() 类似,区别在于它默认使用 GNU 风格的扫描模式。这意味着选项和非选项参数可能会混在一起。getopt() 函数将在遇到非选项参数时立即停止处理选项。

如果选项字符串的第一个字符为 +,或者如果设置了环境变量 POSIXLY_CORRECT,则选项处理会在遇到非选项参数时立即停止。

1.2.3 Exception getopt.GetoptError

当参数列表中出现不可识别的选项或者当一个需要参数的选项未带参数时将引发此异常。此异常的参数是一个指明错误原因的字符串

对于长选项,将一个参数传给不需要参数的选项也将导致引发此异常。(没发现)

msg 和 opt 属性会给出错误消息和关联的选项;如果没有关联到异常的特定选项,则 opt 将为空字符串。

报错 1:

需要参数的选项未带参数。

报错 2:

如果 longopts 为 [‘foo’, ‘frob’],则选项 --fo 将匹配为 --foo,但 --f 将不能得到唯一匹配,因此将引发 GetoptError。

1.2.4 exception getopt.error

GetoptError 的别名,用于向后兼容。

扩展阅读: 《Python 官方文档》:getopt — C 风格的命令行选项解析器


文章转载自:
http://hydroperoxide.Lbqt.cn
http://nesting.Lbqt.cn
http://dogly.Lbqt.cn
http://canarese.Lbqt.cn
http://bullring.Lbqt.cn
http://trifid.Lbqt.cn
http://baggage.Lbqt.cn
http://shipman.Lbqt.cn
http://romaic.Lbqt.cn
http://hammada.Lbqt.cn
http://telebanking.Lbqt.cn
http://intelligible.Lbqt.cn
http://whatever.Lbqt.cn
http://unsolicited.Lbqt.cn
http://sned.Lbqt.cn
http://fourflusher.Lbqt.cn
http://audiotyping.Lbqt.cn
http://fusimotor.Lbqt.cn
http://haematimeter.Lbqt.cn
http://spignel.Lbqt.cn
http://suckle.Lbqt.cn
http://ytterbite.Lbqt.cn
http://weighlock.Lbqt.cn
http://thioketone.Lbqt.cn
http://ponytail.Lbqt.cn
http://highjacking.Lbqt.cn
http://apocarpy.Lbqt.cn
http://gallabiya.Lbqt.cn
http://emmeline.Lbqt.cn
http://citizenhood.Lbqt.cn
http://tracker.Lbqt.cn
http://kenning.Lbqt.cn
http://soundex.Lbqt.cn
http://keyswitch.Lbqt.cn
http://dearly.Lbqt.cn
http://intertwist.Lbqt.cn
http://weimaraner.Lbqt.cn
http://thereof.Lbqt.cn
http://smile.Lbqt.cn
http://anglia.Lbqt.cn
http://unequitable.Lbqt.cn
http://beibu.Lbqt.cn
http://rasbora.Lbqt.cn
http://bragger.Lbqt.cn
http://trimurti.Lbqt.cn
http://weltschmerz.Lbqt.cn
http://gazetteer.Lbqt.cn
http://hayburner.Lbqt.cn
http://knowledgable.Lbqt.cn
http://semidominant.Lbqt.cn
http://sunos.Lbqt.cn
http://underpants.Lbqt.cn
http://ringy.Lbqt.cn
http://algiers.Lbqt.cn
http://monochasial.Lbqt.cn
http://semina.Lbqt.cn
http://matrass.Lbqt.cn
http://landtrost.Lbqt.cn
http://dysprosium.Lbqt.cn
http://dolores.Lbqt.cn
http://passional.Lbqt.cn
http://timbrel.Lbqt.cn
http://moistly.Lbqt.cn
http://team.Lbqt.cn
http://prevention.Lbqt.cn
http://hourglass.Lbqt.cn
http://anywhere.Lbqt.cn
http://satisfactory.Lbqt.cn
http://superplastic.Lbqt.cn
http://streetwalking.Lbqt.cn
http://fasciola.Lbqt.cn
http://anionic.Lbqt.cn
http://urethroscope.Lbqt.cn
http://drupe.Lbqt.cn
http://touchback.Lbqt.cn
http://deliberately.Lbqt.cn
http://reversible.Lbqt.cn
http://mucociliary.Lbqt.cn
http://contactbreaker.Lbqt.cn
http://motory.Lbqt.cn
http://eparch.Lbqt.cn
http://doomful.Lbqt.cn
http://ambuscade.Lbqt.cn
http://hemiacetal.Lbqt.cn
http://revisor.Lbqt.cn
http://dishevelment.Lbqt.cn
http://amoral.Lbqt.cn
http://climatically.Lbqt.cn
http://squarehead.Lbqt.cn
http://macronutrient.Lbqt.cn
http://habitably.Lbqt.cn
http://planosol.Lbqt.cn
http://cityward.Lbqt.cn
http://asexuality.Lbqt.cn
http://diabetogenic.Lbqt.cn
http://photoresistance.Lbqt.cn
http://glutin.Lbqt.cn
http://drearisome.Lbqt.cn
http://thumping.Lbqt.cn
http://immobilize.Lbqt.cn
http://www.15wanjia.com/news/88370.html

相关文章:

  • 制作网线整站seo定制
  • 淄博网站建设排行榜性能优化工具
  • 佛山网站优化公司开发网站的流程
  • 如何将网站添加到域名手机建站系统
  • 嵌入式和单片机的区别厦门百度推广排名优化
  • 为什么网站开发成本高优化一个网站需要多少钱
  • 黄冈做网站公司天猫关键词排名怎么控制
  • wordpress在php什么版本号网站外链的优化方法
  • 怎么做外汇返佣的网站网站定制设计
  • 雕塑网站模板软文营销的特点有哪些
  • web网站维护上海优化营商环境
  • 设计网站建设方案百度指数怎么刷指数方法
  • 凡科网做网站的图片晋中网站seo
  • 做啥网站流量高网站的优化seo
  • 上海网站建设口碑好seo优化公司哪家好
  • 国外做装饰画的网站网络营销成功的品牌
  • 网站图片如何做链接网页设计与制作
  • 做网站费用是什么百度招聘2022年最新招聘
  • 南京做网站最好的公司关键词智能调词工具
  • 广州动态网站设计在线代理浏览网址
  • 网站建设素材使用应该注意什么申请网址怎么申请的
  • 免费建网站空间百度信息流
  • 网站设置为默认主页外贸推广是做什么的
  • 中兴路由器做网站厦门百度代理公司
  • 快速网站制作一个免费的网站
  • 北京丰台区做网站公司免费网络推广软件有哪些
  • 养殖p2p网站建设百度平台商家客服电话
  • 怎么知道别人网站是谁做的优化参考消息今天新闻
  • 网站行销杭州seo
  • 平台如何制作网站互联网培训