Use argpasrse module
import argparse
parser = argparse.ArgumentParser(
prog = 'ls',
description='Process some int',
add_help = True
)
parser.add_argument('path',nargs='?', default='.', help='file path')# 位置参数
parser.add_argument('-l',dest='list', action='store_true')
parser.add_argument('-a', '--all', action='store_true')
args = parser.parse_args() # 解析:如:/etc ---> e t c 把其当作一个可迭代对象了,所以可以这样输入 ('/etc',)
parser.print_help() # windows 使用这个调试
print('-------------------------------------')
print( args.all, args.list, args.path) #None None /etc
In win, the insertion position of the analog parameter, such as / etc can run-> configuration or the Edit: args = parser.parse_args ( '/ etc')
① default print help information provided by default '' '
py file:
'''
遇到问题没人解答?小编创建了一个Python学习交流QQ群:579817333
寻找有志同道合的小伙伴,互帮互助,群里还有不错的视频学习教程和PDF电子书!
'''
import argparse
parser = argparse.ArgumentParser(description='Process some int')
args = parser.parse_args()
parser.print_help() # windows 使用这个调试
Print information:
usage: homework_解析_9-4.py [-h]
Process some int
optional arguments:
-h, --help show this help message and exit
Contents of ② ArgumentParser
def __init__(self,
prog=None,描述程序的,sys.args[0]也就是输入的命令, 如:a.py
usage=None,程序使用方式
description=None,
epilog=None,
parents=[],
formatter_class=HelpFormatter,
prefix_chars='-',
fromfile_prefix_chars=None,
argument_default=None, 缺省值
conflict_handler='error',
add_help=True,默认的帮助信息,-h,--help Fals表示取消
allow_abbrev=True):
③ cancel help
'''
遇到问题没人解答?小编创建了一个Python学习交流QQ群:579817333
寻找有志同道合的小伙伴,互帮互助,群里还有不错的视频学习教程和PDF电子书!
'''
import argparse
parser = argparse.ArgumentParser(
prog = 'ls', # 给命令起个名字 ls
description='Process some int',
add_help = False # 改为False
)
args = parser.parse_args()
parser.print_help() # windows 使用这个调试
Print information:
usage: ls
Process some int
No cancellation information:
usage: ls [-h] # 中括号表示可选,linux特色
Process some int
optional arguments:
-h, --help show this help message and exit
④ have to provide, that is to say when the ls.py performed, such as back to keep the path: / etc
'''
遇到问题没人解答?小编创建了一个Python学习交流QQ群:579817333
寻找有志同道合的小伙伴,互帮互助,群里还有不错的视频学习教程和PDF电子书!
'''
import argparse
parser = argparse.ArgumentParser(
prog = 'ls',
description='Process some int',
add_help = True
)
parser.add_argument('path')#
Print information
args = parser.parse_args()
usage: ls [-h] path
ls: error: the following arguments are required: path
⑤ long options
import argparse
parser = argparse.ArgumentParser(
prog = 'ls',
description='Process some int',
add_help = True
)
parser.add_argument('path')
parser.add_argument('-l')
parser.add_argument('-a','--all')
args = parser.parse_args()
Print information:
usage: ls [-h] [-l L] [-a ALL] path
Process some int
positional arguments:
path
optional arguments:
-h, --help show this help message and exit
-l L
-a ALL, --all ALL
⑥ namespace, sys.argv [1:], does not provide, is None, provided, then spread namespace
'''
遇到问题没人解答?小编创建了一个Python学习交流QQ群:579817333
寻找有志同道合的小伙伴,互帮互助,群里还有不错的视频学习教程和PDF电子书!
'''
import argparse
parser = argparse.ArgumentParser(
prog = 'ls',
description='Process some int',
add_help = True
)
parser.add_argument('path')
parser.add_argument('-l')
parser.add_argument('-a','--all')
args = parser.parse_args(('/etc',)) # 解析:如:/etc ---> e t c 把其当作一个可迭代对象了,所以可以这样输入 ('/etc',)
parser.print_help() # windows 使用这个调试
print(args)
Print information:
usage: ls [-h] [-l L] [-a ALL] path
Process some int
positional arguments:
path
optional arguments:
-h, --help show this help message and exit
-l L
-a ALL, --all ALL
Namespace(all=None, l=None, path='/etc')!!!!!!!!!!!!!!!!!!
⑦ acquisition parameter corresponds to the value of the property by namespace, but -all parameter if so, can only args.all '' '
args = parser.parse_args('/etc'.split()) # 解析:如:/etc ---> e t c 把其当作一个可迭代对象了,所以可以这样输入 ('/etc',)
print( args.all, args.l, args.path) #None None /etc
Print information:
None None /etc
⑧ -l -a must take default parameters, how no parameters' ''
Help documentation:
add_argument() method
•name or flags - Either a name or a list of option strings, e.g. foo or -f, --foo.
•action - The basic type of action to be taken when this argument is encountered at the command line.
•nargs - The number of command-line arguments that should be consumed.
•const - A constant value required by some action and nargs selections.
•default - The value produced if the argument is absent from the command line.
•type - The type to which the command-line argument should be converted.
•choices - A container of the allowable values for the argument.
•required - Whether or not the command-line option may be omitted (optionals only).
•help - A brief description of what the argument does.
•dest - The name of the attribute to be added to the object returned by parse_args().
parser.add_argument('path',nargs='?')# 位置参数
打印:
usage: ls [-h] [-l L] [-a ALL] [path] 此时path 加中括号了,可有可无
parser.add_argument('path',nargs='?', default='.')# 位置参数,加了一个默认值,当前目录
#### ? 表示可有可无,+ 至少一个,*可以任意个,数字表示必须指定数目
parser.add_argument('-l', action='store_true') # 不需要传参了
Print information:
usage: ls [-h] [-l] [-a ALL] [path]
parser.add_argument('-l', action='store_true')
args = parser.parse_args('-l'.split())
print( args.all, args.l, args.path)
Print Information:
Because store_true, so give the -l print is true, otherwise false
if store_false, do not give -l to print true
parser.add_argument('-l', action='store_const', const=23)
args = parser.parse_args('-l'.split())
Print information:
If you give up, you print 23, not to print None
⑨ Each command provides a help information '' '
parser.add_argument('path',nargs='?', default='.', help='file path')# 位置参数
Print information:
path file path
⑩ namespace attribute to provide a user-friendly name, such as the use args.l args.list '' '
parser.add_argument('-l',dest='list', action='store_true')
print( args.all, args.list, args.path) #None None /etc
Print information:
False False .
The name is only used in the namespace will not affect -l command when calling