-status=open
+status=fixed
The entry function for your plugin. args is everything from
sys.argv after the name of your plugin (e.g. for the command
`be open abc', args=['abc']).
+
+ Note: be supports command-completion. To avoid raising errors you
+ need to deal with possible '--complete' options and arguments.
+ See the 'Command completion' section below for more information.
help()
Return the string to be output by `be help <yourplugin>',
`be <yourplugin> --help', etc.
Again, you can just browse around in becommands to get a feel for things.
+Testing
+-------
+
Run any doctests in your plugin with
be$ python test.py <yourplugin>
for example
be$ python test.py merge
+
+Command completion
+------------------
+
+BE implements a general framework to make it easy to support command
+completion for arbitrary plugins. In order to support this system,
+all becommands should properly handle the '--complete' commandline
+argument, returning a list of possible completions. For example
+ $ be --commands
+ lists options accepted by be and the names of all available becommands.
+ $ be list --commands
+ lists options accepted by becommand/list
+ $ be list --status --commands
+ lists arguments accepted by the becommand/list --status option
+ $ be show -- --commands
+ lists possible vals for the first positional argument of becommand/show
+This is a lot of information, but command-line completion is really
+convenient for the user. See becommand/list.py and becommand/show.py
+for example implementations. The basic idea is to raise
+ cmdutil.GetCompletions(['list','of','possible','completions'])
+once you've determined what that list should be.
+
+However, command completion is not critcal. The first priority is to
+implement the target functionality, with fancy shell sugar coming
+later. In recognition of this, cmdutil provides the default_complete
+function which ensures that if '--complete' is any one of the
+arguments, options, or option-arguments, GetCompletions will be raised
+with and empty list.
>>> bd.bug_from_shortname("a").assigned is None
True
"""
- options, args = get_parser().parse_args(args)
+ parser = get_parser()
+ options, args = parser.parse_args(args)
+ cmdutil.default_complete(options, args, parser)
assert(len(args) in (0, 1, 2))
if len(args) == 0:
raise cmdutil.UsageError("Please specify a bug id.")
>>> print bd.bug_from_shortname("a").status
closed
"""
- options, args = get_parser().parse_args(args)
+ parser = get_parser()
+ options, args = parser.parse_args(args)
+ cmdutil.default_complete(options, args, parser)
if len(args) == 0:
raise cmdutil.UsageError("Please specify a bug id.")
if len(args) > 1:
I like cheese
<BLANKLINE>
"""
- options, args = get_parser().parse_args(args)
+ parser = get_parser()
+ options, args = parser.parse_args(args)
+ cmdutil.default_complete(options, args, parser)
if len(args) == 0:
raise cmdutil.UsageError("Please specify a bug or comment id.")
if len(args) > 2:
status: open -> closed
<BLANKLINE>
"""
- options, args = get_parser().parse_args(args)
+ parser = get_parser()
+ options, args = parser.parse_args(args)
+ cmdutil.default_complete(options, args, parser)
if len(args) == 0:
revision = None
if len(args) == 1:
<BLANKLINE>
Options:
-h, --help Print a help message
+ --complete Print a list of available completions
<BLANKLINE>
Print help for specified command or list of all commands.
<BLANKLINE>
"""
- options, args = get_parser().parse_args(args)
+ parser = get_parser()
+ options, args = parser.parse_args(args)
+ cmdutil.default_complete(options, args, parser)
if len(args) > 1:
raise cmdutil.UsageError("Too many arguments.")
if len(args) == 0:
"""
parser = get_parser()
options, args = parser.parse_args(args)
-
- for option in [o.dest for o in parser.option_list if o.dest != None]:
- value = getattr(options, option)
- if value == "--complete":
- if option == "status":
- raise cmdutil.GetCompletions(status_values)
- raise cmdutil.GetCompletions()
- if "--complete" in args:
- raise cmdutil.GetCompletions() # no completions for arguments yet
-
+ complete(options, args, parser)
if len(args) > 0:
raise cmdutil.UsageError("Too many arguments.")
def help():
return get_parser().help_str() + longhelp
+
+def complete(options, args, parser):
+ for option, value in cmdutil.option_value_pairs(options, parser):
+ if value == "--complete":
+ if option == "status":
+ raise cmdutil.GetCompletions(status_values)
+ elif option == "severity":
+ raise cmdutil.GetCompletions(severity_values)
+ raise cmdutil.GetCompletions()
+ if "--complete" in args:
+ raise cmdutil.GetCompletions() # no positional arguments for list
>>> print b.status
closed
"""
- options, args = get_parser().parse_args(args)
+ parser = get_parser()
+ options, args = parser.parse_args(args)
+ cmdutil.default_complete(options, args, parser)
if len(args) < 2:
raise cmdutil.UsageError("Please specify two bug ids.")
if len(args) > 2:
>>> bug.target == None
True
"""
- options, args = get_parser().parse_args(args)
+ parser = get_parser()
+ options, args = parser.parse_args(args)
+ cmdutil.default_complete(options, args, parser)
if len(args) != 1:
raise cmdutil.UsageError("Please supply a summary message")
bd = bugdir.BugDir(from_disk=True, manipulate_encodings=not test)
>>> print bd.bug_from_shortname("b").status
open
"""
- options, args = get_parser().parse_args(args)
+ parser = get_parser()
+ options, args = parser.parse_args(args)
+ cmdutil.default_complete(options, args, parser)
if len(args) == 0:
raise cmdutil.UsageError, "Please specify a bug id."
if len(args) > 1:
... print "Bug not found"
Bug not found
"""
- options, args = get_parser().parse_args(args)
+ parser = get_parser()
+ options, args = parser.parse_args(args)
+ cmdutil.default_complete(options, args, parser)
if len(args) != 1:
raise cmdutil.UsageError, "Please specify a bug id."
bd = bugdir.BugDir(from_disk=True, manipulate_encodings=not test)
>>> execute(["target"], test=True)
None
"""
- options, args = get_parser().parse_args(args)
+ parser = get_parser()
+ options, args = parser.parse_args(args)
+ cmdutil.default_complete(options, args, parser)
if len(args) > 2:
raise cmdutil.UsageError, "Too many arguments"
bd = bugdir.BugDir(from_disk=True, manipulate_encodings=not test)
UserError: No such directory: /highly-unlikely-to-exist
>>> os.chdir('/')
"""
- options, args = get_parser().parse_args(args)
+ parser = get_parser()
+ options, args = parser.parse_args(args)
+ cmdutil.default_complete(options, args, parser)
if len(args) > 1:
raise cmdutil.UsageError
if len(args) == 1:
Traceback (most recent call last):
UserError: Invalid severity level: none
"""
- options, args = get_parser().parse_args(args)
+ parser = get_parser()
+ options, args = parser.parse_args(args)
+ cmdutil.default_complete(options, args, parser)
if len(args) not in (1,2):
raise cmdutil.UsageError
bd = bugdir.BugDir(from_disk=True, manipulate_encodings=not test)
Bug A
<BLANKLINE>
"""
- options, args = get_parser().parse_args(args)
+ parser = get_parser()
+ options, args = parser.parse_args(args)
+ cmdutil.default_complete(options, args, parser)
if len(args) == 0:
raise cmdutil.UsageError
bd = bugdir.BugDir(from_disk=True, manipulate_encodings=not test)
Traceback (most recent call last):
UserError: Invalid status: none
"""
- options, args = get_parser().parse_args(args)
+ parser = get_parser()
+ options, args = parser.parse_args(args)
+ cmdutil.default_complete(options, args, parser)
if len(args) not in (1,2):
raise cmdutil.UsageError
bd = bugdir.BugDir(from_disk=True, manipulate_encodings=not test)
>>> execute(["a"], test=True)
No target assigned.
"""
- options, args = get_parser().parse_args(args)
+ parser = get_parser()
+ options, args = parser.parse_args(args)
+ cmdutil.default_complete(options, args, parser)
if len(args) not in (1, 2):
raise cmdutil.UsageError
bd = bugdir.BugDir(from_disk=True, manipulate_encodings=not test)
ret.append("be %s%*s %s" % (name, numExtraSpaces, "", desc))
return "\n".join(ret)
-def options(cmd):
+def completions(cmd):
parser = get_command(cmd).get_parser()
longopts = []
for opt in parser.option_list:
raise GetHelp
def raise_get_completions(option, opt, value, parser):
- raise GetCompletions(options(sys.argv[1]))
+ raise GetCompletions(completions(sys.argv[1]))
class CmdOptionParser(optparse.OptionParser):
def __init__(self, usage):
self.print_help(f)
return f.getvalue()
+def option_value_pairs(options, parser):
+ """
+ Iterate through OptionParser (option, value) pairs.
+ """
+ for option in [o.dest for o in parser.option_list if o.dest != None]:
+ value = getattr(options, option)
+ yield (option, value)
+
+def default_complete(options, args, parser):
+ """
+ A dud complete implementation for becommands to that the
+ --complete argument doesn't cause any problems. Use this
+ until you've set up a command-specific complete function.
+ """
+ for option,value in option_value_pairs(options, parser):
+ if value == "--complete":
+ raise cmdutil.GetCompletions()
+ if "--complete" in args:
+ raise cmdutil.GetCompletions()
+
def underlined(instring):
"""Produces a version of a string that is underlined with '='