日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > python >内容正文

python

python getopt argparse_sys.argvgetopt-argparse

發(fā)布時(shí)間:2023/12/20 python 37 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python getopt argparse_sys.argvgetopt-argparse 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

* 作者:煮酒品茶 tea

* 博客:http://www.zwhset.com http://cwtea.blog.51cto.com

* 目前在京峰教育擔(dān)任python講師

#0、命令行參數(shù)介紹

煮酒品茶:說明再補(bǔ),有事先

> sys.argv #獲取命令行參數(shù)為一個(gè)列表

> getopt # 這個(gè)模塊用于解析sys.argv的參數(shù),可以支常用的- 與 --方法。通過關(guān)鍵字取值來進(jìn)行解析。

> argparse 更復(fù)雜的命令行處理交由argparse來處理

經(jīng)常看到:

> ./webbench -c 500 -t 60 http://www.baidu.com/

> ./webbench --help

#1、sys.argv 獲取命令行傳的參數(shù)輸出為列表

~~~

#coding:utf8

import sys

print(sys.argv)

~~~

結(jié)果為列表 args[0] 為文件名,后面跟著是一個(gè)個(gè)參數(shù)。可根據(jù)相應(yīng)去取值做處理就可以。

~~~

root@zwhset-python-22-190 books]# python ar.py

['ar.py']

[root@zwhset-python-22-190 books]# python ar.py first_arg second_arg

['ar.py', 'first_arg', 'second_arg']

~~~

#2、getopt模塊

## 2.1、-參數(shù) 類unix風(fēng)格參數(shù) -c -n

~~~

In [1]: import getopt

In [2]: args = ['-a', '-b', '-cfoo', '-d', 'bar', 'a1', 'a2'] #實(shí)際為sys.argv[1:]

In [3]: optlist, args = getopt.getopt(args, 'abc:d:') #注意第二個(gè)參數(shù)的編寫

In [4]: optlist

Out[4]: [('-a', ''), ('-b', ''), ('-c', 'foo'), ('-d', 'bar')]

In [5]: args

Out[5]: ['a1', 'a2']

In [6]: dict(optlist) #對結(jié)果轉(zhuǎn)換成字典從而進(jìn)行更高級的處理

Out[6]: {'-a': '', '-b': '', '-c': 'foo', '-d': 'bar'}

~~~

## 2.2、--參數(shù) 長參 --help --nova-list

~~~

In [37]: args = ['--condition=foo', '--testing', '--output-file', 'abc.def', '-x', 'a1', 'a2']

In [38]: long_arg = [

....: ... 'condition=', 'output-file=', 'testing']

In [40]: optlist, args = getopt.getopt(args, 'x', long_arg)

In [41]: optlist

Out[41]:

[('--condition', 'foo'),

('--testing', ''),

('--output-file', 'abc.def'),

('-x', '')]

In [42]: args

Out[42]: ['a1', 'a2']

In [43]: dict(optlist)

Out[43]: {'--condition': 'foo', '--output-file': 'abc.def', '--testing': '', '-x': ''}

~~~

## 2.3 實(shí)現(xiàn) webbench 的參數(shù)功能

**代碼:**

~~~

#coding:utf8

'''

./webbench -c 500 -t 60 http://www.baidu.com/

./webbench --help

'''

import sys

import getopt

def webbench(url,client,time):

print 'webbench atack:{url},client number : {client},time:{time}'.format(url=url,

client=client,

time=time)

def webbench_help():

print 'help info:\n./webbench -c 500 -t 60 http://www.baidu.com/'

def main():

try:

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

opts = dict(opts)

if '--help' in opts:

webbench_help()

elif '-c' in opts and '-t' in opts:

webbench(url=args[0],client=opts['-c'],time=opts['-t'])

except Exception as e:

webbench_help()

if __name__ == '__main__':

main()

~~~

**結(jié)果如下:**

~~~

E:\code\test>python op.py -c 200 -t 60 http://www.zwhset.com/

webbench atack:http://www.zwhset.com/,client number : 200,time:60

E:\code\test>python op.py --help

help info:

./webbench -c 500 -t 60 http://www.baidu.com/

E:\code\test>python op.py --askdf

help info:

./webbench -c 500 -t 60 http://www.baidu.com/

~~~

煮酒品茶:argparse再補(bǔ),有事先

總結(jié)

以上是生活随笔為你收集整理的python getopt argparse_sys.argvgetopt-argparse的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。