python参数之间用什么分隔
admin
2023-09-04 06:09:00

python参数之间用什么分隔

python之分割参数getopt

import sys

import getopt

def usage():

print("Usage:%s [-a|-o|-c] [--help|--output] args...." %Dsys.argv[0])

if "__main__" == __name__:

#lsArgs = [""]

try:

opts,args = getopt.getopt(sys.argv[1:], "ao:c", ["help", "output="])

print("============ opts ==================")

print(opts)

print("============ args ==================")

print(args)

#check all param

for opt,arg in opts:

if opt in ("-h", "--help"):

usage()

sys.exit(1)

elif opt in ("-t", "--test"):

print("for test option")

else:

print("%s ==> %s" %(opt, arg))

except getopt.GetoptError:

print("getopt error!")

usage()

sys.exit(1)

运行结果:

$ ./test_getopt.py -a -oaaa -caa --output=out file1 t file2 -d

============ opts ==================

[('-a', ''), ('-o', 'aaa'), ('-c', ''), ('-a', ''), ('-a', ''), ('--output', 'out')]

============ args ==================

['file1', 't', 'file2', '-d']

-a ==>

-o ==> aaa

-c ==>

-a ==>

-a ==>

--output ==> out

语音朗读:

相关内容

热门资讯

秦国书同文车同轨 秦国之前的文... 还不知道:七国文字的读者,下面趣历史小编就为大家带来详细介绍,接着往下看吧~秦国的统一,不仅仅是地域...
古代一千年前就有出租车了 古人... 今天趣历史小编给大家准备了:古代的出租车的文章,感兴趣的小伙伴们快来看看吧!古代的马车就和现代的汽车...
宋朝著名的“乌台诗... 今天趣历史小编就给大家带来苏轼的故事,希望能对大家有所帮助。宋神宗元丰二年(1079),在这一年,宋...
药师提醒:这些用药习惯赶紧改   胶囊掰开吃、果汁送药、跟风囤药?错错错!  药师提醒:这些用药习惯赶紧改胶囊较轻且无法立即溶解,...
福清多所学校课间延长至15分钟   14日,记者获悉,本学期起,福清市崇文小学、三山中心小学等福清多所中小学将课间活动时间从原来的1...