Browse files

print usage on invalid command-line arguments

  • Loading branch information...
1 parent 6fdecea commit 15652253de6f9e27014bb3235ef65d410d0047c5 @minrk committed May 21, 2011
Showing with 20 additions and 9 deletions.
  1. +15 −7 IPython/config/application.py
  2. +5 −2 IPython/config/loader.py
View
22 IPython/config/application.py
@@ -24,7 +24,7 @@
from IPython.config.configurable import SingletonConfigurable
from IPython.config.loader import (
- KeyValueConfigLoader, PyFileConfigLoader, Config
+ KeyValueConfigLoader, PyFileConfigLoader, Config, ArgumentError
)
from IPython.utils.traitlets import (
@@ -175,8 +175,10 @@ def print_alias_help(self):
lines.append('')
classdict = {}
- for c in self.classes:
- classdict[c.__name__] = c
+ for cls in self.classes:
+ # include all parents in available names
+ for c in cls.mro():
+ classdict[c.__name__] = c
for alias, longname in self.aliases.iteritems():
classname, traitname = longname.split('.',1)
@@ -212,7 +214,7 @@ def print_subcommands(self):
lines = ["Subcommands"]
lines.append('-'*len(lines[0]))
- for subc, cls,help in self.subcommands:
+ for subc, (cls,help) in self.subcommands.iteritems():
lines.append("%s : %s"%(subc, cls))
if help:
lines.append(indent(help, flatten=True))
@@ -267,14 +269,14 @@ def initialize_subcommand(self, subc, argv=None):
self.print_description()
self.print_subcommands()
self.exit(0)
- subapp = self.subcommands.get(subc, None)
+ subapp,help = self.subcommands.get(subc, (None,None))
if subapp is None:
self.print_description()
print "No such subcommand: %r"%subc
print
self.print_subcommands()
self.exit(1)
-
+
if isinstance(subapp, basestring):
subapp = import_item(subapp)
@@ -308,7 +310,13 @@ def parse_command_line(self, argv=None):
loader = KeyValueConfigLoader(argv=argv, aliases=self.aliases,
flags=self.flags)
- config = loader.load_config()
+ try:
+ config = loader.load_config()
+ except ArgumentError as e:
+ self.log.fatal(str(e))
+ self.print_description()
+ self.print_help()
+ self.exit(1)
self.update_config(config)
def load_config_file(self, filename, path=None):
View
7 IPython/config/loader.py
@@ -36,6 +36,9 @@ class ConfigError(Exception):
class ConfigLoaderError(ConfigError):
pass
+class ArgumentError(ConfigLoaderError):
+ pass
+
#-----------------------------------------------------------------------------
# Argparse fix
#-----------------------------------------------------------------------------
@@ -403,14 +406,14 @@ def load_config(self, argv=None, aliases=None, flags=None):
m = item[2:]
cfg,_ = flags.get(m, (None,None))
if cfg is None:
- raise ValueError("Unrecognized flag: %r"%item)
+ raise ArgumentError("Unrecognized flag: %r"%item)
elif isinstance(cfg, (dict, Config)):
# update self.config with Config:
self.config.update(cfg)
else:
raise ValueError("Invalid flag: %r"%flag)
else:
- raise ValueError("Invalid argument: %r"%item)
+ raise ArgumentError("Invalid argument: %r"%item)
return self.config
class ArgParseConfigLoader(CommandLineConfigLoader):

0 comments on commit 1565225

Please sign in to comment.