##// END OF EJS Templates
allow extra_args in applications
MinRK -
Show More
@@ -112,6 +112,9 b' class Application(SingletonConfigurable):'
112 # parse_command_line will initialize a subapp, if requested
112 # parse_command_line will initialize a subapp, if requested
113 subapp = Instance('IPython.config.application.Application', allow_none=True)
113 subapp = Instance('IPython.config.application.Application', allow_none=True)
114
114
115 # extra command-line arguments that don't set config values
116 extra_args = List(Unicode)
117
115
118
116 def __init__(self, **kwargs):
119 def __init__(self, **kwargs):
117 SingletonConfigurable.__init__(self, **kwargs)
120 SingletonConfigurable.__init__(self, **kwargs)
@@ -266,13 +269,7 b' class Application(SingletonConfigurable):'
266
269
267 def initialize_subcommand(self, subc, argv=None):
270 def initialize_subcommand(self, subc, argv=None):
268 """Initialize a subcommand with argv"""
271 """Initialize a subcommand with argv"""
269 subapp,help = self.subcommands.get(subc, (None,None))
272 subapp,help = self.subcommands.get(subc)
270 if subapp is None:
271 self.print_description()
272 print "No such subcommand: %r"%subc
273 print
274 self.print_subcommands()
275 self.exit(1)
276
273
277 if isinstance(subapp, basestring):
274 if isinstance(subapp, basestring):
278 subapp = import_item(subapp)
275 subapp = import_item(subapp)
@@ -289,7 +286,7 b' class Application(SingletonConfigurable):'
289 if self.subcommands and len(argv) > 0:
286 if self.subcommands and len(argv) > 0:
290 # we have subcommands, and one may have been specified
287 # we have subcommands, and one may have been specified
291 subc, subargv = argv[0], argv[1:]
288 subc, subargv = argv[0], argv[1:]
292 if re.match(r'^\w(\-?\w)*$', subc):
289 if re.match(r'^\w(\-?\w)*$', subc) and subc in self.subcommands:
293 # it's a subcommand, and *not* a flag or class parameter
290 # it's a subcommand, and *not* a flag or class parameter
294 return self.initialize_subcommand(subc, subargv)
291 return self.initialize_subcommand(subc, subargv)
295
292
@@ -312,6 +309,8 b' class Application(SingletonConfigurable):'
312 self.print_help()
309 self.print_help()
313 self.exit(1)
310 self.exit(1)
314 self.update_config(config)
311 self.update_config(config)
312 # store unparsed args in extra_args
313 self.extra_args = loader.extra_args
315
314
316 def load_config_file(self, filename, path=None):
315 def load_config_file(self, filename, path=None):
317 """Load a .py based config file by filename and path."""
316 """Load a .py based config file by filename and path."""
@@ -382,7 +382,9 b' class KeyValueConfigLoader(CommandLineConfigLoader):'
382 aliases = self.aliases
382 aliases = self.aliases
383 if flags is None:
383 if flags is None:
384 flags = self.flags
384 flags = self.flags
385
385
386 self.extra_args = []
387
386 for item in argv:
388 for item in argv:
387 if kv_pattern.match(item):
389 if kv_pattern.match(item):
388 lhs,rhs = item.split('=',1)
390 lhs,rhs = item.split('=',1)
@@ -414,8 +416,13 b' class KeyValueConfigLoader(CommandLineConfigLoader):'
414 self.config[sec].update(c)
416 self.config[sec].update(c)
415 else:
417 else:
416 raise ValueError("Invalid flag: %r"%flag)
418 raise ValueError("Invalid flag: %r"%flag)
417 else:
419 elif item.startswith('-'):
420 # this shouldn't ever be valid
418 raise ArgumentError("Invalid argument: %r"%item)
421 raise ArgumentError("Invalid argument: %r"%item)
422 else:
423 # keep all args that aren't valid in a list,
424 # in case our parent knows what to do with them.
425 self.extra_args.append(item)
419 return self.config
426 return self.config
420
427
421 class ArgParseConfigLoader(CommandLineConfigLoader):
428 class ArgParseConfigLoader(CommandLineConfigLoader):
@@ -116,14 +116,20 b' class TestApplication(TestCase):'
116 """test that setting flags doesn't clobber existing settings"""
116 """test that setting flags doesn't clobber existing settings"""
117 app = MyApp()
117 app = MyApp()
118 app.parse_command_line(["Bar.b=5", "--disable"])
118 app.parse_command_line(["Bar.b=5", "--disable"])
119 print app.config
120 app.init_bar()
119 app.init_bar()
121 self.assertEquals(app.bar.enabled, False)
120 self.assertEquals(app.bar.enabled, False)
122 self.assertEquals(app.bar.b, 5)
121 self.assertEquals(app.bar.b, 5)
123 app.parse_command_line(["--enable", "Bar.b=10"])
122 app.parse_command_line(["--enable", "Bar.b=10"])
124 print app.config
125 app.init_bar()
123 app.init_bar()
126 self.assertEquals(app.bar.enabled, True)
124 self.assertEquals(app.bar.enabled, True)
127 self.assertEquals(app.bar.b, 10)
125 self.assertEquals(app.bar.b, 10)
128
126
127 def test_extra_args(self):
128 app = MyApp()
129 app.parse_command_line(['extra', "Bar.b=5", "--disable", 'args'])
130 app.init_bar()
131 self.assertEquals(app.bar.enabled, False)
132 self.assertEquals(app.bar.b, 5)
133 self.assertEquals(app.extra_args, ['extra', 'args'])
134
129
135
@@ -123,6 +123,13 b' class TestKeyValueCL(TestCase):'
123 self.assertEquals(config.Foo.Bar.value, 10)
123 self.assertEquals(config.Foo.Bar.value, 10)
124 self.assertEquals(config.Foo.Bam.value, range(10))
124 self.assertEquals(config.Foo.Bam.value, range(10))
125 self.assertEquals(config.D.C.value, 'hi there')
125 self.assertEquals(config.D.C.value, 'hi there')
126
127 def test_extra_args(self):
128 cl = KeyValueConfigLoader()
129 config = cl.load_config(['a=5', 'b', 'c=10', 'd'])
130 self.assertEquals(cl.extra_args, ['b', 'd'])
131 self.assertEquals(config.a, 5)
132 self.assertEquals(config.c, 10)
126
133
127
134
128 class TestConfig(TestCase):
135 class TestConfig(TestCase):
General Comments 0
You need to be logged in to leave comments. Login now