##// END OF EJS Templates
Simplify options handling code by using argparse argument_default....
Fernando Perez -
Show More
@@ -297,6 +297,8 b' NoConfigDefault = __NoConfigDefault()'
297 297
298 298
299 299 class ArgParseConfigLoader(CommandLineConfigLoader):
300 #: Global default for arguments (see argparse docs for details)
301 argument_default = NoConfigDefault
300 302
301 303 def __init__(self, argv=None, arguments=(), *args, **kw):
302 304 """Create a config loader for use with argparse.
@@ -322,7 +324,9 b' class ArgParseConfigLoader(CommandLineConfigLoader):'
322 324 self.argv = argv
323 325 self.arguments = arguments
324 326 self.args = args
325 self.kw = kw
327 kwargs = dict(argument_default=self.argument_default)
328 kwargs.update(kw)
329 self.kw = kwargs
326 330
327 331 def load_config(self, args=None):
328 332 """Parse command line arguments and return as a Struct.
@@ -353,14 +357,13 b' class ArgParseConfigLoader(CommandLineConfigLoader):'
353 357 self._add_arguments()
354 358 self._add_other_arguments()
355 359
356 def _add_other_arguments(self):
357 pass
358
359 360 def _add_arguments(self):
360 361 for argument in self.arguments:
361 argument[1].setdefault('default', NoConfigDefault)
362 362 self.parser.add_argument(*argument[0],**argument[1])
363 363
364 def _add_other_arguments(self):
365 pass
366
364 367 def _parse_args(self, args):
365 368 """self.parser->self.parsed_data"""
366 369 self.parsed_data, self.extra_args = self.parser.parse_known_args(args)
@@ -39,7 +39,6 b' from IPython.config.loader import ('
39 39 PyFileConfigLoader,
40 40 ArgParseConfigLoader,
41 41 Config,
42 NoConfigDefault
43 42 )
44 43
45 44 #-----------------------------------------------------------------------------
@@ -57,7 +56,6 b' app_cl_args = ('
57 56 """Set to override default location of the IPython directory
58 57 IPYTHON_DIR, stored as Global.ipython_dir. This can also be specified
59 58 through the environment variable IPYTHON_DIR.""",
60 default=NoConfigDefault,
61 59 metavar='Global.ipython_dir') ),
62 60 (('-p', '--profile',), dict(
63 61 dest='Global.profile',type=unicode,
@@ -69,12 +67,10 b' app_cl_args = ('
69 67 in your more specialized ones. You can keep a basic
70 68 IPYTHON_DIR/ipython_config.py file and then have other 'profiles' which
71 69 include this one and load extra things for particular tasks.""",
72 default=NoConfigDefault,
73 70 metavar='Global.profile') ),
74 71 (('--log-level',), dict(
75 72 dest="Global.log_level",type=int,
76 73 help='Set the log level (0,10,20,30,40,50). Default is 30.',
77 default=NoConfigDefault,
78 74 metavar='Global.log_level')),
79 75 (('--config-file',), dict(
80 76 dest='Global.config_file',type=unicode,
@@ -84,7 +80,6 b' app_cl_args = ('
84 80 IPYTHON_DIR/ipython_config.py. If the loading of your config file
85 81 fails, IPython starts with a bare bones configuration (no modules
86 82 loaded at all).""",
87 default=NoConfigDefault,
88 83 metavar='Global.config_file')),
89 84 )
90 85
@@ -28,15 +28,12 b' import os'
28 28 import sys
29 29
30 30 from IPython.core import crashhandler
31 from IPython.core import release
32 31 from IPython.core.application import Application
33 from IPython.core.error import UsageError
34 32 from IPython.core.iplib import InteractiveShell
35 from IPython.core.pylabtools import pylab_activate
36 33 from IPython.config.loader import (
37 NoConfigDefault,
38 34 Config,
39 PyFileConfigLoader
35 PyFileConfigLoader,
36 # NoConfigDefault,
40 37 )
41 38 from IPython.lib import inputhook
42 39 from IPython.utils.genutils import filefind, get_ipython_dir
@@ -50,7 +47,7 b" default_config_file_name = u'ipython_config.py'"
50 47
51 48 cl_args = (
52 49 (('--autocall',), dict(
53 type=int, dest='InteractiveShell.autocall', default=NoConfigDefault,
50 type=int, dest='InteractiveShell.autocall',
54 51 help=
55 52 """Make IPython automatically call any callable object even if you
56 53 didn't type explicit parentheses. For example, 'str 43' becomes
@@ -63,47 +60,39 b' cl_args = ('
63 60 ),
64 61 (('--autoindent',), dict(
65 62 action='store_true', dest='InteractiveShell.autoindent',
66 default=NoConfigDefault,
67 63 help='Turn on autoindenting.')
68 64 ),
69 65 (('--no-autoindent',), dict(
70 66 action='store_false', dest='InteractiveShell.autoindent',
71 default=NoConfigDefault,
72 67 help='Turn off autoindenting.')
73 68 ),
74 69 (('--automagic',), dict(
75 70 action='store_true', dest='InteractiveShell.automagic',
76 default=NoConfigDefault,
77 71 help='Turn on the auto calling of magic commands.'
78 72 'Type %%magic at the IPython prompt for more information.')
79 73 ),
80 74 (('--no-automagic',), dict(
81 75 action='store_false', dest='InteractiveShell.automagic',
82 default=NoConfigDefault,
83 76 help='Turn off the auto calling of magic commands.')
84 77 ),
85 78 (('--autoedit-syntax',), dict(
86 79 action='store_true', dest='InteractiveShell.autoedit_syntax',
87 default=NoConfigDefault,
88 80 help='Turn on auto editing of files with syntax errors.')
89 81 ),
90 82 (('--no-autoedit-syntax',), dict(
91 83 action='store_false', dest='InteractiveShell.autoedit_syntax',
92 default=NoConfigDefault,
93 84 help='Turn off auto editing of files with syntax errors.')
94 85 ),
95 86 (('--banner',), dict(
96 87 action='store_true', dest='Global.display_banner',
97 default=NoConfigDefault,
98 88 help='Display a banner upon starting IPython.')
99 89 ),
100 90 (('--no-banner',), dict(
101 91 action='store_false', dest='Global.display_banner',
102 default=NoConfigDefault,
103 92 help="Don't display a banner upon starting IPython.")
104 93 ),
105 94 (('--cache-size',), dict(
106 type=int, dest='InteractiveShell.cache_size', default=NoConfigDefault,
95 type=int, dest='InteractiveShell.cache_size',
107 96 help=
108 97 """Set the size of the output cache. The default is 1000, you can
109 98 change it permanently in your config file. Setting it to 0 completely
@@ -115,17 +104,16 b' cl_args = ('
115 104 metavar='InteractiveShell.cache_size')
116 105 ),
117 106 (('--classic',), dict(
118 action='store_true', dest='Global.classic', default=NoConfigDefault,
107 action='store_true', dest='Global.classic',
119 108 help="Gives IPython a similar feel to the classic Python prompt.")
120 109 ),
121 110 (('--colors',), dict(
122 type=str, dest='InteractiveShell.colors', default=NoConfigDefault,
111 type=str, dest='InteractiveShell.colors',
123 112 help="Set the color scheme (NoColor, Linux, and LightBG).",
124 113 metavar='InteractiveShell.colors')
125 114 ),
126 115 (('--color-info',), dict(
127 116 action='store_true', dest='InteractiveShell.color_info',
128 default=NoConfigDefault,
129 117 help=
130 118 """IPython can display information about objects via a set of func-
131 119 tions, and optionally can use colors for this, syntax highlighting
@@ -140,12 +128,10 b' cl_args = ('
140 128 ),
141 129 (('--no-color-info',), dict(
142 130 action='store_false', dest='InteractiveShell.color_info',
143 default=NoConfigDefault,
144 131 help="Disable using colors for info related things.")
145 132 ),
146 133 (('--confirm-exit',), dict(
147 134 action='store_true', dest='InteractiveShell.confirm_exit',
148 default=NoConfigDefault,
149 135 help=
150 136 """Set to confirm when you try to exit IPython with an EOF (Control-D
151 137 in Unix, Control-Z/Enter in Windows). By typing 'exit', 'quit' or
@@ -155,12 +141,10 b' cl_args = ('
155 141 ),
156 142 (('--no-confirm-exit',), dict(
157 143 action='store_false', dest='InteractiveShell.confirm_exit',
158 default=NoConfigDefault,
159 144 help="Don't prompt the user when exiting.")
160 145 ),
161 146 (('--deep-reload',), dict(
162 147 action='store_true', dest='InteractiveShell.deep_reload',
163 default=NoConfigDefault,
164 148 help=
165 149 """Enable deep (recursive) reloading by default. IPython can use the
166 150 deep_reload module which reloads changes in modules recursively (it
@@ -174,52 +158,45 b' cl_args = ('
174 158 ),
175 159 (('--no-deep-reload',), dict(
176 160 action='store_false', dest='InteractiveShell.deep_reload',
177 default=NoConfigDefault,
178 161 help="Disable deep (recursive) reloading by default.")
179 162 ),
180 163 (('--editor',), dict(
181 type=str, dest='InteractiveShell.editor', default=NoConfigDefault,
164 type=str, dest='InteractiveShell.editor',
182 165 help="Set the editor used by IPython (default to $EDITOR/vi/notepad).",
183 166 metavar='InteractiveShell.editor')
184 167 ),
185 168 (('--log','-l'), dict(
186 169 action='store_true', dest='InteractiveShell.logstart',
187 default=NoConfigDefault,
188 170 help="Start logging to the default log file (./ipython_log.py).")
189 171 ),
190 172 (('--logfile','-lf'), dict(
191 type=unicode, dest='InteractiveShell.logfile', default=NoConfigDefault,
173 type=unicode, dest='InteractiveShell.logfile',
192 174 help="Start logging to logfile with this name.",
193 175 metavar='InteractiveShell.logfile')
194 176 ),
195 177 (('--log-append','-la'), dict(
196 178 type=unicode, dest='InteractiveShell.logappend',
197 default=NoConfigDefault,
198 179 help="Start logging to the given file in append mode.",
199 180 metavar='InteractiveShell.logfile')
200 181 ),
201 182 (('--pdb',), dict(
202 183 action='store_true', dest='InteractiveShell.pdb',
203 default=NoConfigDefault,
204 184 help="Enable auto calling the pdb debugger after every exception.")
205 185 ),
206 186 (('--no-pdb',), dict(
207 187 action='store_false', dest='InteractiveShell.pdb',
208 default=NoConfigDefault,
209 188 help="Disable auto calling the pdb debugger after every exception.")
210 189 ),
211 190 (('--pprint',), dict(
212 191 action='store_true', dest='InteractiveShell.pprint',
213 default=NoConfigDefault,
214 192 help="Enable auto pretty printing of results.")
215 193 ),
216 194 (('--no-pprint',), dict(
217 195 action='store_false', dest='InteractiveShell.pprint',
218 default=NoConfigDefault,
219 196 help="Disable auto auto pretty printing of results.")
220 197 ),
221 198 (('--prompt-in1','-pi1'), dict(
222 type=str, dest='InteractiveShell.prompt_in1', default=NoConfigDefault,
199 type=str, dest='InteractiveShell.prompt_in1',
223 200 help=
224 201 """Set the main input prompt ('In [\#]: '). Note that if you are using
225 202 numbered prompts, the number is represented with a '\#' in the string.
@@ -231,7 +208,7 b' cl_args = ('
231 208 metavar='InteractiveShell.prompt_in1')
232 209 ),
233 210 (('--prompt-in2','-pi2'), dict(
234 type=str, dest='InteractiveShell.prompt_in2', default=NoConfigDefault,
211 type=str, dest='InteractiveShell.prompt_in2',
235 212 help=
236 213 """Set the secondary input prompt (' .\D.: '). Similar to the previous
237 214 option, but used for the continuation prompts. The special sequence
@@ -242,27 +219,24 b' cl_args = ('
242 219 metavar='InteractiveShell.prompt_in2')
243 220 ),
244 221 (('--prompt-out','-po'), dict(
245 type=str, dest='InteractiveShell.prompt_out', default=NoConfigDefault,
222 type=str, dest='InteractiveShell.prompt_out',
246 223 help="Set the output prompt ('Out[\#]:')",
247 224 metavar='InteractiveShell.prompt_out')
248 225 ),
249 226 (('--quick',), dict(
250 action='store_true', dest='Global.quick', default=NoConfigDefault,
227 action='store_true', dest='Global.quick',
251 228 help="Enable quick startup with no config files.")
252 229 ),
253 230 (('--readline',), dict(
254 231 action='store_true', dest='InteractiveShell.readline_use',
255 default=NoConfigDefault,
256 232 help="Enable readline for command line usage.")
257 233 ),
258 234 (('--no-readline',), dict(
259 235 action='store_false', dest='InteractiveShell.readline_use',
260 default=NoConfigDefault,
261 236 help="Disable readline for command line usage.")
262 237 ),
263 238 (('--screen-length','-sl'), dict(
264 239 type=int, dest='InteractiveShell.screen_length',
265 default=NoConfigDefault,
266 240 help=
267 241 """Number of lines of your screen, used to control printing of very
268 242 long strings. Strings longer than this number of lines will be sent
@@ -276,38 +250,34 b' cl_args = ('
276 250 metavar='InteractiveShell.screen_length')
277 251 ),
278 252 (('--separate-in','-si'), dict(
279 type=str, dest='InteractiveShell.separate_in', default=NoConfigDefault,
253 type=str, dest='InteractiveShell.separate_in',
280 254 help="Separator before input prompts. Default '\\n'.",
281 255 metavar='InteractiveShell.separate_in')
282 256 ),
283 257 (('--separate-out','-so'), dict(
284 258 type=str, dest='InteractiveShell.separate_out',
285 default=NoConfigDefault,
286 259 help="Separator before output prompts. Default 0 (nothing).",
287 260 metavar='InteractiveShell.separate_out')
288 261 ),
289 262 (('--separate-out2','-so2'), dict(
290 263 type=str, dest='InteractiveShell.separate_out2',
291 default=NoConfigDefault,
292 264 help="Separator after output prompts. Default 0 (nonight).",
293 265 metavar='InteractiveShell.separate_out2')
294 266 ),
295 267 (('-no-sep',), dict(
296 action='store_true', dest='Global.nosep', default=NoConfigDefault,
268 action='store_true', dest='Global.nosep',
297 269 help="Eliminate all spacing between prompts.")
298 270 ),
299 271 (('--term-title',), dict(
300 272 action='store_true', dest='InteractiveShell.term_title',
301 default=NoConfigDefault,
302 273 help="Enable auto setting the terminal title.")
303 274 ),
304 275 (('--no-term-title',), dict(
305 276 action='store_false', dest='InteractiveShell.term_title',
306 default=NoConfigDefault,
307 277 help="Disable auto setting the terminal title.")
308 278 ),
309 279 (('--xmode',), dict(
310 type=str, dest='InteractiveShell.xmode', default=NoConfigDefault,
280 type=str, dest='InteractiveShell.xmode',
311 281 help=
312 282 """Exception reporting mode ('Plain','Context','Verbose'). Plain:
313 283 similar to python's normal traceback printing. Context: prints 5 lines
@@ -323,18 +293,17 b' cl_args = ('
323 293 metavar='InteractiveShell.xmode')
324 294 ),
325 295 (('--ext',), dict(
326 type=str, dest='Global.extra_extension', default=NoConfigDefault,
296 type=str, dest='Global.extra_extension',
327 297 help="The dotted module name of an IPython extension to load.",
328 298 metavar='Global.extra_extension')
329 299 ),
330 300 (('-c',), dict(
331 type=str, dest='Global.code_to_run', default=NoConfigDefault,
301 type=str, dest='Global.code_to_run',
332 302 help="Execute the given command string.",
333 303 metavar='Global.code_to_run')
334 304 ),
335 305 (('-i',), dict(
336 306 action='store_true', dest='Global.force_interact',
337 default=NoConfigDefault,
338 307 help=
339 308 "If running code from the command line, become interactive afterwards."
340 309 )
@@ -342,13 +311,13 b' cl_args = ('
342 311
343 312 # Options to start with GUI control enabled from the beginning
344 313 (('--gui',), dict(
345 type=str, dest='Global.gui', default=NoConfigDefault,
314 type=str, dest='Global.gui',
346 315 help="Enable GUI event loop integration ('qt', 'wx', 'gtk').",
347 316 metavar='gui-mode')
348 317 ),
349 318
350 319 (('--pylab','-pylab'), dict(
351 type=str, dest='Global.pylab', default=NoConfigDefault,
320 type=str, dest='Global.pylab',
352 321 nargs='?', const='auto', metavar='gui-mode',
353 322 help="Pre-load matplotlib and numpy for interactive use. "+
354 323 "If no value is given, the gui backend is matplotlib's, else use "+
@@ -358,17 +327,17 b' cl_args = ('
358 327 # Legacy GUI options. Leave them in for backwards compatibility, but the
359 328 # 'thread' names are really a misnomer now.
360 329 (('--wthread','-wthread'), dict(
361 action='store_true', dest='Global.wthread', default=NoConfigDefault,
330 action='store_true', dest='Global.wthread',
362 331 help="Enable wxPython event loop integration "+
363 332 "(DEPRECATED, use --gui wx)")
364 333 ),
365 334 (('--q4thread','--qthread','-q4thread','-qthread'), dict(
366 action='store_true', dest='Global.q4thread', default=NoConfigDefault,
335 action='store_true', dest='Global.q4thread',
367 336 help="Enable Qt4 event loop integration. Qt3 is no longer supported. "+
368 337 "(DEPRECATED, use --gui qt)")
369 338 ),
370 339 (('--gthread','-gthread'), dict(
371 action='store_true', dest='Global.gthread', default=NoConfigDefault,
340 action='store_true', dest='Global.gthread',
372 341 help="Enable GTK event loop integration. "+
373 342 "(DEPRECATED, use --gui gtk)")
374 343 ),
General Comments 0
You need to be logged in to leave comments. Login now