##// END OF EJS Templates
allow extra_args in applications
MinRK -
Show More
@@ -112,6 +112,9 b' class Application(SingletonConfigurable):'
112 112 # parse_command_line will initialize a subapp, if requested
113 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 119 def __init__(self, **kwargs):
117 120 SingletonConfigurable.__init__(self, **kwargs)
@@ -266,13 +269,7 b' class Application(SingletonConfigurable):'
266 269
267 270 def initialize_subcommand(self, subc, argv=None):
268 271 """Initialize a subcommand with argv"""
269 subapp,help = self.subcommands.get(subc, (None,None))
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)
272 subapp,help = self.subcommands.get(subc)
276 273
277 274 if isinstance(subapp, basestring):
278 275 subapp = import_item(subapp)
@@ -289,7 +286,7 b' class Application(SingletonConfigurable):'
289 286 if self.subcommands and len(argv) > 0:
290 287 # we have subcommands, and one may have been specified
291 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 290 # it's a subcommand, and *not* a flag or class parameter
294 291 return self.initialize_subcommand(subc, subargv)
295 292
@@ -312,6 +309,8 b' class Application(SingletonConfigurable):'
312 309 self.print_help()
313 310 self.exit(1)
314 311 self.update_config(config)
312 # store unparsed args in extra_args
313 self.extra_args = loader.extra_args
315 314
316 315 def load_config_file(self, filename, path=None):
317 316 """Load a .py based config file by filename and path."""
@@ -382,7 +382,9 b' class KeyValueConfigLoader(CommandLineConfigLoader):'
382 382 aliases = self.aliases
383 383 if flags is None:
384 384 flags = self.flags
385
385
386 self.extra_args = []
387
386 388 for item in argv:
387 389 if kv_pattern.match(item):
388 390 lhs,rhs = item.split('=',1)
@@ -414,8 +416,13 b' class KeyValueConfigLoader(CommandLineConfigLoader):'
414 416 self.config[sec].update(c)
415 417 else:
416 418 raise ValueError("Invalid flag: %r"%flag)
417 else:
419 elif item.startswith('-'):
420 # this shouldn't ever be valid
418 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 426 return self.config
420 427
421 428 class ArgParseConfigLoader(CommandLineConfigLoader):
@@ -116,14 +116,20 b' class TestApplication(TestCase):'
116 116 """test that setting flags doesn't clobber existing settings"""
117 117 app = MyApp()
118 118 app.parse_command_line(["Bar.b=5", "--disable"])
119 print app.config
120 119 app.init_bar()
121 120 self.assertEquals(app.bar.enabled, False)
122 121 self.assertEquals(app.bar.b, 5)
123 122 app.parse_command_line(["--enable", "Bar.b=10"])
124 print app.config
125 123 app.init_bar()
126 124 self.assertEquals(app.bar.enabled, True)
127 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 123 self.assertEquals(config.Foo.Bar.value, 10)
124 124 self.assertEquals(config.Foo.Bam.value, range(10))
125 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 135 class TestConfig(TestCase):
General Comments 0
You need to be logged in to leave comments. Login now