search
HomeBackend DevelopmentPython Tutorialpython中argparse模块用法实例详解

本文实例讲述了python中argparse模块用法。分享给大家供大家参考。具体分析如下:

平常在写命令行工具的时候,经常会带参数,所以用python中的argparse来实现。

# -*- coding: utf-8 -*- 
import argparse 
args = "-f hello.txt -n 1 2 3 -x 100 -y b -z a -q hello @args.txt i_am_bar -h".split()  
# 使用@args.txt要求fromfile_prefix_chars="@" 
# args.txt文件中应该一行一个参数,想改变行为参考convert_arg_line_to_args()
# ArgumentParser参数的简单说明 
## description - 命令行帮助的开始文字,大部分情况下,我们只会用到这个参数 
# epilog - 命令行帮助的结尾文字 
# prog - (default: sys.argv[0])程序的名字,一般不需要修改,另外,如果你需要在help中使用到程序的名字,可以使用%(prog)s 
# prefix_chars - 命令的前缀,默认是-,例如-f/--file。有些程序可能希望支持/f这样的选项,可以使用prefix_chars="/" 
# fromfile_prefix_chars - (default: None)如果你希望命令行参数可以从文件中读取,就可能用到。例如,如果fromfile_prefix_chars='@',命令行参数中有一个为"@args.txt",args.txt的内容会作为命令行参数 
# add_help - 是否增加-h/-help选项 (default: True),一般help信息都是必须的,所以不用设置啦。 
## parents - 类型是list,如果这个parser的一些选项跟其他某些parser的选项一样,可以用parents来实现继承,例如parents=[parent_parser] 
## formatter_class - 自定义帮助信息的格式(description和epilog)。默认情况下会将长的帮助信息进行<自动换行和消除多个连续空白>。 
#三个允许的值: 
# class argparse.RawDescriptionHelpFormatter 直接输出description和epilog的原始形式(不进行自动换行和消除空白的操作) 
# class argparse.RawTextHelpFormatter 直接输出description和epilog以及add_argument中的help字符串的原始形式(不进行自动换行和消除空白的操作) 
## class argparse.ArgumentDefaultsHelpFormatter 在每个选项的帮助信息后面输出他们对应的缺省值,如果有设置的话。这个最常用吧! 
# argument_default - (default: None)设置一个全局的选项的缺省值,一般每个选项单独设置,所以这个参数用得少,不细说 
# usage - (default: generated)如果你需要修改usage的信息(usage: PROG [-h] [--foo [FOO]] bar [bar ...]),那么可以修改这个,一般不要修改。 
# conflict_handler - 不建议使用。这个在极端情况下才会用到,主要是定义两个add_argument中添加的选项的名字发生冲突时怎么处理,默认处理是抛出异常。 
#注释一行有##表示这几个参数比较常用 
parser = argparse.ArgumentParser(description="This is a description of %(prog)s", epilog="This is a epilog of %(prog)s", prefix_chars="-+", fromfile_prefix_chars="@", formatter_class=argparse.ArgumentDefaultsHelpFormatter) 
# ArgumentParser.add_argument(name or flags...[, action][, nargs][, const][, default][, type][, choices][, required][, help][, metavar][, dest]) 
# add_argument的参数是比较复杂的。。。 
# name or flags - 指定参数的形式,想写几个写几个,不过我们一般就写两个,一个短参数,一个长参数,看下面的例子"-f", "--file" 
# 可选的选项,位置不固定,想怎么写就怎么写,默认是可选的 
parser.add_argument("-f", "--file", help="test test test") 
# 位置固定的选项,例如"prog i_am_bar",这样子的话,i_am_bar就是bar选项的值啦,默认是必须有的 
parser.add_argument("bar", help="test test test") 
# nargs - 指定这个参数后面的value有多少个,例如,我们希望使用-n 1 2 3 4,来设置n的值为[1, 2, 3, 4] 
parser.add_argument("-n", "--num", nargs="+", type=int) 
# 这里nargs="+"表示,如果你指定了-n选项,那么-n后面至少要跟一个参数,+表示至少一个,&#63;表示一个或0个,*0个或多个, 
# default - 如果命令行没有出现这个选项,那么使用default指定的默认值 
parser.add_argument("+g", "++gold", help="test test test", default="test_gold")#需要prefix_chars包含"+" 
# type - 如果希望传进来的参数是指定的类型(例如 float, int or file等可以从字符串转化过来的类型),可以使用 
parser.add_argument("-x", type=int) 
# choices - 设置参数值的范围,如果choices中的类型不是字符串,记得指定type哦 
parser.add_argument("-y", choices=['a', 'b', 'd']) 
# required - 通常-f这样的选项是可选的,但是如果required=True那么就是必须的了 
parser.add_argument("-z", choices=['a', 'b', 'd'], required=True) 
# metavar - 参数的名字,在显示 帮助信息时才用到. 
parser.add_argument("-o", metavar="OOOOOO") 
# help - 设置这个选项的帮助信息 
# dest - 设置这个选项的值就是解析出来后放到哪个属性中 
parser.add_argument("-q", dest="world")  
args = parser.parse_args(args) # 如果你没有args参数,那么就使用sys.argv,也就是命令行参数啦。有这个参数,就方便我们调试啊 
# args.world就是-q的值啦 
# action - The basic type of action to be taken when this argument is encountered at the command line. 
# const - A constant value required by some action and nargs selections.
# 这两个自己看帮助文档啦,比较复杂 
# http://docs.python.org/library/argparse.html 
print args 

希望本文所述对大家的Python程序设计有所帮助。

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Python: Automation, Scripting, and Task ManagementPython: Automation, Scripting, and Task ManagementApr 16, 2025 am 12:14 AM

Python excels in automation, scripting, and task management. 1) Automation: File backup is realized through standard libraries such as os and shutil. 2) Script writing: Use the psutil library to monitor system resources. 3) Task management: Use the schedule library to schedule tasks. Python's ease of use and rich library support makes it the preferred tool in these areas.

Python and Time: Making the Most of Your Study TimePython and Time: Making the Most of Your Study TimeApr 14, 2025 am 12:02 AM

To maximize the efficiency of learning Python in a limited time, you can use Python's datetime, time, and schedule modules. 1. The datetime module is used to record and plan learning time. 2. The time module helps to set study and rest time. 3. The schedule module automatically arranges weekly learning tasks.

Python: Games, GUIs, and MorePython: Games, GUIs, and MoreApr 13, 2025 am 12:14 AM

Python excels in gaming and GUI development. 1) Game development uses Pygame, providing drawing, audio and other functions, which are suitable for creating 2D games. 2) GUI development can choose Tkinter or PyQt. Tkinter is simple and easy to use, PyQt has rich functions and is suitable for professional development.

Python vs. C  : Applications and Use Cases ComparedPython vs. C : Applications and Use Cases ComparedApr 12, 2025 am 12:01 AM

Python is suitable for data science, web development and automation tasks, while C is suitable for system programming, game development and embedded systems. Python is known for its simplicity and powerful ecosystem, while C is known for its high performance and underlying control capabilities.

The 2-Hour Python Plan: A Realistic ApproachThe 2-Hour Python Plan: A Realistic ApproachApr 11, 2025 am 12:04 AM

You can learn basic programming concepts and skills of Python within 2 hours. 1. Learn variables and data types, 2. Master control flow (conditional statements and loops), 3. Understand the definition and use of functions, 4. Quickly get started with Python programming through simple examples and code snippets.

Python: Exploring Its Primary ApplicationsPython: Exploring Its Primary ApplicationsApr 10, 2025 am 09:41 AM

Python is widely used in the fields of web development, data science, machine learning, automation and scripting. 1) In web development, Django and Flask frameworks simplify the development process. 2) In the fields of data science and machine learning, NumPy, Pandas, Scikit-learn and TensorFlow libraries provide strong support. 3) In terms of automation and scripting, Python is suitable for tasks such as automated testing and system management.

How Much Python Can You Learn in 2 Hours?How Much Python Can You Learn in 2 Hours?Apr 09, 2025 pm 04:33 PM

You can learn the basics of Python within two hours. 1. Learn variables and data types, 2. Master control structures such as if statements and loops, 3. Understand the definition and use of functions. These will help you start writing simple Python programs.

How to teach computer novice programming basics in project and problem-driven methods within 10 hours?How to teach computer novice programming basics in project and problem-driven methods within 10 hours?Apr 02, 2025 am 07:18 AM

How to teach computer novice programming basics within 10 hours? If you only have 10 hours to teach computer novice some programming knowledge, what would you choose to teach...

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.