summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKevin Van Brunt <kmvanbrunt@gmail.com>2018-09-21 09:03:55 -0400
committerKevin Van Brunt <kmvanbrunt@gmail.com>2018-09-21 09:03:55 -0400
commit20d57ff875f9f47c3c471ad1de2c15d587b1bf0c (patch)
tree20adad21cc22aaa9fc251500d6f0286092f3118d
parent68e107e614ec344a0339da9eccb1a88f0bef3478 (diff)
downloadcmd2-git-20d57ff875f9f47c3c471ad1de2c15d587b1bf0c.tar.gz
Simplified functions more
-rw-r--r--cmd2/cmd2.py23
-rw-r--r--cmd2/utils.py12
2 files changed, 16 insertions, 19 deletions
diff --git a/cmd2/cmd2.py b/cmd2/cmd2.py
index 7b6b4bcb..8b1cabc9 100644
--- a/cmd2/cmd2.py
+++ b/cmd2/cmd2.py
@@ -2553,10 +2553,12 @@ Usage: Usage: unalias [-a] name [name ...]
param = parameter.strip().lower()
result = {}
maxlen = 0
+
for p in self.settable:
if (not param) or p.startswith(param):
result[p] = '{}: {}'.format(p, str(getattr(self, p)))
maxlen = max(maxlen, len(result[p]))
+
if result:
for p in sorted(result):
if args.long:
@@ -2568,7 +2570,7 @@ Usage: Usage: unalias [-a] name [name ...]
if args.all:
self.poutput('\nRead only settings:{}'.format(self.cmdenvironment()))
else:
- raise LookupError("Parameter '%s' not supported (type 'set' for list of parameters)." % param)
+ raise LookupError("Parameter '{}' not supported (type 'set' for list of parameters).".format(param))
set_description = "Sets a settable parameter or shows current settings of parameters.\n"
set_description += "\n"
@@ -2578,7 +2580,7 @@ Usage: Usage: unalias [-a] name [name ...]
set_parser = ACArgumentParser(description=set_description)
set_parser.add_argument('-a', '--all', action='store_true', help='display read-only settings as well')
set_parser.add_argument('-l', '--long', action='store_true', help='describe function of parameter')
- setattr(set_parser.add_argument('param', nargs=(0, 1), help='parameter to set or view'),
+ setattr(set_parser.add_argument('param', nargs='?', help='parameter to set or view'),
ACTION_ARG_CHOICES, settable)
set_parser.add_argument('value', nargs='?', help='the new value for settable')
@@ -2589,15 +2591,12 @@ Usage: Usage: unalias [-a] name [name ...]
# Check if param was passed in
if not args.param:
return self.show(args)
- else:
- param = args.param.strip().lower()
+ param = args.param.strip().lower()
# Check if value was passed in
- value = ''
- if args.value:
- value = args.value.strip()
- if not value:
+ if not args.value:
return self.show(args, param)
+ value = args.value
# Check if param points to just one settable
if param not in self.settable:
@@ -2609,13 +2608,9 @@ Usage: Usage: unalias [-a] name [name ...]
# Update the settable's value
current_value = getattr(self, param)
-
- if utils.is_quoted(value):
- value = utils.strip_quotes(value)
- else:
- value = utils.cast(current_value, value)
-
+ value = utils.cast(current_value, value)
setattr(self, param, value)
+
self.poutput('{} - was: {}\nnow: {}\n'.format(param, current_value, value))
# See if we need to call a change hook for this settable
diff --git a/cmd2/utils.py b/cmd2/utils.py
index 9cbea915..9d71d061 100644
--- a/cmd2/utils.py
+++ b/cmd2/utils.py
@@ -93,6 +93,8 @@ def cast(current: Any, new: str) -> Any:
:return: new value with same type as current, or the current value if there was an error casting
"""
typ = type(current)
+ orig_new = new
+
if typ == bool:
try:
return bool(int(new))
@@ -100,18 +102,18 @@ def cast(current: Any, new: str) -> Any:
pass
try:
new = new.lower()
+ if (new == 'on') or (new[0] in ('y', 't')):
+ return True
+ if (new == 'off') or (new[0] in ('n', 'f')):
+ return False
except AttributeError:
pass
- if (new == 'on') or (new[0] in ('y', 't')):
- return True
- if (new == 'off') or (new[0] in ('n', 'f')):
- return False
else:
try:
return typ(new)
except (ValueError, TypeError):
pass
- print("Problem setting parameter (now %s) to %s; incorrect type?" % (current, new))
+ print("Problem setting parameter (now {}) to {}; incorrect type?".format(current, orig_new))
return current