~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/help.py

  • Committer: Alexander Belchenko
  • Date: 2006-07-31 16:12:57 UTC
  • mto: (1711.2.111 jam-integration)
  • mto: This revision was merged to the branch mainline in revision 1906.
  • Revision ID: bialix@ukr.net-20060731161257-91a231523255332c
new official bzr.ico

Show diffs side-by-side

added added

removed removed

Lines of Context:
21
21
 
22
22
# TODO: `help commands --all` should show hidden commands
23
23
import textwrap
24
 
from bzrlib import osutils 
25
24
 
26
25
global_help = \
27
26
"""Bazaar -- a free distributed version-control tool
54
53
 
55
54
 
56
55
def help(topic=None, outfile = None):
57
 
    if outfile is None:
 
56
    if outfile == None:
58
57
        outfile = sys.stdout
59
 
    if topic is None:
 
58
    if topic == None:
60
59
        outfile.write(global_help)
61
60
    elif topic == 'commands':
62
61
        help_commands(outfile = outfile)
103
102
 
104
103
    cmdname = str(cmdname)
105
104
 
106
 
    if outfile is None:
 
105
    if outfile == None:
107
106
        outfile = sys.stdout
108
107
 
109
108
    cmd_object = get_cmd_object(cmdname)
110
109
 
111
110
    doc = cmd_object.help()
112
 
    if doc is None:
 
111
    if doc == None:
113
112
        raise NotImplementedError("sorry, no detailed help yet for %r" % cmdname)
114
113
 
115
114
    print >>outfile, 'usage:', command_usage(cmd_object) 
129
128
 
130
129
 
131
130
def help_on_command_options(cmd, outfile=None):
132
 
    from bzrlib.option import Option, get_optparser
133
 
    if outfile is None:
134
 
        outfile = sys.stdout
 
131
    from bzrlib.option import Option
135
132
    options = cmd.options()
136
 
    outfile.write('\n')
137
 
    outfile.write(get_optparser(options).format_option_help())
 
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')
138
151
 
139
152
 
140
153
def help_commands(outfile=None):
142
155
    from bzrlib.commands import (builtin_command_names,
143
156
                                 plugin_command_names,
144
157
                                 get_cmd_object)
145
 
    if outfile is None:
 
158
 
 
159
    if outfile == None:
146
160
        outfile = sys.stdout
147
 
    names = set(builtin_command_names()) # to eliminate duplicates
 
161
 
 
162
    names = set()                       # to eliminate duplicates
 
163
    names.update(builtin_command_names())
148
164
    names.update(plugin_command_names())
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):
 
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
 
155
174
        plugin_name = cmd_object.plugin_name()
156
 
        if plugin_name is None:
157
 
            plugin_name = ''
158
 
        else:
159
 
            plugin_name = ' [%s]' % plugin_name
 
175
        print_command_plugin(cmd_object, outfile, '        %s\n')
160
176
 
161
177
        cmd_help = cmd_object.help()
162
178
        if cmd_help:
163
179
            firstline = cmd_help.split('\n', 1)[0]
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')
 
180
            print >>outfile, '        ' + firstline
 
181