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