Python命令行參數(shù)argv和argparse該如何使用
概述
運行python腳本時通過命令行方式傳入運行參數(shù)通常有以下兩種自建方式:
- sys.argv - 簡潔
- argparse - 豐富,可自定義
下面詳細說一下具體時使用
argv
# test_argv.py import sys args = sys.argv print(f'args = {args}') >>> output ➜ git:(master) python3 test_argv.py args = ['test_argv.py'] ➜ git:(master) ✗ python3 test_argv.py 1 2 3 args = ['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)⒌模簶酥尽⒏袷健㈩愋秃头秶忍匦?,常用如下?/p>
- *name_or_flag - 定義入?yún)⒚騠lag,如'-n', '--number'
- type - 指定入?yún)㈩愋?/li>
- choices - 指定入?yún)⒎秶?/li>
- default - 指定入?yún)⒛J值
- 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ù)foo parser.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 -h usage: test_argv.py [-h] [-n NUMBER] [-o {txt,csv,doc}] [-d {1,2,3,4,5,6,7,8,9}] foo test module of argparse positional arguments: foo optional 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ù)運行,結(jié)果為None ➜ git:(master) ✗ python3 test_argv.py args = Namespace(number=None, output=None) number = None output = None # 帶參數(shù)運行 ➜ git:(master) ✗ python3 test_argv.py -n 33 --output txt args = 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') # 默認參數(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該如何使用的詳細內(nèi)容,更多關(guān)于Python命令行參數(shù)argv和argparse的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Python構(gòu)建機器學習API服務(wù)的操作過程
這篇文章主要介紹了Python構(gòu)建機器學習API服務(wù)的操作過程,通過本文的指導,讀者可以學習如何使用Python構(gòu)建機器學習模型的API服務(wù),并了解到在實際應(yīng)用中需要考慮的一些關(guān)鍵問題和解決方案,從而為自己的項目提供更好的支持和服務(wù),需要的朋友可以參考下2024-04-04pyinstaller參數(shù)介紹以及總結(jié)詳解
這篇文章主要介紹了pyinstaller參數(shù)介紹以及總結(jié)詳解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-07-07python pandas庫中DataFrame對行和列的操作實例講解
今天小編就為大家分享一篇python pandas庫中DataFrame對行和列的操作實例講解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-06-06Python 基于Twisted框架的文件夾網(wǎng)絡(luò)傳輸源碼
這篇文章主要介紹了Python 基于Twisted框架的文件夾網(wǎng)絡(luò)傳輸源碼,需要的朋友可以參考下2016-08-08