python getopt的用法

python中 getopt 模块,
该模块是专门用来处理命令行参数的
函数getopt(args, shortopts, longopts = [])
参数args一般是sys.argv[1:]
shortopts 短格式 (-)
longopts 长格式(--)
try:
options,args = getopt.getopt(sys.argv[1:],"hp:i:",["help","ip=","port="])
except getopt.GetoptError:
sys.exit()
for name,value in options:
if name in ("-h","--help"):
usage()
if name in ("-i","--ip"):
print ‘ip is----‘,value
if name in ("-p","--port")
print ‘port is----‘,value
python test.py -i 127.0.0.1 -p 80 55 66

“hp:i:”
短格式 --- h 后面没有冒号:表示后面不带参数,p:和 i:后面有冒号表示后面需要参数

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

长格式 --- help后面没有等号=,表示后面不带参数,其他三个有=,表示后面需要参数

返回值 options 是个包含元祖的列表,每个元祖是分析出来的格式信息,比如 [(‘-i‘,‘127.0.0.1‘),(‘-p‘,‘80‘)] ;
 args 是个列表,包含那些没有‘-’或‘--’的参数,比如:[‘55‘,‘66‘]

注意:定义命令行参数时,要先定义带‘-‘选项的参数,再定义没有‘-’的参数

再说说linux下getopt用法
#include <unistd.h>
int getopt(int argc,char * const argv[ ],const char * optstring);
a:b:cd::e",这就是一个选项字符串。对应到命令行就是-a ,-b ,-c ,-d, -e 。冒号又是什么呢? 冒号表示参数,一个冒号表示这个选项后面必须带参数(没有带参数会报错哦),但是这个参数可以和选项连在一起写,也可以用空格隔开,比如-a123 和-a   123(中间有空格) 都表示123是-a的参数;两个冒号表示参数可选,即可以有参数,也可以没有参数,但要注意有参数时,参数与选项之间不能有空格(有空格会报错的哦),这一点和一个冒号时是有区别的。

#include <unistd.h>
#include <stdio.h>
int main(int argc, char * argv[])
{

int ch;
printf("\n\n");
printf("optind:%d,opterr:%d\n",optind,opterr);
printf("--------------------------\n");
while ((ch = getopt(argc, argv, "ab:c:de::")) != -1)
{
printf("optind: %d\n", optind);
switch (ch)
{
case ‘a‘:
printf("HAVE option: -a\n\n");
break;
case ‘b‘:
printf("HAVE option: -b\n");
printf("The argument of -b is %s\n\n", optarg);
break;
case ‘c‘:
printf("HAVE option: -c\n");
printf("The argument of -c is %s\n\n", optarg);
break;
case ‘d‘:
printf("HAVE option: -d\n");
break;
case ‘e‘:
printf("HAVE option: -e\n");
printf("The argument of -e is %s\n\n", optarg);
break;
case ‘?‘:
printf("Unknown option: %c\n",(char)optopt);
break;
}
}

}
./a.out -a -b 123

原文地址:https://www.cnblogs.com/che1234/p/8905829.html

时间: 2024-08-11 04:40:50

python getopt的用法的相关文章

python &#160; __name__==&#39;__main__&#39; 用法

python 文件的后缀为.py,比如 name.py python 文件可以直接执行,也可以被导入.调用,比如import name; script 1: #!/bin/python # Filename:name.py if __name__=='__main__':     print 'This program is being run by itself' else:     print 'I am being imported from another module' script

python之函数用法capitalize()

# -*- coding: utf-8 -*- #python 27 #xiaodeng #python之函数用法capitalize() #capitalize() #说明:将字符串的第一个字母变成大写,其他字母变小写. ''' capitalize(...) S.capitalize() -> string Return a copy of the string S with only its first character capitalized. ''' #案例 str='xiaoden

python之函数用法setdefault()

# -*- coding: utf-8 -*- #python 27 #xiaodeng #python之函数用法setdefault() #D.get(k,d) #说明:k在D中,则返回 D[K],如果k不在D中,则返回d值 #D.get(k,d), also set D[k]=d if k not in D ''' >>> help(dict.setdefault) Help on built-in function setdefault: setdefault(...) D.set

python之函数用法islower()

# -*- coding: utf-8 -*- #python 27 #xiaodeng #python之函数用法islower() #http://www.runoob.com/python/att-string-islower.html #islower() #说明:检测字符串是否都由小写字母组成 str = "THIS is string example....wow!!!" print str.islower()#False str = "this is string

python之函数用法xrange()

# -*- coding: utf-8 -*- #python 27 #xiaodeng #python之函数用法xrange() #xrange() #说明:返回一个生成器 #xrange做循环的性能比range好,尤其是返回很大的时候.除非要返回一个列表,则用range. ''' class xrange(object) | xrange(stop) -> xrange object | xrange(start, stop[, step]) -> xrange object | | Li

python之函数用法startswith()

# -*- coding: utf-8 -*- #python 27 #xiaodeng #python之函数用法startswith() #http://www.runoob.com/python/att-string-startswith.html #startswith() #说明:返回布尔值,用于检查字符串是否是以指定子字符串开头,如果是则返回 True,否则返回 False. ''' startswith(...) S.startswith(prefix[, start[, end]]

python之函数用法globals()

# -*- coding: utf-8 -*- #python 27 #xiaodeng #python之函数用法globals() #globals() #说明:在当前作用域下,查看全局变量 ''' globals(...) globals() -> dictionary Return the dictionary containing the current scope's global variables. ''' #案例 b='xiaodeng' print globals#<buil

python之函数用法__setattr__

# -*- coding: utf-8 -*- #python 27 #xiaodeng #python之函数用法__setattr__ #http://www.cnblogs.com/hongfei/p/3858256.html #用__setattr__函数重构方法 class Fruit(): def __init__(self,color,price): self.__color = color self.__price = price def __setattr__(self,name

python之函数用法get()

# -*- coding: utf-8 -*- #python 27 #xiaodeng #python之函数用法get() #http://www.runoob.com/python/att-dictionary-get.html #dict.get(key, default=None) #说明:返回指定键的值,如果值不在字典中返回默认值. #key:要查找的键 #default:如果指定键的值不存在时,返回该默认值值 ''' >>> help(d.get) Help on built