summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorZearin <zearin@gonk.net>2011-11-15 15:00:37 -0500
committerZearin <zearin@gonk.net>2011-11-15 15:00:37 -0500
commit527ff957fa65262c0e8c7d9a7c69575ccf726684 (patch)
tree2af7ab17cede498b0c139c83de250a84f4ced8f7
parent636e4528bbaac98a24bc059b813116444e0873ba (diff)
downloadcmd2-527ff957fa65262c0e8c7d9a7c69575ccf726684.tar.gz
Use `.format()` over `%`; Python3 style exceptions.
-rw-r--r--cmd2/support.py60
1 files changed, 30 insertions, 30 deletions
diff --git a/cmd2/support.py b/cmd2/support.py
index 0f61a88..49941be 100644
--- a/cmd2/support.py
+++ b/cmd2/support.py
@@ -288,31 +288,40 @@ class OutputTrap(Borg):
@decorator
def options(option_list, arg_desc="arg"):
- '''Used as a decorator and passed a list of optparse-style options,
- alters a cmd2 method to populate its ``opts`` argument from its
- raw text argument.
-
- Example: transform
- def do_something(self, arg):
-
- into
- @options([make_option('-q', '--quick', action="store_true",
- help="Makes things fast")],
- "source dest")
- def do_something(self, arg, opts):
- if opts.quick:
- self.fast_button = True
- '''
+ '''
+ Used as a decorator and passed a list of optparse-style options,
+ alters a cmd2 method to populate its ``opts`` argument from its
+ raw text argument.
+
+
+ EXAMPLE: Change this...
+
+ def do_something(self, arg):
+
+
+ ...into this:
+
+
+ @options([make_option('-q', '--quick', action="store_true",
+ help="Makes things fast")],
+ "source dest")
+ def do_something(self, arg, opts):
+ if opts.quick:
+ self.fast_button = True
+ '''
if not isinstance(option_list, list):
option_list = [option_list]
for opt in option_list:
options_defined.append(pyparsing.Literal(opt.get_opt_string()))
def option_setup(func):
optionParser = OptionParser()
+
for opt in option_list:
optionParser.add_option(opt)
- optionParser.set_usage("%s [options] %s" % (func.__name__[3:], arg_desc))
+
+ optionParser.set_usage("{} [options] {}".format(func.__name__[3:], arg_desc))
optionParser._func = func
+
def new_func(instance, arg):
try:
opts, newArgList = optionParser.parse_args(arg.split())
@@ -325,15 +334,15 @@ def options(option_list, arg_desc="arg"):
arg = arg.with_args_replaced(newArgs)
else:
arg = newArgs
- except optparse.OptParseError, e:
- print (e)
+ except (optparse.OptParseError) as e:
+ print(e)
optionParser.print_help()
return
if hasattr(opts, '_exit'):
return None
result = func(instance, arg, opts)
return result
- new_func.__doc__ = '%s\n%s' % (func.__doc__, optionParser.format_help())
+ new_func.__doc__ = "{}\n{}".format(func.__doc__, optionParser.format_help())
return new_func
return option_setup
@@ -361,20 +370,11 @@ def cast(current, new):
return typ(new)
except:
pass
- print ("Problem setting parameter (now %s) to %s; incorrect type?" % (current, new))
+ print ("Problem setting parameter (now {}) to {}; incorrect type?".format(current, new))
return current
-def _attr_get_(obj, attr):
- '''Returns an attribute's value (or None if undefined--no error).
- Analagous to .get() for dictionaries. Useful when checking for
- option values that may not have be defined on a given method.
- '''
- try:
- return getattr(obj, attr)
- except AttributeError:
- return None
def replace_with_file_contents(fname):
@@ -382,7 +382,7 @@ def replace_with_file_contents(fname):
try:
result = open(os.path.expanduser(fname[0])).read()
except IOError:
- result = '< %s' % fname[0] # wasn't a file after all
+ result = '< {}'.format(fname[0]) # wasn't a file after all
else:
result = get_paste_buffer()
return result