/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/help.py

  • Committer: Martin Pool
  • Date: 2006-10-06 02:04:17 UTC
  • mfrom: (1908.10.1 bench_usecases.merge2)
  • mto: This revision was merged to the branch mainline in revision 2068.
  • Revision ID: mbp@sourcefrog.net-20061006020417-4949ca86f4417a4d
merge additional fix from cfbolz

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2004, 2005 by Canonical Ltd
2
 
 
 
1
# Copyright (C) 2004, 2005, 2006 by Canonical Ltd
 
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
 
http://bazaar-ng.org/
28
 
 
29
 
WARNING: This is an unstable development version.
30
 
         Please keep backups.
 
27
"""Bazaar -- a free distributed version-control tool
 
28
http://bazaar-vcs.org/
31
29
 
32
30
Basic commands:
33
31
 
56
54
 
57
55
 
58
56
def help(topic=None, outfile = None):
59
 
    if outfile == None:
 
57
    if outfile is None:
60
58
        outfile = sys.stdout
61
 
    if topic == None:
 
59
    if topic is None:
62
60
        outfile.write(global_help)
63
61
    elif topic == 'commands':
64
62
        help_commands(outfile = outfile)
88
86
    return s
89
87
 
90
88
 
 
89
def print_command_plugin(cmd_object, outfile, format):
 
90
    """Print the plugin that provides a command object, if any.
 
91
 
 
92
    If the cmd_object is provided by a plugin, prints the plugin name to
 
93
    outfile using the provided format string.
 
94
    """
 
95
    plugin_name = cmd_object.plugin_name()
 
96
    if plugin_name is not None:
 
97
        out_str = '(From plugin "%s")' % plugin_name
 
98
        outfile.write(format % out_str)
 
99
 
 
100
 
91
101
def help_on_command(cmdname, outfile=None):
92
102
    from bzrlib.commands import get_cmd_object
93
103
 
94
104
    cmdname = str(cmdname)
95
105
 
96
 
    if outfile == None:
 
106
    if outfile is None:
97
107
        outfile = sys.stdout
98
108
 
99
109
    cmd_object = get_cmd_object(cmdname)
100
110
 
101
111
    doc = cmd_object.help()
102
 
    if doc == None:
 
112
    if doc is None:
103
113
        raise NotImplementedError("sorry, no detailed help yet for %r" % cmdname)
104
114
 
105
115
    print >>outfile, 'usage:', command_usage(cmd_object) 
110
120
 
111
121
    print >>outfile
112
122
 
 
123
    print_command_plugin(cmd_object, outfile, '%s\n\n')
 
124
 
113
125
    outfile.write(doc)
114
126
    if doc[-1] != '\n':
115
127
        outfile.write('\n')
117
129
 
118
130
 
119
131
def help_on_command_options(cmd, outfile=None):
120
 
    from bzrlib.option import Option
 
132
    from bzrlib.option import Option, get_optparser
 
133
    if outfile is None:
 
134
        outfile = sys.stdout
121
135
    options = cmd.options()
122
 
    if not options:
123
 
        return
124
 
    if outfile == None:
125
 
        outfile = sys.stdout
126
 
    outfile.write('\noptions:\n')
127
 
    for option_name, option in sorted(options.items()):
128
 
        l = '    --' + option_name
129
 
        if option.type is not None:
130
 
            l += ' ' + option.argname.upper()
131
 
        short_name = option.short_name()
132
 
        if short_name:
133
 
            assert len(short_name) == 1
134
 
            l += ', -' + short_name
135
 
        l += (30 - len(l)) * ' ' + option.help
136
 
        # TODO: split help over multiple lines with correct indenting and 
137
 
        # wrapping
138
 
        wrapped = textwrap.fill(l, initial_indent='', subsequent_indent=30*' ')
139
 
        outfile.write(wrapped + '\n')
 
136
    outfile.write('\n')
 
137
    outfile.write(get_optparser(options).format_option_help())
140
138
 
141
139
 
142
140
def help_commands(outfile=None):
144
142
    from bzrlib.commands import (builtin_command_names,
145
143
                                 plugin_command_names,
146
144
                                 get_cmd_object)
147
 
 
148
 
    if outfile == None:
 
145
    if outfile is None:
149
146
        outfile = sys.stdout
150
 
 
151
 
    names = set()                       # to eliminate duplicates
152
 
    names.update(builtin_command_names())
 
147
    names = set(builtin_command_names()) # to eliminate duplicates
153
148
    names.update(plugin_command_names())
154
 
    names = list(names)
155
 
    names.sort()
 
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):
 
155
        plugin_name = cmd_object.plugin_name()
 
156
        if plugin_name is None:
 
157
            plugin_name = ''
 
158
        else:
 
159
            plugin_name = ' [%s]' % plugin_name
156
160
 
157
 
    for cmd_name in names:
158
 
        cmd_object = get_cmd_object(cmd_name)
159
 
        if cmd_object.hidden:
160
 
            continue
161
 
        print >>outfile, command_usage(cmd_object)
162
161
        cmd_help = cmd_object.help()
163
162
        if cmd_help:
164
163
            firstline = cmd_help.split('\n', 1)[0]
165
 
            print >>outfile, '        ' + firstline
166
 
        
 
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')