~bzr-pqm/bzr/bzr.dev

351 by Martin Pool
- Split out help functions into bzrlib.help
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
974.1.26 by aaron.bentley at utoronto
merged mbp@sourcefrog.net-20050817233101-0939da1cf91f2472
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 'bzr help commands' for
43
all commands.
351 by Martin Pool
- Split out help functions into bzrlib.help
44
"""
45
46
635 by Martin Pool
- manpage generator by Hans Ulrich Niedermann
47
import sys
48
49
50
def help(topic=None, outfile = None):
51
    if outfile == None:
52
        outfile = sys.stdout
351 by Martin Pool
- Split out help functions into bzrlib.help
53
    if topic == None:
635 by Martin Pool
- manpage generator by Hans Ulrich Niedermann
54
        outfile.write(global_help)
351 by Martin Pool
- Split out help functions into bzrlib.help
55
    elif topic == 'commands':
635 by Martin Pool
- manpage generator by Hans Ulrich Niedermann
56
        help_commands(outfile = outfile)
351 by Martin Pool
- Split out help functions into bzrlib.help
57
    else:
635 by Martin Pool
- manpage generator by Hans Ulrich Niedermann
58
        help_on_command(topic, outfile = outfile)
351 by Martin Pool
- Split out help functions into bzrlib.help
59
60
452 by Martin Pool
- show command usage in help
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
635 by Martin Pool
- manpage generator by Hans Ulrich Niedermann
83
def help_on_command(cmdname, outfile = None):
351 by Martin Pool
- Split out help functions into bzrlib.help
84
    cmdname = str(cmdname)
85
635 by Martin Pool
- manpage generator by Hans Ulrich Niedermann
86
    if outfile == None:
87
        outfile = sys.stdout
88
351 by Martin Pool
- Split out help functions into bzrlib.help
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
635 by Martin Pool
- manpage generator by Hans Ulrich Niedermann
97
    outfile.write('usage: ' + command_usage(topic, cmdclass) + '\n')
352 by Martin Pool
- Show aliases in command help
98
99
    if cmdclass.aliases:
635 by Martin Pool
- manpage generator by Hans Ulrich Niedermann
100
        outfile.write('aliases: ' + ', '.join(cmdclass.aliases) + '\n')
101
    
102
    outfile.write(doc)
675 by Martin Pool
- help formatting fix from ndim
103
    if doc[-1] != '\n':
104
        outfile.write('\n')
635 by Martin Pool
- manpage generator by Hans Ulrich Niedermann
105
    
106
    help_on_option(cmdclass.takes_options, outfile = None)
107
108
109
def help_on_option(options, outfile = None):
351 by Martin Pool
- Split out help functions into bzrlib.help
110
    import commands
111
    
112
    if not options:
113
        return
114
    
635 by Martin Pool
- manpage generator by Hans Ulrich Niedermann
115
    if outfile == None:
116
        outfile = sys.stdout
117
118
    outfile.write('\noptions:\n')
351 by Martin Pool
- Split out help functions into bzrlib.help
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
635 by Martin Pool
- manpage generator by Hans Ulrich Niedermann
125
        outfile.write(l + '\n')
126
127
128
def help_commands(outfile = None):
351 by Martin Pool
- Split out help functions into bzrlib.help
129
    """List all commands"""
130
    import inspect
131
    import commands
635 by Martin Pool
- manpage generator by Hans Ulrich Niedermann
132
133
    if outfile == None:
134
        outfile = sys.stdout
351 by Martin Pool
- Split out help functions into bzrlib.help
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
635 by Martin Pool
- manpage generator by Hans Ulrich Niedermann
143
        outfile.write(command_usage(cmdname, cmdclass) + '\n')
351 by Martin Pool
- Split out help functions into bzrlib.help
144
        help = inspect.getdoc(cmdclass)
145
        if help:
635 by Martin Pool
- manpage generator by Hans Ulrich Niedermann
146
            outfile.write("    " + help.split('\n', 1)[0] + '\n')
147
351 by Martin Pool
- Split out help functions into bzrlib.help
148
            
149