~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/help.py

  • Committer: Martin Pool
  • Date: 2005-08-29 06:56:22 UTC
  • Revision ID: mbp@sourcefrog.net-20050829065622-5aa7add87c38f188
- additional trace messages for plugins

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2004, 2005 by Canonical Ltd
 
2
 
 
3
# This program is free software; you can redistribute it and/or modify
 
4
# it under the terms of the GNU General Public License as published by
 
5
# the Free Software Foundation; either version 2 of the License, or
 
6
# (at your option) any later version.
 
7
 
 
8
# This program is distributed in the hope that it will be useful,
 
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
11
# GNU General Public License for more details.
 
12
 
 
13
# You should have received a copy of the GNU General Public License
 
14
# along with this program; if not, write to the Free Software
 
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
16
 
 
17
global_help = \
 
18
"""Bazaar-NG -- a free distributed version-control tool
 
19
http://bazaar-ng.org/
 
20
 
 
21
WARNING: This is an unstable development version.
 
22
         Please keep backups.
 
23
 
 
24
Basic commands:
 
25
 
 
26
  bzr init      makes this branch versioned
 
27
  bzr branch    make a copy of another branch
 
28
 
 
29
  bzr add       make files or directories versioned
 
30
  bzr ignore    ignore a file or pattern
 
31
  bzr mv        move or rename a versioned file
 
32
 
 
33
  bzr status    summarize changes in working copy
 
34
  bzr diff      show detailed diffs
 
35
 
 
36
  bzr merge     pull in changes from another branch
 
37
  bzr commit    
 
38
 
 
39
  bzr log       show history of changes
 
40
  bzr check     validate storage
 
41
 
 
42
Use e.g. 'bzr help init' for more details, or
 
43
'bzr help commands' for all commands.
 
44
"""
 
45
 
 
46
 
 
47
import sys
 
48
 
 
49
 
 
50
def help(topic=None, outfile = None):
 
51
    if outfile == None:
 
52
        outfile = sys.stdout
 
53
    if topic == None:
 
54
        outfile.write(global_help)
 
55
    elif topic == 'commands':
 
56
        help_commands(outfile = outfile)
 
57
    else:
 
58
        help_on_command(topic, outfile = outfile)
 
59
 
 
60
 
 
61
def command_usage(cmdname, cmdclass):
 
62
    """Return single-line grammar for command.
 
63
 
 
64
    Only describes arguments, not options.
 
65
    """
 
66
    s = cmdname + ' '
 
67
    for aname in cmdclass.takes_args:
 
68
        aname = aname.upper()
 
69
        if aname[-1] in ['$', '+']:
 
70
            aname = aname[:-1] + '...'
 
71
        elif aname[-1] == '?':
 
72
            aname = '[' + aname[:-1] + ']'
 
73
        elif aname[-1] == '*':
 
74
            aname = '[' + aname[:-1] + '...]'
 
75
        s += aname + ' '
 
76
            
 
77
    assert s[-1] == ' '
 
78
    s = s[:-1]
 
79
    
 
80
    return s
 
81
 
 
82
 
 
83
def help_on_command(cmdname, outfile = None):
 
84
    cmdname = str(cmdname)
 
85
 
 
86
    if outfile == None:
 
87
        outfile = sys.stdout
 
88
 
 
89
    from inspect import getdoc
 
90
    import commands
 
91
    topic, cmdclass = commands.get_cmd_class(cmdname)
 
92
 
 
93
    doc = getdoc(cmdclass)
 
94
    if doc == None:
 
95
        raise NotImplementedError("sorry, no detailed help yet for %r" % cmdname)
 
96
 
 
97
    outfile.write('usage: ' + command_usage(topic, cmdclass) + '\n')
 
98
 
 
99
    if cmdclass.aliases:
 
100
        outfile.write('aliases: ' + ', '.join(cmdclass.aliases) + '\n')
 
101
    
 
102
    outfile.write(doc)
 
103
    if doc[-1] != '\n':
 
104
        outfile.write('\n')
 
105
    
 
106
    help_on_option(cmdclass.takes_options, outfile = None)
 
107
 
 
108
 
 
109
def help_on_option(options, outfile = None):
 
110
    import commands
 
111
    
 
112
    if not options:
 
113
        return
 
114
    
 
115
    if outfile == None:
 
116
        outfile = sys.stdout
 
117
 
 
118
    outfile.write('\noptions:\n')
 
119
    for on in options:
 
120
        l = '    --' + on
 
121
        for shortname, longname in commands.SHORT_OPTIONS.items():
 
122
            if longname == on:
 
123
                l += ', -' + shortname
 
124
                break
 
125
        outfile.write(l + '\n')
 
126
 
 
127
 
 
128
def help_commands(outfile = None):
 
129
    """List all commands"""
 
130
    import inspect
 
131
    import commands
 
132
 
 
133
    if outfile == None:
 
134
        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:
 
142
            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