Browse files

add suggestions for most common errors in kv_loader

The most likely mistakes people will make are:

ipython -pdb # single leading '-' for flag, which requires '--'
and ipython --gui=foo # leading '-' on assignment

This adds a suggestion to the error message in these two cases for otherwise valid arguments.
  • Loading branch information...
1 parent 3207b26 commit 358f7434ff437326ed01813c67c6dc70a8c52532 @minrk committed Jul 6, 2011
Showing with 18 additions and 2 deletions.
  1. +18 −2 IPython/config/loader.py
View
20 IPython/config/loader.py
@@ -326,7 +326,8 @@ class CommandLineConfigLoader(ConfigLoader):
"""
kv_pattern = re.compile(r'[A-Za-z]\w*(\.\w+)*\=.*')
-flag_pattern = re.compile(r'\-\-\w+(\-\w)*')
+flag_pattern = re.compile(r'\-\-\w+(\-\w)*$')
+bad_assign_pattern = re.compile(r'\-+.*\=.*')
class KeyValueConfigLoader(CommandLineConfigLoader):
"""A config loader that loads key value pairs from the command line.
@@ -460,7 +461,22 @@ def load_config(self, argv=None, aliases=None, flags=None):
raise ValueError("Invalid flag: %r"%flag)
elif item.startswith('-'):
# this shouldn't ever be valid
- raise ArgumentError("Invalid argument: %r"%item)
+ stripped = item.lstrip('-')
+ msg = "Invalid argument: '%s'"%item
+ suggest = None
+ if '=' in stripped:
+ # this is an assignment, but with one or more leading '-'
+ suggest = stripped
+ elif stripped in flags:
+ # this is a known flag with one (or more than two)
+ # leading '-'
+ suggest = '--'+stripped
+
+ if suggest:
+ # add suggestion to error message
+ msg += ". Did you mean '%s'?"%suggest
+
+ raise ArgumentError(msg)
else:
# keep all args that aren't valid in a list,
# in case our parent knows what to do with them.

0 comments on commit 358f743

Please sign in to comment.