~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/help.py

- merge improved merge base selection from aaron
aaron.bentley@utoronto.ca-20050912025534-43d7275dd948e4ad

Show diffs side-by-side

added added

removed removed

Lines of Context:
14
14
# along with this program; if not, write to the Free Software
15
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
16
 
 
17
# TODO: Some way to get a list of external commands (defined by shell
 
18
# scripts) so that they can be included in the help listing as well.
 
19
# It should be enough to just list the plugin directory and look for
 
20
# executable files with reasonable names.
 
21
 
 
22
# TODO: `help commands --all` should show hidden commands
 
23
 
17
24
global_help = \
18
25
"""Bazaar-NG -- a free distributed version-control tool
19
26
http://bazaar-ng.org/
58
65
        help_on_command(topic, outfile = outfile)
59
66
 
60
67
 
61
 
def command_usage(cmdname, cmdclass):
 
68
def command_usage(cmd_object):
62
69
    """Return single-line grammar for command.
63
70
 
64
71
    Only describes arguments, not options.
65
72
    """
66
 
    s = cmdname + ' '
67
 
    for aname in cmdclass.takes_args:
 
73
    s = 'bzr ' + cmd_object.name() + ' '
 
74
    for aname in cmd_object.takes_args:
68
75
        aname = aname.upper()
69
76
        if aname[-1] in ['$', '+']:
70
77
            aname = aname[:-1] + '...'
80
87
    return s
81
88
 
82
89
 
83
 
def help_on_command(cmdname, outfile = None):
 
90
def help_on_command(cmdname, outfile=None):
 
91
    from bzrlib.commands import get_cmd_object
 
92
 
84
93
    cmdname = str(cmdname)
85
94
 
86
95
    if outfile == None:
87
96
        outfile = sys.stdout
88
97
 
89
 
    from inspect import getdoc
90
 
    import commands
91
 
    topic, cmdclass = commands.get_cmd_class(cmdname)
 
98
    cmd_object = get_cmd_object(cmdname)
92
99
 
93
 
    doc = getdoc(cmdclass)
 
100
    doc = cmd_object.help()
94
101
    if doc == None:
95
102
        raise NotImplementedError("sorry, no detailed help yet for %r" % cmdname)
96
103
 
97
 
    outfile.write('usage: ' + command_usage(topic, cmdclass) + '\n')
98
 
 
99
 
    if cmdclass.aliases:
100
 
        outfile.write('aliases: ' + ', '.join(cmdclass.aliases) + '\n')
101
 
    
 
104
    print >>outfile, 'usage:', command_usage(cmd_object) 
 
105
 
 
106
    if cmd_object.aliases:
 
107
        print >>outfile, 'aliases:',
 
108
        print >>outfile, ', '.join(cmd_object.aliases)
 
109
 
 
110
    print >>outfile
 
111
 
102
112
    outfile.write(doc)
103
113
    if doc[-1] != '\n':
104
114
        outfile.write('\n')
105
115
    
106
 
    help_on_option(cmdclass.takes_options, outfile = None)
107
 
 
108
 
 
109
 
def help_on_option(options, outfile = None):
110
 
    import commands
 
116
    help_on_options(cmd_object.takes_options, outfile=None)
 
117
 
 
118
 
 
119
def help_on_options(options, outfile=None):
 
120
    from bzrlib.commands import SHORT_OPTIONS
111
121
    
112
122
    if not options:
113
123
        return
118
128
    outfile.write('\noptions:\n')
119
129
    for on in options:
120
130
        l = '    --' + on
121
 
        for shortname, longname in commands.SHORT_OPTIONS.items():
 
131
        for shortname, longname in SHORT_OPTIONS.items():
122
132
            if longname == on:
123
133
                l += ', -' + shortname
124
134
                break
125
135
        outfile.write(l + '\n')
126
136
 
127
137
 
128
 
def help_commands(outfile = None):
 
138
def help_commands(outfile=None):
129
139
    """List all commands"""
130
 
    import inspect
131
 
    import commands
 
140
    from bzrlib.commands import (builtin_command_names,
 
141
                                 plugin_command_names,
 
142
                                 get_cmd_object)
132
143
 
133
144
    if outfile == None:
134
145
        outfile = sys.stdout
135
 
    
136
 
    accu = []
137
 
    for cmdname, cmdclass in commands.get_all_cmds():
138
 
        accu.append((cmdname, cmdclass))
139
 
    accu.sort()
140
 
    for cmdname, cmdclass in accu:
141
 
        if cmdclass.hidden:
 
146
 
 
147
    names = set()                       # to eliminate duplicates
 
148
    names.update(builtin_command_names())
 
149
    names.update(plugin_command_names())
 
150
    names = list(names)
 
151
    names.sort()
 
152
 
 
153
    for cmd_name in names:
 
154
        cmd_object = get_cmd_object(cmd_name)
 
155
        if cmd_object.hidden:
142
156
            continue
143
 
        outfile.write(command_usage(cmdname, cmdclass) + '\n')
144
 
        help = inspect.getdoc(cmdclass)
145
 
        if help:
146
 
            outfile.write("    " + help.split('\n', 1)[0] + '\n')
147
 
 
148
 
            
149
 
 
 
157
        print >>outfile, command_usage(cmd_object)
 
158
        cmd_help = cmd_object.help()
 
159
        if cmd_help:
 
160
            firstline = cmd_help.split('\n', 1)[0]
 
161
            print >>outfile, '    ' + firstline
 
162