##// END OF EJS Templates
Manage and propagate argv correctly....
Fernando Perez -
Show More
@@ -301,18 +301,39 b' class ArgParseConfigLoader(CommandLineConfigLoader):'
301 301 # arguments = [(('-f','--file'),dict(type=str,dest='file'))]
302 302 arguments = ()
303 303
304 def __init__(self, *args, **kw):
304 def __init__(self, argv=None, *args, **kw):
305 305 """Create a config loader for use with argparse.
306 306
307 The args and kwargs arguments here are passed onto the constructor
308 of :class:`argparse.ArgumentParser`.
307 With the exception of argv, other args and kwargs arguments here are
308 passed onto the constructor of :class:`argparse.ArgumentParser`.
309
310 Parameters
311 ----------
312
313 argv : optional, list
314 If given, used to read command-line arguments from, otherwise
315 sys.argv[1:] is used.
309 316 """
310 317 super(CommandLineConfigLoader, self).__init__()
318 if argv == None:
319 argv = sys.argv[1:]
320 self.argv = argv
311 321 self.args = args
312 322 self.kw = kw
313 323
314 324 def load_config(self, args=None):
315 """Parse command line arguments and return as a Struct."""
325 """Parse command line arguments and return as a Struct.
326
327 Parameters
328 ----------
329
330 args : optional, list
331 If given, a list with the structure of sys.argv[1:] to parse arguments
332 from. If not given, the instance's self.argv attribute (given at
333 construction time) is used."""
334
335 if args is None:
336 args = self.argv
316 337 self._create_parser()
317 338 self._parse_args(args)
318 339 self._convert_to_config()
@@ -337,12 +358,9 b' class ArgParseConfigLoader(CommandLineConfigLoader):'
337 358 argument[1].setdefault('default', NoConfigDefault)
338 359 self.parser.add_argument(*argument[0],**argument[1])
339 360
340 def _parse_args(self, args=None):
341 """self.parser->self.parsed_data"""
342 if args is None:
343 self.parsed_data, self.extra_args = self.parser.parse_known_args()
344 else:
345 self.parsed_data, self.extra_args = self.parser.parse_known_args(args)
361 def _parse_args(self, args):
362 """self.parser->self.parsed_data"""
363 self.parsed_data, self.extra_args = self.parser.parse_known_args(args)
346 364
347 365 def _convert_to_config(self):
348 366 """self.parsed_data->self.config"""
@@ -92,11 +92,14 b' class Application(object):'
92 92 profile_name = None
93 93 #: User's ipython directory, typically ~/.ipython/
94 94 ipython_dir = None
95 #: A reference to the argv to be used (typically ends up being sys.argv[1:])
96 argv = None
95 97
96 98 # Private attributes
97 99 _exiting = False
98 100
99 def __init__(self):
101 def __init__(self, argv=None):
102 self.argv = sys.argv[1:] if argv is None else argv
100 103 self.init_logger()
101 104
102 105 def init_logger(self):
@@ -173,7 +176,7 b' class Application(object):'
173 176
174 177 def create_command_line_config(self):
175 178 """Create and return a command line config loader."""
176 return BaseAppArgParseConfigLoader(
179 return BaseAppArgParseConfigLoader(self.argv,
177 180 description=self.description,
178 181 version=release.version
179 182 )
@@ -344,7 +344,7 b' class IPythonApp(Application):'
344 344
345 345 def create_command_line_config(self):
346 346 """Create and return a command line config loader."""
347 return IPythonAppCLConfigLoader(
347 return IPythonAppCLConfigLoader(self.argv,
348 348 description=self.description,
349 349 version=release.version
350 350 )
@@ -565,7 +565,8 b' class IPythonApp(Application):'
565 565 if self.master_config.Global.interact:
566 566 self.log.debug("Starting IPython's mainloop...")
567 567 self.shell.mainloop()
568
568 else:
569 self.log.debug("IPython not interactive, start_app is no-op...")
569 570
570 571
571 572 def load_default_config(ipython_dir=None):
@@ -584,4 +585,3 b' def launch_new_instance():'
584 585 """Create and run a full blown IPython instance"""
585 586 app = IPythonApp()
586 587 app.start()
587
General Comments 0
You need to be logged in to leave comments. Login now