command-line suggestions #561

Closed
wants to merge 2 commits into
from
Jump to file or symbol
Failed to load files and symbols.
+27 −6
Split
@@ -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
View
@@ -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.