国产成人精品亚洲777人妖,欧美日韩精品一区视频,最新亚洲国产,国产乱码精品一区二区亚洲

您的位置:首頁技術(shù)文章
文章詳情頁

Python命令行參數(shù)argv和argparse該如何使用

瀏覽:78日期:2022-06-28 08:54:30
概述

運(yùn)行python腳本時通過命令行方式傳入運(yùn)行參數(shù)通常有以下兩種自建方式:

sys.argv - 簡潔 argparse - 豐富,可自定義

下面詳細(xì)說一下具體時使用

argv

# test_argv.pyimport sysargs = sys.argvprint(f’args = {args}’)>>> output➜ git:(master) python3 test_argv.py args = [’test_argv.py’]➜ git:(master) ✗ python3 test_argv.py 1 2 3args = [’test_argv.py’, ’1’, ’2’, ’3’]➜ git:(master) ✗ python3 test_argv.py 1 2 3 ’hello world !’args = [’test_argv.py’, ’1’, ’2’, ’3’, ’hello world !’]

從上面可以看出,通過argv方法獲取的結(jié)果:

返回為list 第一個參數(shù)為腳本本身 如參數(shù)中間帶空格,用引號即可 argparse

argparse模塊的功能較為豐富,其核心是通過add_argument方法自定義入?yún)⒌模簶?biāo)志、格式、類型和范圍等特性,常用如下:

*name_or_flag - 定義入?yún)⒚騠lag,如’-n’, ’--number’ type - 指定入?yún)㈩愋? choices - 指定入?yún)⒎秶? default - 指定入?yún)⒛J(rèn)值 required - 指定該餐素是否不要,布爾類型 help - 參數(shù)概述

更多請參考: argparse

實例

test_argv.py

import argparse# 初始化一個parser對象parser = argparse.ArgumentParser(description=’test module of argparse’)# 指定-n/--number的參數(shù)# 類型為int# help為簡短地說明parser.add_argument( ’-n’, ’--number’, type=int, help=’args of number’)# 指定-o/--output參數(shù)# 并限制類型為:[’txt’, ’csv’, ’doc’]parser.add_argument( ’-o’, ’--output’, type=str, choices=[’txt’, ’csv’, ’doc’], help=’output method’)# 指定-d/--default參數(shù)# 并限制類型為:[’txt’, ’csv’, ’doc’]parser.add_argument( ’-d’, ’--default’, type=int, choices=[_ for _ in range(1, 10)], default=5, help=’default’)# 指定位置參數(shù)fooparser.add_argument(’foo’)args = parser.parse_args()print(f’args = {args}’)# 獲取指定參數(shù)print( f’number = {args.number}, type = {type(args.number)}n’ f’output = {args.output}, type = {type(args.output)}n’ f’default = {args.default}, type = {type(args.default)}n’ f’foo = {args.foo}, type = {type(args.foo)}’)

output

# -h - 打印help➜ git:(master) ✗ python3 test_argv.py -husage: test_argv.py [-h] [-n NUMBER] [-o {txt,csv,doc}] [-d {1,2,3,4,5,6,7,8,9}] footest module of argparsepositional arguments: foooptional arguments: -h, --help show this help message and exit -n NUMBER, --number NUMBER args of number -o {txt,csv,doc}, --output {txt,csv,doc} output method -d {1,2,3,4,5,6,7,8,9}, --default {1,2,3,4,5,6,7,8,9} default# 不帶參數(shù)運(yùn)行,結(jié)果為None➜ git:(master) ✗ python3 test_argv.py args = Namespace(number=None, output=None)number = Noneoutput = None# 帶參數(shù)運(yùn)行➜ git:(master) ✗ python3 test_argv.py -n 33 --output txtargs = Namespace(number=33, output=’txt’)number = 33, type = <class ’int’>output = txt, type = <class ’str’># 參數(shù)格式錯誤➜ git:(master) ✗ python3 test_argv.py -n str usage: test_argv.py [-h] [-n NUMBER] [-o {txt,csv,doc}]test_argv.py: error: argument -n/--number: invalid int value: ’str’➜ git:(master) ✗ python3 test_argv.py -o excel usage: test_argv.py [-h] [-n NUMBER] [-o {txt,csv,doc}]test_argv.py: error: argument -o/--output: invalid choice: ’excel’ (choose from ’txt’, ’csv’, ’doc’)# 默認(rèn)參數(shù) ➜ git:(master) ✗ python3 test_argv.py args = Namespace(default=5, number=None, output=None)number = None, type = <class ’NoneType’>output = None, type = <class ’NoneType’>output = 5, type = <class ’int’>

以上就是Python命令行參數(shù)argv和argparse該如何使用的詳細(xì)內(nèi)容,更多關(guān)于Python命令行參數(shù)argv和argparse的資料請關(guān)注好吧啦網(wǎng)其它相關(guān)文章!

標(biāo)簽: Python 編程
相關(guān)文章:
主站蜘蛛池模板: 德兴市| 舞钢市| 丰镇市| 弥渡县| 平定县| 本溪市| 铁岭市| 米脂县| 商洛市| 安远县| 扎兰屯市| 日土县| 开封县| 肥东县| 曲水县| 葵青区| 梅州市| 奉贤区| 阿合奇县| 雅江县| 千阳县| 惠水县| 武安市| 贺兰县| 永安市| 漳州市| 霍邱县| 饶河县| 兴业县| 上杭县| 高雄县| 泗阳县| 乡城县| 杂多县| 马鞍山市| 同江市| 布拖县| 兴城市| 辉南县| 望谟县| 荆门市|