python中main()函数写法

时间:2022-10-28 14:18:56

顶顶大名的Guido van Rossum(Python之父)推荐的main写法:

#!/usr/bin/python
import sys
import getopt class Usage(Exception):
def __init__(self, msg):
self.msg = msg def main(argv=None):
if argv is None:
argv = sys.argv
try:
try:
opts, args = getopt.getopt(argv[1:], "h", ["help"])
except getopt.error, msg:
raise Usage(msg)
except Usage, err:
print >>sys.stderr, err.msg
print >>sys.stderr, "for help use --help"
return 2 if __name__ == "__main__":
sys.exit(main())

getopt模块用于抽出命令行选项和参数,也就是sys.argv。

命令行选项使得程序的参数更加灵活。支持短选项模式和长选项模式

opts, args = getopt.getopt( sys.argv[1:], shortargs, longargs )

getopt.getopt ( [命令行参数列表], '短选项', [长选项列表] )

>>> 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']

参考http://www.jb51.net/article/50067.htm

python中main()函数写法的更多相关文章

  1. python 中main函数总结

    Python使用缩进对齐组织代码的执行,所有没有缩进的代码(非函数定义和类定义),都会在载入时自动执行,这些代码,可以认为是Python的main函数. 每个文件(模块)都可以任意写一些没有缩进的代码 ...

  2. 『无为则无心』Python函数 — 25、Python中的函数

    目录 1.函数的使用 (1)定义函数 (2)调用函数 (3)使用函数的注意事项 2.函数的参数 3.实参的类型 Python函数的说明: Python中函数的应用非常广泛,前面章节中我们已经接触过多个 ...

  3. .NET中Main函数使用小技巧

    摘要:任何语言开发出来的程序,都会有一个程序入口函数,可能每个语言所使用的程序入口函数名称不一样,但是它们的作用都是一样的,都是被操作系统去调用.那么本文主要总结.NET中的程序入口函数Main使用的 ...

  4. Python中split()函数的用法及实际使用示例

    Python中split()函数,通常用于将字符串切片并转换为列表. 一.函数说明: split():语法:str.split(str="",num=string.count(st ...

  5. Python中利用函数装饰器实现备忘功能

    Python中利用函数装饰器实现备忘功能 这篇文章主要介绍了Python中利用函数装饰器实现备忘功能,同时还降到了利用装饰器来检查函数的递归.确保参数传递的正确,需要的朋友可以参考下   " ...

  6. 在C++工程中main函数之前跑代码的廉价方法(使用全局变量和全局函数)

    // test.cpp : Defines the entry point for the console application. // #include "stdafx.h" ...

  7. python中range()函数的用法

    python中range()函数可创建一个整数列表,一般用在for循环中. range()函数语法: range(start,stop[,step]) 参数说明: star: 计数从star开始.默认 ...

  8. python中注释的写法

    说明: 记录在python中注释的写法. 1.单行注释,代码行以 # 开头 # 这是一个单行注释 print('hello world') 2.多行注释,使用三个单引号,或者三个双引号将其要注释的内容 ...

  9. Python 中的函数

    学了 Python 中的数据类型,语句,接下来就来说一下 Python 中的函数,函数是结构化编程的核心.我们使用函数可以增加程序的可读性.自定义函数时使用关键字def 函数由多条语句组成.在定义函数 ...

随机推荐

  1. TabLayout和ViewPager简单实现页卡的滑动

    首先需要在当前的module中的build Gradle的 dependencies中加入以下句子 compile 'com.android.support:design:23.0.1' 因为我们用到 ...

  2. JS焦点图 上下翻动 支持IE6

    <div class="slider"> <ul class="num" id="homePushName"> &l ...

  3. linux下apache 的安装

    1.进入work目录下:cd /usr/local/work(如没有则自己新建,命令:mkdir /usr/local/work) 2.在woke目录下从网站下载apache并解压:wget http ...

  4. Freemaker配置文件详解

    classic_compatible=true              ##如果变量为null,转化为空字符串,比如做比较的时候按照空字符做比较 whitespace_stripping=true  ...

  5. getchar&lpar;&rpar;&comma;scanf&lpar;&rpar;&comma;gets&lpar;&rpar;&comma;cin&comma;输入字符串

    #include<iostream>#include<stdio.h>#include<string.h>#include<string>using n ...

  6. python&plus;selenium十三:破解简单的图形验证码

    此方法可破解简单的验证码,如: 注:中文识别正在寻找办法 安装: 1.python3 2.Pillow 3.pytesseract 4.tesseract-ocr    下载地址:https://pa ...

  7. 使用 Chef 自动执行 Azure 虚拟机部署

    Chef 是一个强大的工具,用于提供自动化和所需的状态配置. 使用我们的最新 cloud-api 版本,Chef 提供了与 Azure 的无缝集成,使得你能够通过单个命令设置和部署配置状态. 在本文中 ...

  8. Vista&sol;Win7以上系统查看和清除本地DNS缓存新方法

    你是否因修改网站DNS解析后,却因本机DNS缓存而需要等待... 你是否遇到修改了本机的hosts文件后,必须重起firefox和ie才起作用... 其实只要清空DNS缓存这些问题都可以解决. 查看D ...

  9. 部署Centos7

    挂载和导入镜像 mount /dev/cdrom /media ll /media/ cobbler import --path=/media --name=centos7.4 --arch=x86_ ...

  10. Neighbor Discovery Protocol Address Resolution Protocol

    https://en.wikipedia.org/wiki/Address_Resolution_Protocol The Address Resolution Protocol (ARP) is a ...