~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/help.py

  • Committer: John Arbash Meinel
  • Date: 2006-10-11 00:23:23 UTC
  • mfrom: (2070 +trunk)
  • mto: This revision was merged to the branch mainline in revision 2071.
  • Revision ID: john@arbash-meinel.com-20061011002323-82ba88c293d7caff
[merge] bzr.dev 2070

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
# Copyright (C) 2004, 2005, 2006 by Canonical Ltd
2
 
 
 
2
#
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
5
5
# the Free Software Foundation; either version 2 of the License, or
6
6
# (at your option) any later version.
7
 
 
 
7
#
8
8
# This program is distributed in the hope that it will be useful,
9
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
11
# GNU General Public License for more details.
12
 
 
 
12
#
13
13
# You should have received a copy of the GNU General Public License
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
21
21
 
22
22
# TODO: `help commands --all` should show hidden commands
23
23
import textwrap
 
24
from bzrlib import osutils 
24
25
 
25
26
global_help = \
26
 
"""Bazaar-NG -- a free distributed version-control tool
 
27
"""Bazaar -- a free distributed version-control tool
27
28
http://bazaar-vcs.org/
28
29
 
29
30
Basic commands:
53
54
 
54
55
 
55
56
def help(topic=None, outfile = None):
56
 
    if outfile == None:
 
57
    if outfile is None:
57
58
        outfile = sys.stdout
58
 
    if topic == None:
 
59
    if topic is None:
59
60
        outfile.write(global_help)
60
61
    elif topic == 'commands':
61
62
        help_commands(outfile = outfile)
102
103
 
103
104
    cmdname = str(cmdname)
104
105
 
105
 
    if outfile == None:
 
106
    if outfile is None:
106
107
        outfile = sys.stdout
107
108
 
108
109
    cmd_object = get_cmd_object(cmdname)
109
110
 
110
111
    doc = cmd_object.help()
111
 
    if doc == None:
 
112
    if doc is None:
112
113
        raise NotImplementedError("sorry, no detailed help yet for %r" % cmdname)
113
114
 
114
115
    print >>outfile, 'usage:', command_usage(cmd_object) 
128
129
 
129
130
 
130
131
def help_on_command_options(cmd, outfile=None):
131
 
    from bzrlib.option import Option
 
132
    from bzrlib.option import Option, get_optparser
 
133
    if outfile is None:
 
134
        outfile = sys.stdout
132
135
    options = cmd.options()
133
 
    if not options:
134
 
        return
135
 
    if outfile == None:
136
 
        outfile = sys.stdout
137
 
    outfile.write('\noptions:\n')
138
 
    for option_name, option in sorted(options.items()):
139
 
        l = '    --' + option_name
140
 
        if option.type is not None:
141
 
            l += ' ' + option.argname.upper()
142
 
        short_name = option.short_name()
143
 
        if short_name:
144
 
            assert len(short_name) == 1
145
 
            l += ', -' + short_name
146
 
        l += (30 - len(l)) * ' ' + option.help
147
 
        # TODO: split help over multiple lines with correct indenting and 
148
 
        # wrapping
149
 
        wrapped = textwrap.fill(l, initial_indent='', subsequent_indent=30*' ')
150
 
        outfile.write(wrapped + '\n')
 
136
    outfile.write('\n')
 
137
    outfile.write(get_optparser(options).format_option_help())
151
138
 
152
139
 
153
140
def help_commands(outfile=None):
155
142
    from bzrlib.commands import (builtin_command_names,
156
143
                                 plugin_command_names,
157
144
                                 get_cmd_object)
158
 
 
159
 
    if outfile == None:
 
145
    if outfile is None:
160
146
        outfile = sys.stdout
161
 
 
162
 
    names = set()                       # to eliminate duplicates
163
 
    names.update(builtin_command_names())
 
147
    names = set(builtin_command_names()) # to eliminate duplicates
164
148
    names.update(plugin_command_names())
165
 
    names = list(names)
166
 
    names.sort()
167
 
 
168
 
    for cmd_name in names:
169
 
        cmd_object = get_cmd_object(cmd_name)
170
 
        if cmd_object.hidden:
171
 
            continue
172
 
        print >>outfile, command_usage(cmd_object)
173
 
 
 
149
    commands = ((n, get_cmd_object(n)) for n in names)
 
150
    shown_commands = [(n, o) for n, o in commands if not o.hidden]
 
151
    max_name = max(len(n) for n, o in shown_commands)
 
152
    indent = ' ' * (max_name + 1)
 
153
    width = osutils.terminal_width() - 1
 
154
    for cmd_name, cmd_object in sorted(shown_commands):
174
155
        plugin_name = cmd_object.plugin_name()
175
 
        print_command_plugin(cmd_object, outfile, '        %s\n')
 
156
        if plugin_name is None:
 
157
            plugin_name = ''
 
158
        else:
 
159
            plugin_name = ' [%s]' % plugin_name
176
160
 
177
161
        cmd_help = cmd_object.help()
178
162
        if cmd_help:
179
163
            firstline = cmd_help.split('\n', 1)[0]
180
 
            print >>outfile, '        ' + firstline
181
 
        
 
164
        else:
 
165
            firstline = ''
 
166
        helpstring = '%-*s %s%s' % (max_name, cmd_name, firstline, plugin_name)
 
167
        lines = textwrap.wrap(helpstring, subsequent_indent=indent,
 
168
                              width=width)
 
169
        for line in lines:
 
170
            outfile.write(line + '\n')