From 84551c20fe603ee9832b0b17061660cdf31a913e Mon Sep 17 00:00:00 2001 From: "W. Trevor King" Date: Thu, 27 Nov 2008 11:03:21 -0500 Subject: [PATCH] Added bugid_args option to cmdutil.default_complete. Now most of the bug-id arguments support Bash completion. Since there will hopefully be lots of bugs in the database, I decided to filter the list of available bugs. Currently, we just auto-complete active bugs for most commands, with the exceptions of open (obviously) and status (which needs to work on all types of bugs). --- becommands/assign.py | 3 ++- becommands/close.py | 3 ++- becommands/merge.py | 5 ++++- becommands/open.py | 3 ++- becommands/remove.py | 3 ++- becommands/severity.py | 3 ++- becommands/show.py | 3 ++- becommands/status.py | 3 ++- becommands/target.py | 3 ++- libbe/cmdutil.py | 26 +++++++++++++++++++++++--- 10 files changed, 43 insertions(+), 12 deletions(-) diff --git a/becommands/assign.py b/becommands/assign.py index 9869350..a41bbae 100644 --- a/becommands/assign.py +++ b/becommands/assign.py @@ -43,7 +43,8 @@ def execute(args, test=False): """ parser = get_parser() options, args = parser.parse_args(args) - cmdutil.default_complete(options, args, parser) + cmdutil.default_complete(options, args, parser, + bugid_args={0: lambda bug : bug.active==True}) assert(len(args) in (0, 1, 2)) if len(args) == 0: raise cmdutil.UsageError("Please specify a bug id.") diff --git a/becommands/close.py b/becommands/close.py index 37fa032..d8826b0 100644 --- a/becommands/close.py +++ b/becommands/close.py @@ -33,7 +33,8 @@ def execute(args, test=False): """ parser = get_parser() options, args = parser.parse_args(args) - cmdutil.default_complete(options, args, parser) + cmdutil.default_complete(options, args, parser, + bugid_args={0: lambda bug : bug.active==True}) if len(args) == 0: raise cmdutil.UsageError("Please specify a bug id.") if len(args) > 1: diff --git a/becommands/merge.py b/becommands/merge.py index 583bd28..9f324f7 100644 --- a/becommands/merge.py +++ b/becommands/merge.py @@ -122,7 +122,10 @@ def execute(args, test=False): """ parser = get_parser() options, args = parser.parse_args(args) - cmdutil.default_complete(options, args, parser) + cmdutil.default_complete(options, args, parser, + bugid_args={0: lambda bug : bug.active==True, + 1: lambda bug : bug.active==True}) + if len(args) < 2: raise cmdutil.UsageError("Please specify two bug ids.") if len(args) > 2: diff --git a/becommands/open.py b/becommands/open.py index 41db5e9..7a18fd0 100644 --- a/becommands/open.py +++ b/becommands/open.py @@ -32,7 +32,8 @@ def execute(args, test=False): """ parser = get_parser() options, args = parser.parse_args(args) - cmdutil.default_complete(options, args, parser) + cmdutil.default_complete(options, args, parser, + bugid_args={0: lambda bug : bug.active==False}) if len(args) == 0: raise cmdutil.UsageError, "Please specify a bug id." if len(args) > 1: diff --git a/becommands/remove.py b/becommands/remove.py index cd877c5..fa264b8 100644 --- a/becommands/remove.py +++ b/becommands/remove.py @@ -37,7 +37,8 @@ def execute(args, test=False): """ parser = get_parser() options, args = parser.parse_args(args) - cmdutil.default_complete(options, args, parser) + cmdutil.default_complete(options, args, parser, + bugid_args={0: lambda bug : bug.active==True}) if len(args) != 1: raise cmdutil.UsageError, "Please specify a bug id." bd = bugdir.BugDir(from_disk=True, manipulate_encodings=not test) diff --git a/becommands/severity.py b/becommands/severity.py index 29a5545..7b7ee29 100644 --- a/becommands/severity.py +++ b/becommands/severity.py @@ -35,7 +35,8 @@ def execute(args, test=False): """ parser = get_parser() options, args = parser.parse_args(args) - cmdutil.default_complete(options, args, parser) + cmdutil.default_complete(options, args, parser, + bugid_args={0: lambda bug : bug.active==True}) if len(args) not in (1,2): raise cmdutil.UsageError bd = bugdir.BugDir(from_disk=True, manipulate_encodings=not test) diff --git a/becommands/show.py b/becommands/show.py index 3c00712..37168c1 100644 --- a/becommands/show.py +++ b/becommands/show.py @@ -37,7 +37,8 @@ def execute(args, test=False): """ parser = get_parser() options, args = parser.parse_args(args) - cmdutil.default_complete(options, args, parser) + cmdutil.default_complete(options, args, parser, + bugid_args={0: lambda bug : bug.active==True}) if len(args) == 0: raise cmdutil.UsageError bd = bugdir.BugDir(from_disk=True, manipulate_encodings=not test) diff --git a/becommands/status.py b/becommands/status.py index 4ac7170..5ff824e 100644 --- a/becommands/status.py +++ b/becommands/status.py @@ -35,7 +35,8 @@ def execute(args, test=False): """ parser = get_parser() options, args = parser.parse_args(args) - cmdutil.default_complete(options, args, parser) + cmdutil.default_complete(options, args, parser, + bugid_args={0: lambda bug : True}) if len(args) not in (1,2): raise cmdutil.UsageError bd = bugdir.BugDir(from_disk=True, manipulate_encodings=not test) diff --git a/becommands/target.py b/becommands/target.py index 497ebf0..d14ff06 100644 --- a/becommands/target.py +++ b/becommands/target.py @@ -34,7 +34,8 @@ def execute(args, test=False): """ parser = get_parser() options, args = parser.parse_args(args) - cmdutil.default_complete(options, args, parser) + cmdutil.default_complete(options, args, parser, + bugid_args={0: lambda bug : bug.active==True}) if len(args) not in (1, 2): raise cmdutil.UsageError bd = bugdir.BugDir(from_disk=True, manipulate_encodings=not test) diff --git a/libbe/cmdutil.py b/libbe/cmdutil.py index 7f6e5ac..0382664 100644 --- a/libbe/cmdutil.py +++ b/libbe/cmdutil.py @@ -129,17 +129,37 @@ def option_value_pairs(options, parser): value = getattr(options, option) yield (option, value) -def default_complete(options, args, parser): +def default_complete(options, args, parser, bugid_args={}): """ 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. + + bugid_args is an optional dict where the keys are positional + arguments taking bug shortnames and the values are functions for + filtering, since that's a common enough operation. + e.g. for "be open [options] BUGID" + bugid_args = {0: lambda bug : bug.active == False} """ for option,value in option_value_pairs(options, parser): if value == "--complete": raise cmdutil.GetCompletions() - if "--complete" in args: - raise cmdutil.GetCompletions() + for pos,value in enumerate(args): + if value == "--complete": + if pos in bugid_args: + filter = bugid_args[pos] + bugshortnames = [] + try: + bd = bugdir.BugDir(from_disk=True, + manipulate_encodings=False) + bd.load_all_bugs() + bugs = [bug for bug in bd if filter(bug) == True] + bugshortnames = [bd.bug_shortname(bug) for bug in bugs] + except bugdir.NoBugDir: + bugshortnames = ["NOBUGDIR"] + pass + raise GetCompletions(bugshortnames) + raise GetCompletions() def underlined(instring): """Produces a version of a string that is underlined with '=' -- 2.26.2