/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
7476.2.1 by Jelmer Vernooij
Default to running Python 3.
1
#!/usr/bin/env python3
0.27.2 by Martin von Gagern
First programmatic generation of completions.
2
5147.5.4 by Martin von Gagern
Assign copyright to Canonical Ltd.
3
# Copyright (C) 2009, 2010 Canonical Ltd
0.27.14 by Martin von Gagern
Added GPL 2 license document and copyright notice.
4
#
5147.5.5 by Martin von Gagern
Adjust copyright notice comments for bash_completion.
5
# This program is free software; you can redistribute it and/or modify
6
# it under the terms of the GNU General Public License as published by
7
# the Free Software Foundation; either version 2 of the License, or
8
# (at your option) any later version.
9
#
10
# This program is distributed in the hope that it will be useful,
11
# but WITHOUT ANY WARRANTY; without even the implied warranty of
12
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
# GNU General Public License for more details.
0.27.14 by Martin von Gagern
Added GPL 2 license document and copyright notice.
14
#
15
# You should have received a copy of the GNU General Public License
5147.5.5 by Martin von Gagern
Adjust copyright notice comments for bash_completion.
16
# along with this program; if not, write to the Free Software
17
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
0.27.14 by Martin von Gagern
Added GPL 2 license document and copyright notice.
18
6624 by Jelmer Vernooij
Merge Python3 porting work ('py3 pokes')
19
from ... import (
5245.1.1 by Andrew Bennetts
Fix deprecation warning in bash_completion plugin.
20
    cmdline,
0.28.2 by Martin von Gagern
merge from trunk
21
    commands,
22
    config,
23
    help_topics,
24
    option,
25
    plugin,
26
)
6622.1.34 by Jelmer Vernooij
Rename brzlib => breezy.
27
import breezy
0.27.18 by Martin von Gagern
Add global options in all cases
28
import re
6386.1.2 by Jelmer Vernooij
Move sys import.
29
import sys
0.27.2 by Martin von Gagern
First programmatic generation of completions.
30
0.32.8 by Martin von Gagern
Refactor to increase modularity.
31
32
class BashCodeGen(object):
33
    """Generate a bash script for given completion data."""
34
6622.1.28 by Jelmer Vernooij
More renames; commands in output, environment variables.
35
    def __init__(self, data, function_name='_brz', debug=False):
0.32.8 by Martin von Gagern
Refactor to increase modularity.
36
        self.data = data
37
        self.function_name = function_name
38
        self.debug = debug
39
40
    def script(self):
41
        return ("""\
6622.1.28 by Jelmer Vernooij
More renames; commands in output, environment variables.
42
# Programmable completion for the Breezy brz command under bash.
0.30.3 by Martin von Gagern
Lazy initialization
43
# Known to work with bash 2.05a as well as bash 4.1.2, and probably
44
# all versions in between as well.
0.27.1 by Martin von Gagern
Original script from bzr.dev/contrib/bash/bzr
45
46
# Based originally on the svn bash completition script.
47
# Customized by Sven Wilhelm/Icecrash.com
0.27.2 by Martin von Gagern
First programmatic generation of completions.
48
# Adjusted for automatic generation by Martin von Gagern
0.27.1 by Martin von Gagern
Original script from bzr.dev/contrib/bash/bzr
49
5147.5.12 by Martin von Gagern
Use version of bzrlib. Drop meta.py.
50
# Generated using the bash_completion plugin.
0.27.17 by Martin von Gagern
Mention version and homepage in generated shell code comment.
51
# See https://launchpad.net/bzr-bash-completion for details.
52
6622.1.28 by Jelmer Vernooij
More renames; commands in output, environment variables.
53
# Commands and options of brz %(brz_version)s
0.27.36 by Martin von Gagern
List versions of bzr and plugins.
54
0.27.20 by Martin von Gagern
More flexible handling of fixed word lists.
55
shopt -s progcomp
0.32.8 by Martin von Gagern
Refactor to increase modularity.
56
%(function)s
6622.1.28 by Jelmer Vernooij
More renames; commands in output, environment variables.
57
complete -F %(function_name)s -o default brz
7143.15.2 by Jelmer Vernooij
Run autopep8.
58
""" % {
0.32.8 by Martin von Gagern
Refactor to increase modularity.
59
            "function_name": self.function_name,
60
            "function": self.function(),
6622.1.28 by Jelmer Vernooij
More renames; commands in output, environment variables.
61
            "brz_version": self.brz_version(),
0.32.8 by Martin von Gagern
Refactor to increase modularity.
62
        })
63
64
    def function(self):
65
        return ("""\
0.27.4 by Martin von Gagern
Introduce --function-name switch. (LP #439829)
66
%(function_name)s ()
0.27.1 by Martin von Gagern
Original script from bzr.dev/contrib/bash/bzr
67
{
7143.16.1 by Jelmer Vernooij
Fix E101.
68
    local cur cmds cmdIdx cmd cmdOpts fixedWords i globalOpts
69
    local curOpt optEnums
70
    local IFS=$' \\n'
71
72
    COMPREPLY=()
73
    cur=${COMP_WORDS[COMP_CWORD]}
74
75
    cmds='%(cmds)s'
76
    globalOpts=( %(global_options)s )
77
78
    # do ordinary expansion if we are anywhere after a -- argument
79
    for ((i = 1; i < COMP_CWORD; ++i)); do
80
        [[ ${COMP_WORDS[i]} == "--" ]] && return 0
81
    done
82
83
    # find the command; it's the first word not starting in -
84
    cmd=
85
    for ((cmdIdx = 1; cmdIdx < ${#COMP_WORDS[@]}; ++cmdIdx)); do
86
        if [[ ${COMP_WORDS[cmdIdx]} != -* ]]; then
87
            cmd=${COMP_WORDS[cmdIdx]}
88
            break
89
        fi
90
    done
91
92
    # complete command name if we are not already past the command
93
    if [[ $COMP_CWORD -le cmdIdx ]]; then
94
        COMPREPLY=( $( compgen -W "$cmds ${globalOpts[*]}" -- $cur ) )
95
        return 0
96
    fi
97
98
    # find the option for which we want to complete a value
99
    curOpt=
100
    if [[ $cur != -* ]] && [[ $COMP_CWORD -gt 1 ]]; then
101
        curOpt=${COMP_WORDS[COMP_CWORD - 1]}
102
        if [[ $curOpt == = ]]; then
103
            curOpt=${COMP_WORDS[COMP_CWORD - 2]}
104
        elif [[ $cur == : ]]; then
105
            cur=
106
            curOpt="$curOpt:"
107
        elif [[ $curOpt == : ]]; then
108
            curOpt=${COMP_WORDS[COMP_CWORD - 2]}:
109
        fi
110
    fi
0.27.31 by Martin von Gagern
Introduce --debug switch to enable some debugging code.
111
%(debug)s
7143.16.1 by Jelmer Vernooij
Fix E101.
112
    cmdOpts=( )
113
    optEnums=( )
114
    fixedWords=( )
115
    case $cmd in
0.27.2 by Martin von Gagern
First programmatic generation of completions.
116
%(cases)s\
7143.16.1 by Jelmer Vernooij
Fix E101.
117
    *)
118
        cmdOpts=(--help -h)
119
        ;;
120
    esac
121
122
    IFS=$'\\n'
123
    if [[ ${#fixedWords[@]} -eq 0 ]] && [[ ${#optEnums[@]} -eq 0 ]] && [[ $cur != -* ]]; then
124
        case $curOpt in
125
            tag:|*..tag:)
126
                fixedWords=( $(brz tags 2>/dev/null | sed 's/  *[^ ]*$//; s/ /\\\\\\\\ /g;') )
127
                ;;
128
        esac
129
        case $cur in
130
            [\\"\\']tag:*)
131
                fixedWords=( $(brz tags 2>/dev/null | sed 's/  *[^ ]*$//; s/^/tag:/') )
132
                ;;
133
            [\\"\\']*..tag:*)
134
                fixedWords=( $(brz tags 2>/dev/null | sed 's/  *[^ ]*$//') )
135
                fixedWords=( $(for i in "${fixedWords[@]}"; do echo "${cur%%..tag:*}..tag:${i}"; done) )
136
                ;;
137
        esac
138
    elif [[ $cur == = ]] && [[ ${#optEnums[@]} -gt 0 ]]; then
139
        # complete directly after "--option=", list all enum values
140
        COMPREPLY=( "${optEnums[@]}" )
141
        return 0
142
    else
143
        fixedWords=( "${cmdOpts[@]}"
144
                     "${globalOpts[@]}"
145
                     "${optEnums[@]}"
146
                     "${fixedWords[@]}" )
147
    fi
148
149
    if [[ ${#fixedWords[@]} -gt 0 ]]; then
150
        COMPREPLY=( $( compgen -W "${fixedWords[*]}" -- $cur ) )
151
    fi
152
153
    return 0
0.27.1 by Martin von Gagern
Original script from bzr.dev/contrib/bash/bzr
154
}
7143.15.2 by Jelmer Vernooij
Run autopep8.
155
""" % {
0.32.8 by Martin von Gagern
Refactor to increase modularity.
156
            "cmds": self.command_names(),
157
            "function_name": self.function_name,
158
            "cases": self.command_cases(),
159
            "global_options": self.global_options(),
160
            "debug": self.debug_output(),
161
        })
0.32.20 by Martin von Gagern
Allow completion of tags at end of revision range.
162
        # Help Emacs terminate strings: "
0.32.8 by Martin von Gagern
Refactor to increase modularity.
163
164
    def command_names(self):
165
        return " ".join(self.data.all_command_aliases())
166
167
    def debug_output(self):
168
        if not self.debug:
169
            return ''
170
        else:
171
            return (r"""
7143.16.1 by Jelmer Vernooij
Fix E101.
172
    # Debugging code enabled using the --debug command line switch.
173
    # Will dump some variables to the top portion of the terminal.
174
    echo -ne '\e[s\e[H'
175
    for (( i=0; i < ${#COMP_WORDS[@]}; ++i)); do
176
        echo "\$COMP_WORDS[$i]='${COMP_WORDS[i]}'"$'\e[K'
177
    done
178
    for i in COMP_CWORD COMP_LINE COMP_POINT COMP_TYPE COMP_KEY cur curOpt; do
179
        echo "\$${i}=\"${!i}\""$'\e[K'
180
    done
181
    echo -ne '---\e[K\e[u'
0.32.8 by Martin von Gagern
Refactor to increase modularity.
182
""")
183
6622.1.28 by Jelmer Vernooij
More renames; commands in output, environment variables.
184
    def brz_version(self):
6622.1.34 by Jelmer Vernooij
Rename brzlib => breezy.
185
        brz_version = breezy.version_string
0.32.8 by Martin von Gagern
Refactor to increase modularity.
186
        if not self.data.plugins:
6622.1.28 by Jelmer Vernooij
More renames; commands in output, environment variables.
187
            brz_version += "."
0.32.8 by Martin von Gagern
Refactor to increase modularity.
188
        else:
6622.1.28 by Jelmer Vernooij
More renames; commands in output, environment variables.
189
            brz_version += " and the following plugins:"
6656.1.1 by Martin
Apply 2to3 dict fixer and clean up resulting mess using view helpers
190
            for name, plugin in sorted(self.data.plugins.items()):
6622.1.28 by Jelmer Vernooij
More renames; commands in output, environment variables.
191
                brz_version += "\n# %s" % plugin
192
        return brz_version
0.32.8 by Martin von Gagern
Refactor to increase modularity.
193
194
    def global_options(self):
195
        return " ".join(sorted(self.data.global_options))
196
197
    def command_cases(self):
198
        cases = ""
199
        for command in self.data.commands:
200
            cases += self.command_case(command)
201
        return cases
202
203
    def command_case(self, command):
204
        case = "\t%s)\n" % "|".join(command.aliases)
205
        if command.plugin:
206
            case += "\t\t# plugin \"%s\"\n" % command.plugin
207
        options = []
208
        enums = []
209
        for option in command.options:
210
            for message in option.error_messages:
211
                case += "\t\t# %s\n" % message
212
            if option.registry_keys:
213
                for key in option.registry_keys:
214
                    options.append("%s=%s" % (option, key))
0.32.15 by Martin von Gagern
Allow spaces in tag names. Use bash arrays more extensively.
215
                enums.append("%s) optEnums=( %s ) ;;" %
0.32.8 by Martin von Gagern
Refactor to increase modularity.
216
                             (option, ' '.join(option.registry_keys)))
217
            else:
218
                options.append(str(option))
0.32.15 by Martin von Gagern
Allow spaces in tag names. Use bash arrays more extensively.
219
        case += "\t\tcmdOpts=( %s )\n" % " ".join(options)
0.32.8 by Martin von Gagern
Refactor to increase modularity.
220
        if command.fixed_words:
221
            fixed_words = command.fixed_words
222
            if isinstance(fixed_words, list):
0.32.15 by Martin von Gagern
Allow spaces in tag names. Use bash arrays more extensively.
223
                fixed_words = "( %s )" + ' '.join(fixed_words)
0.32.8 by Martin von Gagern
Refactor to increase modularity.
224
            case += "\t\tfixedWords=%s\n" % fixed_words
225
        if enums:
226
            case += "\t\tcase $curOpt in\n\t\t\t"
227
            case += "\n\t\t\t".join(enums)
228
            case += "\n\t\tesac\n"
229
        case += "\t\t;;\n"
230
        return case
231
232
233
class CompletionData(object):
234
235
    def __init__(self):
236
        self.plugins = {}
237
        self.global_options = set()
238
        self.commands = []
239
240
    def all_command_aliases(self):
241
        for c in self.commands:
242
            for a in c.aliases:
243
                yield a
244
245
246
class CommandData(object):
247
248
    def __init__(self, name):
249
        self.name = name
250
        self.aliases = [name]
251
        self.plugin = None
252
        self.options = []
253
        self.fixed_words = None
254
255
256
class PluginData(object):
257
258
    def __init__(self, name, version=None):
259
        if version is None:
0.32.20 by Martin von Gagern
Allow completion of tags at end of revision range.
260
            try:
6622.1.34 by Jelmer Vernooij
Rename brzlib => breezy.
261
                version = breezy.plugin.plugins()[name].__version__
0.32.20 by Martin von Gagern
Allow completion of tags at end of revision range.
262
            except:
263
                version = 'unknown'
0.32.8 by Martin von Gagern
Refactor to increase modularity.
264
        self.name = name
265
        self.version = version
266
267
    def __str__(self):
268
        if self.version == 'unknown':
269
            return self.name
270
        return '%s %s' % (self.name, self.version)
271
272
273
class OptionData(object):
274
275
    def __init__(self, name):
276
        self.name = name
277
        self.registry_keys = None
278
        self.error_messages = []
279
280
    def __str__(self):
281
        return self.name
282
283
    def __cmp__(self, other):
284
        return cmp(self.name, other.name)
285
7027.6.1 by Jelmer Vernooij
Fix bash completion tests on Python 3.
286
    def __lt__(self, other):
287
        return self.name < other.name
288
0.32.8 by Martin von Gagern
Refactor to increase modularity.
289
290
class DataCollector(object):
291
292
    def __init__(self, no_plugins=False, selected_plugins=None):
293
        self.data = CompletionData()
294
        self.user_aliases = {}
295
        if no_plugins:
296
            self.selected_plugins = set()
297
        elif selected_plugins is None:
298
            self.selected_plugins = None
299
        else:
6619.3.12 by Jelmer Vernooij
Use 2to3 set_literal fixer.
300
            self.selected_plugins = {x.replace('-', '_')
7143.15.2 by Jelmer Vernooij
Run autopep8.
301
                                     for x in selected_plugins}
0.32.8 by Martin von Gagern
Refactor to increase modularity.
302
303
    def collect(self):
304
        self.global_options()
305
        self.aliases()
306
        self.commands()
307
        return self.data
308
309
    def global_options(self):
310
        re_switch = re.compile(r'\n(--[A-Za-z0-9-_]+)(?:, (-\S))?\s')
311
        help_text = help_topics.topic_registry.get_detail('global-options')
312
        for long, short in re_switch.findall(help_text):
313
            self.data.global_options.add(long)
314
            if short:
315
                self.data.global_options.add(short)
316
317
    def aliases(self):
6656.1.1 by Martin
Apply 2to3 dict fixer and clean up resulting mess using view helpers
318
        for alias, expansion in config.GlobalConfig().get_aliases().items():
5245.1.1 by Andrew Bennetts
Fix deprecation warning in bash_completion plugin.
319
            for token in cmdline.split(expansion):
0.32.8 by Martin von Gagern
Refactor to increase modularity.
320
                if not token.startswith("-"):
321
                    self.user_aliases.setdefault(token, set()).add(alias)
322
                    break
323
324
    def commands(self):
325
        for name in sorted(commands.all_command_names()):
326
            self.command(name)
327
328
    def command(self, name):
329
        cmd = commands.get_cmd_object(name)
330
        cmd_data = CommandData(name)
331
332
        plugin_name = cmd.plugin_name()
333
        if plugin_name is not None:
334
            if (self.selected_plugins is not None and
7143.15.2 by Jelmer Vernooij
Run autopep8.
335
                    plugin not in self.selected_plugins):
0.32.9 by Martin von Gagern
Added test cases for DataCollector and BashCodeGen.
336
                return None
0.32.8 by Martin von Gagern
Refactor to increase modularity.
337
            plugin_data = self.data.plugins.get(plugin_name)
338
            if plugin_data is None:
339
                plugin_data = PluginData(plugin_name)
340
                self.data.plugins[plugin_name] = plugin_data
341
            cmd_data.plugin = plugin_data
342
        self.data.commands.append(cmd_data)
0.27.12 by Martin von Gagern
Take user-configured aliases into account.
343
344
        # Find all aliases to the command; both cmd-defined and user-defined.
345
        # We assume a user won't override one command with a different one,
346
        # but will choose completely new names or add options to existing
347
        # ones while maintaining the actual command name unchanged.
0.32.8 by Martin von Gagern
Refactor to increase modularity.
348
        cmd_data.aliases.extend(cmd.aliases)
349
        cmd_data.aliases.extend(sorted([useralias
7143.15.2 by Jelmer Vernooij
Run autopep8.
350
                                        for cmdalias in cmd_data.aliases
351
                                        if cmdalias in self.user_aliases
352
                                        for useralias in self.user_aliases[cmdalias]
353
                                        if useralias not in cmd_data.aliases]))
0.32.8 by Martin von Gagern
Refactor to increase modularity.
354
0.27.2 by Martin von Gagern
First programmatic generation of completions.
355
        opts = cmd.options()
6656.1.1 by Martin
Apply 2to3 dict fixer and clean up resulting mess using view helpers
356
        for optname, opt in sorted(opts.items()):
0.32.8 by Martin von Gagern
Refactor to increase modularity.
357
            cmd_data.options.extend(self.option(opt))
358
359
        if 'help' == name or 'help' in cmd.aliases:
0.32.15 by Martin von Gagern
Allow spaces in tag names. Use bash arrays more extensively.
360
            cmd_data.fixed_words = ('($cmds %s)' %
7143.15.2 by Jelmer Vernooij
Run autopep8.
361
                                    " ".join(sorted(help_topics.topic_registry.keys())))
0.32.8 by Martin von Gagern
Refactor to increase modularity.
362
0.32.9 by Martin von Gagern
Added test cases for DataCollector and BashCodeGen.
363
        return cmd_data
364
0.32.8 by Martin von Gagern
Refactor to increase modularity.
365
    def option(self, opt):
366
        optswitches = {}
7045.4.23 by Jelmer Vernooij
Fix some help tests.
367
        parser = option.get_optparser([opt])
0.32.8 by Martin von Gagern
Refactor to increase modularity.
368
        parser = self.wrap_parser(optswitches, parser)
369
        optswitches.clear()
370
        opt.add_option(parser, opt.short_name())
371
        if isinstance(opt, option.RegistryOption) and opt.enum_switch:
372
            enum_switch = '--%s' % opt.name
373
            enum_data = optswitches.get(enum_switch)
374
            if enum_data:
0.27.41 by Martin von Gagern
Recover from ImportError when loading a registry.
375
                try:
0.32.8 by Martin von Gagern
Refactor to increase modularity.
376
                    enum_data.registry_keys = opt.registry.keys()
6619.3.2 by Jelmer Vernooij
Apply 2to3 except fix.
377
                except ImportError as e:
0.32.8 by Martin von Gagern
Refactor to increase modularity.
378
                    enum_data.error_messages.append(
379
                        "ERROR getting registry keys for '--%s': %s"
380
                        % (opt.name, str(e).split('\n')[0]))
381
        return sorted(optswitches.values())
382
383
    def wrap_container(self, optswitches, parser):
384
        def tweaked_add_option(*opts, **attrs):
385
            for name in opts:
386
                optswitches[name] = OptionData(name)
387
        parser.add_option = tweaked_add_option
388
        return parser
389
390
    def wrap_parser(self, optswitches, parser):
391
        orig_add_option_group = parser.add_option_group
7143.15.2 by Jelmer Vernooij
Run autopep8.
392
0.32.8 by Martin von Gagern
Refactor to increase modularity.
393
        def tweaked_add_option_group(*opts, **attrs):
394
            return self.wrap_container(optswitches,
7143.15.2 by Jelmer Vernooij
Run autopep8.
395
                                       orig_add_option_group(*opts, **attrs))
0.32.8 by Martin von Gagern
Refactor to increase modularity.
396
        parser.add_option_group = tweaked_add_option_group
397
        return self.wrap_container(optswitches, parser)
398
399
6622.1.28 by Jelmer Vernooij
More renames; commands in output, environment variables.
400
def bash_completion_function(out, function_name="_brz", function_only=False,
0.32.8 by Martin von Gagern
Refactor to increase modularity.
401
                             debug=False,
402
                             no_plugins=False, selected_plugins=None):
7143.15.2 by Jelmer Vernooij
Run autopep8.
403
    dc = DataCollector(no_plugins=no_plugins,
404
                       selected_plugins=selected_plugins)
0.32.8 by Martin von Gagern
Refactor to increase modularity.
405
    data = dc.collect()
406
    cg = BashCodeGen(data, function_name=function_name, debug=debug)
0.27.7 by Martin von Gagern
Introduce --function-only option
407
    if function_only:
0.32.8 by Martin von Gagern
Refactor to increase modularity.
408
        res = cg.function()
409
    else:
410
        res = cg.script()
411
    out.write(res)
412
0.27.2 by Martin von Gagern
First programmatic generation of completions.
413
5147.5.11 by Martin von Gagern
Register command lazily.
414
class cmd_bash_completion(commands.Command):
5147.5.14 by Martin von Gagern
Assign user-visible docstrings to __doc__.
415
    __doc__ = """Generate a shell function for bash command line completion.
5147.5.11 by Martin von Gagern
Register command lazily.
416
417
    This command generates a shell function which can be used by bash to
418
    automatically complete the currently typed command when the user presses
419
    the completion key (usually tab).
7195.5.1 by Martin
Fix remaining whitespace lint in codebase
420
5147.5.11 by Martin von Gagern
Register command lazily.
421
    Commonly used like this:
6622.1.28 by Jelmer Vernooij
More renames; commands in output, environment variables.
422
        eval "`brz bash-completion`"
5147.5.11 by Martin von Gagern
Register command lazily.
423
    """
424
425
    takes_options = [
7479.2.1 by Jelmer Vernooij
Drop python2 support.
426
        option.Option("function-name", short_name="f", type=str, argname="name",
7143.15.2 by Jelmer Vernooij
Run autopep8.
427
                      help="Name of the generated function (default: _brz)"),
5147.5.11 by Martin von Gagern
Register command lazily.
428
        option.Option("function-only", short_name="o", type=None,
7143.15.2 by Jelmer Vernooij
Run autopep8.
429
                      help="Generate only the shell function, don't enable it"),
5147.5.11 by Martin von Gagern
Register command lazily.
430
        option.Option("debug", type=None, hidden=True,
7143.15.2 by Jelmer Vernooij
Run autopep8.
431
                      help="Enable shell code useful for debugging"),
7479.2.1 by Jelmer Vernooij
Drop python2 support.
432
        option.ListOption("plugin", type=str, argname="name",
7143.15.2 by Jelmer Vernooij
Run autopep8.
433
                          # param_name="selected_plugins", # doesn't work, bug #387117
434
                          help="Enable completions for the selected plugin"
435
                          + " (default: all plugins)"),
5147.5.11 by Martin von Gagern
Register command lazily.
436
        ]
437
438
    def run(self, **kwargs):
439
        if 'plugin' in kwargs:
440
            # work around bug #387117 which prevents us from using param_name
5147.5.18 by Martin von Gagern
Include all plugins if --plugins isn't specified (fixes regression).
441
            if len(kwargs['plugin']) > 0:
442
                kwargs['selected_plugins'] = kwargs['plugin']
5147.5.11 by Martin von Gagern
Register command lazily.
443
            del kwargs['plugin']
444
        bash_completion_function(sys.stdout, **kwargs)
445
446
0.27.2 by Martin von Gagern
First programmatic generation of completions.
447
if __name__ == '__main__':
448
449
    import locale
0.27.32 by Martin von Gagern
Added OptionParser for script usage.
450
    import optparse
451
0.27.38 by Martin von Gagern
Add --plugin switch to selectively include plugins.
452
    def plugin_callback(option, opt, value, parser):
453
        values = parser.values.selected_plugins
454
        if value == '-':
455
            del values[:]
456
        else:
457
            values.append(value)
458
5147.5.12 by Martin von Gagern
Use version of bzrlib. Drop meta.py.
459
    parser = optparse.OptionParser(usage="%prog [-f NAME] [-o]")
0.27.32 by Martin von Gagern
Added OptionParser for script usage.
460
    parser.add_option("--function-name", "-f", metavar="NAME",
6622.1.28 by Jelmer Vernooij
More renames; commands in output, environment variables.
461
                      help="Name of the generated function (default: _brz)")
0.27.32 by Martin von Gagern
Added OptionParser for script usage.
462
    parser.add_option("--function-only", "-o", action="store_true",
463
                      help="Generate only the shell function, don't enable it")
464
    parser.add_option("--debug", action="store_true",
465
                      help=optparse.SUPPRESS_HELP)
0.27.37 by Martin von Gagern
Introduce --no-plugins option to script execution mode.
466
    parser.add_option("--no-plugins", action="store_true",
6622.1.28 by Jelmer Vernooij
More renames; commands in output, environment variables.
467
                      help="Don't load any brz plugins")
0.27.38 by Martin von Gagern
Add --plugin switch to selectively include plugins.
468
    parser.add_option("--plugin", metavar="NAME", type="string",
469
                      dest="selected_plugins", default=[],
470
                      action="callback", callback=plugin_callback,
471
                      help="Enable completions for the selected plugin"
472
                      + " (default: all plugins)")
0.27.32 by Martin von Gagern
Added OptionParser for script usage.
473
    (opts, args) = parser.parse_args()
474
    if args:
475
        parser.error("script does not take positional arguments")
476
    kwargs = dict()
6656.1.1 by Martin
Apply 2to3 dict fixer and clean up resulting mess using view helpers
477
    for name, value in opts.__dict__.items():
0.27.32 by Martin von Gagern
Added OptionParser for script usage.
478
        if value is not None:
479
            kwargs[name] = value
0.27.2 by Martin von Gagern
First programmatic generation of completions.
480
481
    locale.setlocale(locale.LC_ALL, '')
0.27.37 by Martin von Gagern
Introduce --no-plugins option to script execution mode.
482
    if not kwargs.get('no_plugins', False):
483
        plugin.load_plugins()
0.27.2 by Martin von Gagern
First programmatic generation of completions.
484
    commands.install_bzr_command_hooks()
0.27.32 by Martin von Gagern
Added OptionParser for script usage.
485
    bash_completion_function(sys.stdout, **kwargs)