网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。
一个人可以走的很快,但一群人才能走的更远!不论你是正从事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
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 :理解为对解析器内的内容进行一个合适的格式化格式,有四种格式,最常用的是
RawDescriptionHelpFormatter
和RawTextHelpFormatter
类。
- 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
- class argparse.RawTextHelpFormatter:
这东西和上面具体啥差别呢??试了一下,这里不太严谨,因为没试出啥大区别,有人知道可以和大家分享一下,不过更推荐第二种,因为…人家描述的多😂😂🤣🤣
- class argparse.ArgumentDefaultsHelpFormatter:添加默认的值的信息到每一个帮助信息的参数
- class argparse.MetavarTypeHelpFormatter:显示加入命令行
type
类型(真鸡肋啊)
网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。
一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!
…(img-0qpZZPnb-1715890102705)]
网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。
一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!