还有人不知道ArgumentParser命令行解析器——我不允许_hfargumentparser

img
img

网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。

需要这份系统化的资料的朋友,可以添加戳这里获取

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!

+ [description](#description_90)
+ [epilog](#epilog_110)
+ [parents](#parents_129)
+ [formatter\_class](#formatter_class_155)
+ [prefix\_chars](#prefix_chars_208)
+ [fromfile\_prefix\_chars](#fromfile_prefix_chars_216)
+ [allow\_abbrev](#allow_abbrev_232)
+ [conflict\_handler](#conflict_handler_245)
+ [add\_help](#add_help_265)
+ [exit\_on\_error](#exit_on_error_269)

1 前言

没想到我会在这上面出差错,结果框框报错,找别的文章就甩一段翻译😂😂我还真没看明白,今天直接上实战搞懂他。

官方文档传送门


2 上才艺——ArgumentParser()

class argparse.ArgumentParser(prog=None, usage=None, description=None, 
	 epilog=None, parents=[], formatter_class=argparse.HelpFormatter, 
	 prefix_chars='-',fromfile_prefix_chars=None,
	 argument_default=None, conflict_handler='error', 
	 add_help=True, allow_abbrev=True, exit_on_error=True)¶

prog

  • prog:解释器名字,默认状态下为文件名,也就是说你可选择不输入。
import argparse
parser = argparse.ArgumentParser(prog='Program')
parser.print_help()
parser = argparse.ArgumentParser()
parser.print_help()


结果:

usage: Program [-h]

Process Test

optional arguments:
  -h, --help show this help message and exit
#——————————————————————————————————————————————————————————————————————————
usage: Add_command_Function.py [-h]

optional arguments:
  -h, --help show this help message and exit

usage

  • usage:描述程序用途的字符串,默认情况下他会输出[-h],如果你加上add_argument函数,他也会显示
parser = argparse.ArgumentParser(prog='Program',usage="我是usage,而且我还也可以用%(prog)s覆盖prog", description="Process Test")
parser.print_help()

parser = argparse.ArgumentParser(prog='Program', description="Process Test")
parser.add_argument("-f",help="first thing")
parser.print_help()

usage: 我是usage,而且我还也可以用Program覆盖prog

Process Test

optional arguments:
  -h, --help show this help message and exit
#----------------------------------------------------------------------------
usage: Program [-h] [-f F]

Process Test

optional arguments:
  -h, --help show this help message and exit
  -f F        first thing

description

  • description:当你使用命令行不加参数时就会出现它

命令行输入python Add_command_Function.py

parser = argparse.ArgumentParser( description="大家好,我是description")
parser.print_help()

输出:

usage: Add_command_Function.py [-h] [-f F]

大家好,我是description

optional arguments:
  -h, --help  show this help message and exit


epilog

  • epilog:在参数帮助文档之后显示的文本
parser = argparse.ArgumentParser( description="大家好,我是description",epilog="我是结尾哦")
parser.print_help()

结果:

usage: Add_command_Function.py [-h]

大家好,我是description

optional arguments:
  -h, --help  show this help message and exit

我是结尾哦

parents

👇👇传送至add_help

  • parents :理解为单个解析器通过此值来引用其他ArgumentParser 对象列表加到自己里面
parser = argparse.ArgumentParser( description="大家好,我是description",epilog="我是结尾哦",add_help=False)
parser.add_argument("-f",help="first thing",type=int)

Two_parser = argparse.ArgumentParser(parents=[parser])
Two_parser.add_argument("-s",help="first thing")
Two_parser.parse_args(['-f', '2'])

#命令行 : python Add\_command\_Function.py -s

结果:

usage: Add_command_Function.py [-f F]

大家好,我是description

optional arguments:
  -f F  first thing

我是结尾哦

formatter_class

  • formatter_class :理解为对解析器内的内容进行一个合适的格式化格式,有四种格式,最常用的是RawDescriptionHelpFormatterRawTextHelpFormatter类。
  1. class argparse.RawDescriptionHelpFormatter:允许使用格式,默认情况下不会有换行
parser = argparse.ArgumentParser( description=''' 
 this description
 was indented weird
 but that is okay''',epilog="我是结尾哦",add_help=False,formatter_class=argparse.RawDescriptionHelpFormatter)
parser.add_argument("-f",help="first thing",type=int)

parser.print_help()
#usage: Add\_command\_Function.py [-f F]

# this description
# was indented weird
# but that is okay

  1. class argparse.RawTextHelpFormatter:

这东西和上面具体啥差别呢??试了一下,这里不太严谨,因为没试出啥大区别,有人知道可以和大家分享一下,不过更推荐第二种,因为…人家描述的多😂😂🤣🤣

  1. class argparse.ArgumentDefaultsHelpFormatter:添加默认的值的信息到每一个帮助信息的参数
  2. class argparse.MetavarTypeHelpFormatter:显示加入命令行type类型(真鸡肋啊)

img
img

网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。

需要这份系统化的资料的朋友,可以添加戳这里获取

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!

…(img-0qpZZPnb-1715890102705)]

网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。

需要这份系统化的资料的朋友,可以添加戳这里获取

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!

  • 17
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值