Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

command-line suggestions #561

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 9 additions & 4 deletions IPython/config/application.py
Expand Up @@ -51,20 +51,25 @@
modes that involve setting multiple options together.

Flags *always* begin with '--', never just one '-'.
""".strip() # trim newlines of front and back
""".strip() # trim newlines off front and back

alias_description = """
These are commonly set parameters, given abbreviated aliases for convenience.
They are set in the same `name=value` way as class parameters, where
<name> is replaced by the real parameter for which it is an alias.
""".strip() # trim newlines of front and back

Setting parameters will *never* be prefixed with '-'.

This line is evaluated in Python, so simple expressions are allowed, e.g.
`C.a=range(3)` For setting C.a=[0,1,2]
""".strip() # trim newlines off front and back

keyvalue_description = """
Parameters are set from command-line arguments of the form:
`Class.trait=value`. Parameters will *never* be prefixed with '-'.
This line is evaluated in Python, so simple expressions are allowed, e.g.
`C.a='range(3)'` For setting C.a=[0,1,2]
""".strip() # trim newlines of front and back
`C.a=range(3)` For setting C.a=[0,1,2]
""".strip() # trim newlines off front and back

#-----------------------------------------------------------------------------
# Application class
Expand Down
20 changes: 18 additions & 2 deletions IPython/config/loader.py
Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down