~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/help.py

[merge] bzr.dev 2255

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2004, 2005, 2006 by Canonical Ltd
 
1
# Copyright (C) 2004, 2005, 2006 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
20
20
# executable files with reasonable names.
21
21
 
22
22
# TODO: `help commands --all` should show hidden commands
 
23
 
 
24
import sys
23
25
import textwrap
24
 
from bzrlib import osutils 
25
 
 
26
 
global_help = \
27
 
"""Bazaar -- a free distributed version-control tool
28
 
http://bazaar-vcs.org/
29
 
 
30
 
Basic commands:
31
 
 
32
 
  bzr init           makes this directory a versioned branch
33
 
  bzr branch         make a copy of another branch
34
 
 
35
 
  bzr add            make files or directories versioned
36
 
  bzr ignore         ignore a file or pattern
37
 
  bzr mv             move or rename a versioned file
38
 
 
39
 
  bzr status         summarize changes in working copy
40
 
  bzr diff           show detailed diffs
41
 
 
42
 
  bzr merge          pull in changes from another branch
43
 
  bzr commit         save some or all changes
44
 
 
45
 
  bzr log            show history of changes
46
 
  bzr check          validate storage
47
 
 
48
 
  bzr help init      more help on e.g. init command
49
 
  bzr help commands  list all commands
50
 
"""
51
 
 
52
 
 
53
 
import sys
54
 
 
55
 
 
56
 
def help(topic=None, outfile = None):
 
26
 
 
27
from bzrlib import (
 
28
    help_topics,
 
29
    osutils,
 
30
    )
 
31
 
 
32
 
 
33
def help(topic=None, outfile=None):
 
34
    """Write the help for the specific topic to outfile"""
57
35
    if outfile is None:
58
36
        outfile = sys.stdout
 
37
 
59
38
    if topic is None:
60
 
        outfile.write(global_help)
61
 
    elif topic == 'commands':
62
 
        help_commands(outfile = outfile)
 
39
        topic = 'basic'
 
40
 
 
41
    if topic in help_topics.topic_registry:
 
42
        txt = help_topics.topic_registry.get_detail(topic)
 
43
        outfile.write(txt)
63
44
    else:
64
 
        help_on_command(topic, outfile = outfile)
 
45
        help_on_command(topic, outfile=outfile)
65
46
 
66
47
 
67
48
def command_usage(cmd_object):
112
93
    if doc is None:
113
94
        raise NotImplementedError("sorry, no detailed help yet for %r" % cmdname)
114
95
 
115
 
    print >>outfile, 'usage:', command_usage(cmd_object) 
 
96
    print >>outfile, 'usage:', command_usage(cmd_object)
116
97
 
117
98
    if cmd_object.aliases:
118
99
        print >>outfile, 'aliases:',
139
120
 
140
121
def help_commands(outfile=None):
141
122
    """List all commands"""
 
123
    if outfile is None:
 
124
        outfile = sys.stdout
 
125
    outfile.write(_help_commands_to_text('commands'))
 
126
 
 
127
 
 
128
def _help_commands_to_text(topic):
 
129
    """Generate the help text for the list of commands"""
142
130
    from bzrlib.commands import (builtin_command_names,
143
131
                                 plugin_command_names,
144
132
                                 get_cmd_object)
145
 
    if outfile is None:
146
 
        outfile = sys.stdout
 
133
    out = []
 
134
    if topic == 'hidden-commands':
 
135
        hidden = True
 
136
    else:
 
137
        hidden = False
147
138
    names = set(builtin_command_names()) # to eliminate duplicates
148
139
    names.update(plugin_command_names())
149
140
    commands = ((n, get_cmd_object(n)) for n in names)
150
 
    shown_commands = [(n, o) for n, o in commands if not o.hidden]
 
141
    shown_commands = [(n, o) for n, o in commands if o.hidden == hidden]
151
142
    max_name = max(len(n) for n, o in shown_commands)
152
143
    indent = ' ' * (max_name + 1)
153
144
    width = osutils.terminal_width() - 1
 
145
 
154
146
    for cmd_name, cmd_object in sorted(shown_commands):
155
147
        plugin_name = cmd_object.plugin_name()
156
148
        if plugin_name is None:
167
159
        lines = textwrap.wrap(helpstring, subsequent_indent=indent,
168
160
                              width=width)
169
161
        for line in lines:
170
 
            outfile.write(line + '\n')
 
162
            out.append(line + '\n')
 
163
    return ''.join(out)
 
164
 
 
165
 
 
166
help_topics.topic_registry.register("commands",
 
167
                                    _help_commands_to_text,
 
168
                                    "Basic help for all commands")
 
169
help_topics.topic_registry.register("hidden-commands", 
 
170
                                    _help_commands_to_text,
 
171
                                    "All hidden commands")