Python学习之命令行参数解析之argparse和optparse

Python 有两个内建的模块用于处理命令行参数:
  一个是 getopt只能简单处理 命令行参数;
另一个是 optparse,它功能强大,而且易于使用,可以方便地生成标准的、符合Unix/Posix 规范的命令行说明。会自动帮你负责-h帮助选项。
更高级的可以使用argparse这个模块,argparse从python2.7开始被加入标准库. 后续版本都不在维护 optparse 了.

Python command line parsing

https://code.google.com/archive/p/argparse/

Python command line parsing

The argparse module is now part of the Python standard library!
All new development on argparse will continue in the Python repository. Get Python 2.7 or 3.2 for the latest argparse updates!

Additionally, there is an effort to maintain an argparse package here: * it is based on the python 2.7 stdlib code, backported to older python versions * provides argparse for everybody who still needs to support python < 2.7 * provides argparse >= 1.2 under Python license to fix license compatibility issues (e.g. Apache License 2.0 is considered incompatible to GPL v2) * fix other issues as they come up without forcing people to use python 2.7

argparse: Python command line parser
The argparse module provides an easy, declarative interface for creating command line tools, which knows how to:

parse the arguments and flags from sys.argv
convert arg strings into objects for your program
format and print informative help messages
and much more...
The argparse module improves on the standard library optparse module in a number of ways including:

handling positional arguments
supporting sub-commands
allowing alternative option prefixes like + and /
handling zero-or-more and one
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值