比较详细的argpasrse模块的基本使用

  1 '''
  2 argpasrse模块的使用!
  3 '''
  4 import  argparse
  5 parser = argparse.ArgumentParser(
  6     prog = 'ls',
  7     description='Process some int',
  8     add_help = True
  9 )
 10 parser.add_argument('path',nargs='?', default='.', help='file path')# 位置参数
 11 parser.add_argument('-l',dest='list', action='store_true')
 12 parser.add_argument('-a', '--all', action='store_true')
 13 
 14 
 15 args = parser.parse_args() # 解析:如:/etc ---> e t c  把其当作一个可迭代对象了,所以可以这样输入 ('/etc',)
 16 parser.print_help() # windows  使用这个调试
 17 print('-------------------------------------')
 18 print( args.all, args.list, args.path) #None None /etc
 19 
 20 
 21 '''在win下,模拟插入位置参数,如 /etc  可以在run-->Edit configuration下
 22     或者在:args = parser.parse_args('/etc')
 23 '''
 24 ''' ①   默认会打印帮助信息,默认提供 '''
 25 '''
 26 py文件:
 27 import  argparse
 28 parser = argparse.ArgumentParser(description='Process some int')
 29 args = parser.parse_args()
 30 parser.print_help() # windows  使用这个调试
 31 
 32 打印信息:
 33 usage: homework_解析_9-4.py [-h]
 34 
 35 Process some int
 36 
 37 optional arguments:
 38   -h, --help  show this help message and exit
 39 '''
 40 ''' ② ArgumentParser下的内容   '''
 41 '''
 42 def __init__(self,
 43                  prog=None,描述程序的,sys.args[0]也就是输入的命令, 如:a.py
 44                  usage=None,程序使用方式
 45                  description=None,
 46                  epilog=None,
 47                  parents=[],
 48                  formatter_class=HelpFormatter,
 49                  prefix_chars='-',
 50                  fromfile_prefix_chars=None,
 51                  argument_default=None, 缺省值
 52                  conflict_handler='error',
 53                  add_help=True,默认的帮助信息,-h,--help Fals表示取消
 54                  allow_abbrev=True):
 55 '''
 56 ''' ③ 取消help '''
 57 '''
 58 import  argparse
 59 parser = argparse.ArgumentParser(
 60     prog = 'ls',  # 给命令起个名字 ls
 61     description='Process some int',
 62     add_help = False # 改为False
 63 )
 64 args = parser.parse_args()
 65 parser.print_help() # windows  使用这个调试
 66 
 67 打印信息:
 68 usage: ls
 69 
 70 Process some int
 71 没有取消的信息:
 72 usage: ls [-h] # 中括号表示可选,linux特色
 73 
 74 Process some int
 75 
 76 optional arguments:
 77   -h, --help  show this help message and exit
 78 
 79 '''
 80 ''' ④ 必须提供参数,也就是说执行ls.py的时候,后面要跟 路径 如:/etc '''
 81 '''
 82 import  argparse
 83 parser = argparse.ArgumentParser(
 84     prog = 'ls',
 85     description='Process some int',
 86     add_help = True
 87 )
 88 parser.add_argument('path')# 
 89 
 90 打印信息
 91 args = parser.parse_args()
 92 usage: ls [-h] path
 93 ls: error: the following arguments are required: path
 94 '''
 95 
 96 ''' ⑤ 长选项'''
 97 '''
 98 import  argparse
 99 parser = argparse.ArgumentParser(
100     prog = 'ls',
101     description='Process some int',
102     add_help = True
103 )
104 parser.add_argument('path')
105 parser.add_argument('-l')
106 parser.add_argument('-a','--all')
107 
108 args = parser.parse_args()
109 打印信息:
110 usage: ls [-h] [-l L] [-a ALL] path
111 
112 Process some int
113 
114 positional arguments:
115   path
116 
117 optional arguments:
118   -h, --help         show this help message and exit
119   -l L
120   -a ALL, --all ALL
121 '''
122 ''' ⑥ namespace,sys.argv[1:],没有提供 ,就是None,提供了,则传到namespace'''
123 '''
124 import  argparse
125 parser = argparse.ArgumentParser(
126     prog = 'ls',
127     description='Process some int',
128     add_help = True
129 )
130 parser.add_argument('path')
131 parser.add_argument('-l')
132 parser.add_argument('-a','--all')
133 
134 args = parser.parse_args(('/etc',)) # 解析:如:/etc ---> e t c  把其当作一个可迭代对象了,所以可以这样输入 ('/etc',)
135 parser.print_help() # windows  使用这个调试
136 print(args)
137 打印信息:
138 usage: ls [-h] [-l L] [-a ALL] path
139 
140 Process some int
141 
142 positional arguments:
143   path
144 
145 optional arguments:
146   -h, --help         show this help message and exit
147   -l L
148   -a ALL, --all ALL
149 Namespace(all=None, l=None, path='/etc')!!!!!!!!!!!!!!!!!!
150 '''
151 ''' ⑦ 获取参数对应的属性的值 ,通过namespace,但是参数如果有--all这样的,只能用args.all'''
152 '''
153 args = parser.parse_args('/etc'.split()) # 解析:如:/etc ---> e t c  把其当作一个可迭代对象了,所以可以这样输入 ('/etc',)
154 print( args.all, args.l, args.path) #None None /etc
155 打印信息:
156 None None /etc
157 
158 '''
159 
160 ''' ⑧ -l -a 默认必须带参,如何不带参'''
161 '''
162 帮助文档:
163     add_argument() method
164          •name or flags - Either a name or a list of option strings, e.g. foo or -f, --foo.
165          •action - The basic type of action to be taken when this argument is encountered at the command line.
166          •nargs - The number of command-line arguments that should be consumed.
167          •const - A constant value required by some action and nargs selections.
168          •default - The value produced if the argument is absent from the command line.
169          •type - The type to which the command-line argument should be converted.
170          •choices - A container of the allowable values for the argument.
171          •required - Whether or not the command-line option may be omitted (optionals only).
172          •help - A brief description of what the argument does.
173          •dest - The name of the attribute to be added to the object returned by parse_args().
174 parser.add_argument('path',nargs='?')# 位置参数
175 打印:
176     usage: ls [-h] [-l L] [-a ALL] [path] 此时path 加中括号了,可有可无
177 
178 parser.add_argument('path',nargs='?', default='.')# 位置参数,加了一个默认值,当前目录
179 
180 #### ? 表示可有可无,+ 至少一个,*可以任意个,数字表示必须指定数目
181 
182 parser.add_argument('-l', action='store_true') # 不需要传参了
183 打印信息:
184     usage: ls [-h] [-l] [-a ALL] [path]  
185     
186  
187     
188 parser.add_argument('-l', action='store_true')
189 args = parser.parse_args('-l'.split()) 
190 print( args.all, args.l, args.path) 
191 打印信息:
192     因为store_true,所以给了 -l 则打印true,否则为 false
193     如果store_false,不给-l 就打印true
194     
195 parser.add_argument('-l', action='store_const', const=23)
196 args = parser.parse_args('-l'.split()) 
197 打印信息:
198     如果给了,就打印 23,不给,打印None
199 '''
200 ''' ⑨ 每个命令提供一个help信息 '''
201 '''
202 parser.add_argument('path',nargs='?', default='.', help='file path')# 位置参数
203 打印信息:
204       path        file path
205       
206       
207 '''
208 ''' ⑩ 给namespace的属性提供一个方便使用的名字,如args.l  使用args.list'''
209 '''
210 parser.add_argument('-l',dest='list', action='store_true')
211 print( args.all, args.list, args.path) #None None /etc
212 打印信息:
213     False False . 
214 这个名字只是在namespace中用,不会影响命令调用时的 -l
215 '''
为什么要坚持,想一想当初!
原文地址:https://www.cnblogs.com/JerryZao/p/9601293.html