##// END OF EJS Templates
print usage on invalid command-line arguments
MinRK -
Show More
@@ -24,7 +24,7 b' import sys'
24
24
25 from IPython.config.configurable import SingletonConfigurable
25 from IPython.config.configurable import SingletonConfigurable
26 from IPython.config.loader import (
26 from IPython.config.loader import (
27 KeyValueConfigLoader, PyFileConfigLoader, Config
27 KeyValueConfigLoader, PyFileConfigLoader, Config, ArgumentError
28 )
28 )
29
29
30 from IPython.utils.traitlets import (
30 from IPython.utils.traitlets import (
@@ -175,8 +175,10 b' class Application(SingletonConfigurable):'
175 lines.append('')
175 lines.append('')
176
176
177 classdict = {}
177 classdict = {}
178 for c in self.classes:
178 for cls in self.classes:
179 classdict[c.__name__] = c
179 # include all parents in available names
180 for c in cls.mro():
181 classdict[c.__name__] = c
180
182
181 for alias, longname in self.aliases.iteritems():
183 for alias, longname in self.aliases.iteritems():
182 classname, traitname = longname.split('.',1)
184 classname, traitname = longname.split('.',1)
@@ -212,7 +214,7 b' class Application(SingletonConfigurable):'
212
214
213 lines = ["Subcommands"]
215 lines = ["Subcommands"]
214 lines.append('-'*len(lines[0]))
216 lines.append('-'*len(lines[0]))
215 for subc, cls,help in self.subcommands:
217 for subc, (cls,help) in self.subcommands.iteritems():
216 lines.append("%s : %s"%(subc, cls))
218 lines.append("%s : %s"%(subc, cls))
217 if help:
219 if help:
218 lines.append(indent(help, flatten=True))
220 lines.append(indent(help, flatten=True))
@@ -267,14 +269,14 b' class Application(SingletonConfigurable):'
267 self.print_description()
269 self.print_description()
268 self.print_subcommands()
270 self.print_subcommands()
269 self.exit(0)
271 self.exit(0)
270 subapp = self.subcommands.get(subc, None)
272 subapp,help = self.subcommands.get(subc, (None,None))
271 if subapp is None:
273 if subapp is None:
272 self.print_description()
274 self.print_description()
273 print "No such subcommand: %r"%subc
275 print "No such subcommand: %r"%subc
274 print
276 print
275 self.print_subcommands()
277 self.print_subcommands()
276 self.exit(1)
278 self.exit(1)
277
279
278 if isinstance(subapp, basestring):
280 if isinstance(subapp, basestring):
279 subapp = import_item(subapp)
281 subapp = import_item(subapp)
280
282
@@ -308,7 +310,13 b' class Application(SingletonConfigurable):'
308
310
309 loader = KeyValueConfigLoader(argv=argv, aliases=self.aliases,
311 loader = KeyValueConfigLoader(argv=argv, aliases=self.aliases,
310 flags=self.flags)
312 flags=self.flags)
311 config = loader.load_config()
313 try:
314 config = loader.load_config()
315 except ArgumentError as e:
316 self.log.fatal(str(e))
317 self.print_description()
318 self.print_help()
319 self.exit(1)
312 self.update_config(config)
320 self.update_config(config)
313
321
314 def load_config_file(self, filename, path=None):
322 def load_config_file(self, filename, path=None):
@@ -36,6 +36,9 b' class ConfigError(Exception):'
36 class ConfigLoaderError(ConfigError):
36 class ConfigLoaderError(ConfigError):
37 pass
37 pass
38
38
39 class ArgumentError(ConfigLoaderError):
40 pass
41
39 #-----------------------------------------------------------------------------
42 #-----------------------------------------------------------------------------
40 # Argparse fix
43 # Argparse fix
41 #-----------------------------------------------------------------------------
44 #-----------------------------------------------------------------------------
@@ -403,14 +406,14 b' class KeyValueConfigLoader(CommandLineConfigLoader):'
403 m = item[2:]
406 m = item[2:]
404 cfg,_ = flags.get(m, (None,None))
407 cfg,_ = flags.get(m, (None,None))
405 if cfg is None:
408 if cfg is None:
406 raise ValueError("Unrecognized flag: %r"%item)
409 raise ArgumentError("Unrecognized flag: %r"%item)
407 elif isinstance(cfg, (dict, Config)):
410 elif isinstance(cfg, (dict, Config)):
408 # update self.config with Config:
411 # update self.config with Config:
409 self.config.update(cfg)
412 self.config.update(cfg)
410 else:
413 else:
411 raise ValueError("Invalid flag: %r"%flag)
414 raise ValueError("Invalid flag: %r"%flag)
412 else:
415 else:
413 raise ValueError("Invalid argument: %r"%item)
416 raise ArgumentError("Invalid argument: %r"%item)
414 return self.config
417 return self.config
415
418
416 class ArgParseConfigLoader(CommandLineConfigLoader):
419 class ArgParseConfigLoader(CommandLineConfigLoader):
General Comments 0
You need to be logged in to leave comments. Login now