/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to bzrlib/shellcomplete.py

  • Committer: Robert Collins
  • Date: 2005-10-16 00:22:17 UTC
  • mto: This revision was merged to the branch mainline in revision 1457.
  • Revision ID: robertc@lifelesslap.robertcollins.net-20051016002217-aa38f9c1eb13ee48
Plugins are now loaded under bzrlib.plugins, not bzrlib.plugin.

Plugins are also made available for other plugins to use by making them 
accessible via import bzrlib.plugins.NAME. You should not import other
plugins during the __init__ of your plugin though, as no ordering is
guaranteed, and the plugins directory is not on the python path.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
import sys
 
2
 
 
3
 
 
4
def shellcomplete(context=None, outfile = None):
 
5
    if outfile == None:
 
6
        outfile = sys.stdout
 
7
    if context == None:
 
8
        shellcomplete_commands(outfile = outfile)
 
9
    else:
 
10
        shellcomplete_on_command(context, outfile = outfile)
 
11
 
 
12
def shellcomplete_on_command(cmdname, outfile = None):
 
13
    cmdname = str(cmdname)
 
14
 
 
15
    if outfile == None:
 
16
        outfile = sys.stdout
 
17
 
 
18
    from inspect import getdoc
 
19
    import commands
 
20
    cmdobj = commands.get_cmd_object(cmdname)
 
21
 
 
22
    doc = getdoc(cmdobj)
 
23
    if doc == None:
 
24
        raise NotImplementedError("sorry, no detailed shellcomplete yet for %r" % cmdname)
 
25
 
 
26
    shellcomplete_on_option(cmdobj.takes_options, outfile = None)
 
27
    for aname in cmdobj.takes_args:
 
28
        outfile.write(aname + '\n')
 
29
 
 
30
 
 
31
def shellcomplete_on_option(options, outfile = None):
 
32
    import commands
 
33
    
 
34
    if not options:
 
35
        return
 
36
    
 
37
    if outfile == None:
 
38
        outfile = sys.stdout
 
39
 
 
40
    for on in options:
 
41
        for shortname, longname in commands.SHORT_OPTIONS.items():
 
42
            if longname == on:
 
43
                l = '"(--' + on + ' -' + shortname + ')"{--' + on + ',-' + shortname + '}'
 
44
                break
 
45
            else:
 
46
                l = '--' + on
 
47
        outfile.write(l + '\n')
 
48
 
 
49
 
 
50
def shellcomplete_commands(outfile = None):
 
51
    """List all commands"""
 
52
    import inspect
 
53
    import commands
 
54
    from inspect import getdoc
 
55
    
 
56
    if outfile == None:
 
57
        outfile = sys.stdout
 
58
    
 
59
    cmds = []
 
60
    for cmdname, cmdclass in commands.get_all_cmds():
 
61
        cmds.append((cmdname, cmdclass))
 
62
        for alias in cmdclass.aliases:
 
63
            cmds.append((alias, cmdclass))
 
64
    cmds.sort()
 
65
    for cmdname, cmdclass in cmds:
 
66
        if cmdclass.hidden:
 
67
            continue
 
68
        doc = getdoc(cmdclass)
 
69
        if doc == None:
 
70
            outfile.write(cmdname + '\n')
 
71
        else:
 
72
            doclines = doc.splitlines()
 
73
            firstline = doclines[0].lower()
 
74
            outfile.write(cmdname + ':' + firstline[0:-1] + '\n')