~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/help.py

  • Committer: Martin Pool
  • Date: 2005-07-04 05:12:59 UTC
  • Revision ID: mbp@sourcefrog.net-20050704051259-a001ad74f39dadb7
- New log --long option

  Patch from William Dodé

Show diffs side-by-side

added added

removed removed

Lines of Context:
56
56
"""
57
57
 
58
58
 
59
 
 
60
 
def help(topic=None):
 
59
import sys
 
60
 
 
61
 
 
62
def help(topic=None, outfile = None):
 
63
    if outfile == None:
 
64
        outfile = sys.stdout
61
65
    if topic == None:
62
 
        print global_help
 
66
        outfile.write(global_help)
63
67
    elif topic == 'commands':
64
 
        help_commands()
 
68
        help_commands(outfile = outfile)
65
69
    else:
66
 
        help_on_command(topic)
 
70
        help_on_command(topic, outfile = outfile)
67
71
 
68
72
 
69
73
def command_usage(cmdname, cmdclass):
88
92
    return s
89
93
 
90
94
 
91
 
def help_on_command(cmdname):
 
95
def help_on_command(cmdname, outfile = None):
92
96
    cmdname = str(cmdname)
93
97
 
 
98
    if outfile == None:
 
99
        outfile = sys.stdout
 
100
 
94
101
    from inspect import getdoc
95
102
    import commands
96
103
    topic, cmdclass = commands.get_cmd_class(cmdname)
99
106
    if doc == None:
100
107
        raise NotImplementedError("sorry, no detailed help yet for %r" % cmdname)
101
108
 
102
 
    print 'usage:', command_usage(topic, cmdclass)
 
109
    outfile.write('usage: ' + command_usage(topic, cmdclass) + '\n')
103
110
 
104
111
    if cmdclass.aliases:
105
 
        print 'aliases: ' + ', '.join(cmdclass.aliases)
106
 
    
107
 
    print doc
108
 
    
109
 
    help_on_option(cmdclass.takes_options)
110
 
 
111
 
 
112
 
def help_on_option(options):
 
112
        outfile.write('aliases: ' + ', '.join(cmdclass.aliases) + '\n')
 
113
    
 
114
    outfile.write(doc)
 
115
    if doc[-1] != '\n':
 
116
        outfile.write('\n')
 
117
    
 
118
    help_on_option(cmdclass.takes_options, outfile = None)
 
119
 
 
120
 
 
121
def help_on_option(options, outfile = None):
113
122
    import commands
114
123
    
115
124
    if not options:
116
125
        return
117
126
    
118
 
    print
119
 
    print 'options:'
 
127
    if outfile == None:
 
128
        outfile = sys.stdout
 
129
 
 
130
    outfile.write('\noptions:\n')
120
131
    for on in options:
121
132
        l = '    --' + on
122
133
        for shortname, longname in commands.SHORT_OPTIONS.items():
123
134
            if longname == on:
124
135
                l += ', -' + shortname
125
136
                break
126
 
        print l
127
 
 
128
 
 
129
 
def help_commands():
 
137
        outfile.write(l + '\n')
 
138
 
 
139
 
 
140
def help_commands(outfile = None):
130
141
    """List all commands"""
131
142
    import inspect
132
143
    import commands
 
144
 
 
145
    if outfile == None:
 
146
        outfile = sys.stdout
133
147
    
134
148
    accu = []
135
149
    for cmdname, cmdclass in commands.get_all_cmds():
138
152
    for cmdname, cmdclass in accu:
139
153
        if cmdclass.hidden:
140
154
            continue
141
 
        print command_usage(cmdname, cmdclass)
 
155
        outfile.write(command_usage(cmdname, cmdclass) + '\n')
142
156
        help = inspect.getdoc(cmdclass)
143
157
        if help:
144
 
            print "    " + help.split('\n', 1)[0]
 
158
            outfile.write("    " + help.split('\n', 1)[0] + '\n')
 
159
 
145
160
            
146
161