~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bashcomp.py

Add --plugin switch to selectively include plugins.

This is intended for scenarios where a bash completion script is generated
once but distributed to a number of sifferent bzr setups.  In this case the
list of plugins for which commands are included in the completion function
can be controlled and used to restrict the function to a fixed set of
plugins.  Other aspects of command line completion, in particular registry
values registered by plugins, are not affected by this switch.

Show diffs side-by-side

added added

removed removed

Lines of Context:
143
143
    return wrap_container(list, parser)
144
144
 
145
145
def bash_completion_function(out, function_name="_bzr", function_only=False,
146
 
                             debug=False, no_plugins=False):
 
146
                             debug=False,
 
147
                             no_plugins=False, selected_plugins=None):
147
148
    cmds = []
148
149
    cases = ""
149
150
    reqarg = {}
150
151
    plugins = set()
 
152
    if selected_plugins:
 
153
        selected_plugins = set([x.replace('-', '_') for x in selected_plugins])
 
154
    else:
 
155
        selected_plugins = None
151
156
 
152
157
    re_switch = re.compile(r'\n(--[A-Za-z0-9-_]+)(?:, (-\S))?\s')
153
158
    help_text = help_topics.topic_registry.get_detail('global-options')
184
189
        cmds.extend(aliases)
185
190
        plugin = cmd.plugin_name()
186
191
        if plugin is not None:
 
192
            if selected_plugins is not None and plugin not in selected_plugins:
 
193
                continue
187
194
            plugins.add(plugin)
188
195
            cases += "\t\t# plugin \"%s\"\n" % plugin
189
196
        opts = cmd.options()
255
262
    import optparse
256
263
    from meta import __version__
257
264
 
 
265
    def plugin_callback(option, opt, value, parser):
 
266
        values = parser.values.selected_plugins
 
267
        if value == '-':
 
268
            del values[:]
 
269
        else:
 
270
            values.append(value)
 
271
 
258
272
    parser = optparse.OptionParser(usage="%prog [-f NAME] [-o]",
259
273
                                   version="%%prog %s" % __version__)
260
274
    parser.add_option("--function-name", "-f", metavar="NAME",
265
279
                      help=optparse.SUPPRESS_HELP)
266
280
    parser.add_option("--no-plugins", action="store_true",
267
281
                      help="Don't load any bzr plugins")
 
282
    parser.add_option("--plugin", metavar="NAME", type="string",
 
283
                      dest="selected_plugins", default=[],
 
284
                      action="callback", callback=plugin_callback,
 
285
                      help="Enable completions for the selected plugin"
 
286
                      + " (default: all plugins)")
268
287
    (opts, args) = parser.parse_args()
269
288
    if args:
270
289
        parser.error("script does not take positional arguments")