Mercurial > public > mercurial-scm > hg
comparison mercurial/commands.py @ 16854:d71ada5a6a33
help: format all output using RST
This change is the last patch needed to implement help text generation based
only on formatting a single text object marked up with RST.
author | Olav Reinert <seroton10@gmail.com> |
---|---|
date | Sat, 02 Jun 2012 11:28:43 +0200 |
parents | 7863ff383894 |
children | 92cfde8728ac |
comparison
equal
deleted
inserted
replaced
16853:7863ff383894 | 16854:d71ada5a6a33 |
---|---|
3095 except error.AmbiguousCommand, inst: | 3095 except error.AmbiguousCommand, inst: |
3096 # py3k fix: except vars can't be used outside the scope of the | 3096 # py3k fix: except vars can't be used outside the scope of the |
3097 # except block, nor can be used inside a lambda. python issue4617 | 3097 # except block, nor can be used inside a lambda. python issue4617 |
3098 prefix = inst.args[0] | 3098 prefix = inst.args[0] |
3099 select = lambda c: c.lstrip('^').startswith(prefix) | 3099 select = lambda c: c.lstrip('^').startswith(prefix) |
3100 helplist(select) | 3100 rst = helplist(select) |
3101 return | 3101 return rst |
3102 | |
3103 rst = [] | |
3102 | 3104 |
3103 # check if it's an invalid alias and display its error if it is | 3105 # check if it's an invalid alias and display its error if it is |
3104 if getattr(entry[0], 'badalias', False): | 3106 if getattr(entry[0], 'badalias', False): |
3105 if not unknowncmd: | 3107 if not unknowncmd: |
3108 ui.pushbuffer() | |
3106 entry[0](ui) | 3109 entry[0](ui) |
3107 return | 3110 rst.append(ui.popbuffer()) |
3108 | 3111 return rst |
3109 rst = [] | |
3110 | 3112 |
3111 # synopsis | 3113 # synopsis |
3112 if len(entry) > 2: | 3114 if len(entry) > 2: |
3113 if entry[2].startswith('hg'): | 3115 if entry[2].startswith('hg'): |
3114 rst.append("%s\n" % entry[2]) | 3116 rst.append("%s\n" % entry[2]) |
3163 rst.append(_('\nuse "hg help %s" to show the full help text\n') | 3165 rst.append(_('\nuse "hg help %s" to show the full help text\n') |
3164 % name) | 3166 % name) |
3165 elif not ui.quiet: | 3167 elif not ui.quiet: |
3166 rst.append(_('\nuse "hg -v help %s" to show more info\n') | 3168 rst.append(_('\nuse "hg -v help %s" to show more info\n') |
3167 % name) | 3169 % name) |
3168 | 3170 return rst |
3169 keep = ui.verbose and ['verbose'] or [] | |
3170 formatted, pruned = minirst.format(''.join(rst), textwidth, keep=keep) | |
3171 ui.write(formatted) | |
3172 | 3171 |
3173 | 3172 |
3174 def helplist(select=None): | 3173 def helplist(select=None): |
3175 # list of commands | 3174 # list of commands |
3176 if name == "shortlist": | 3175 if name == "shortlist": |
3199 if not doc: | 3198 if not doc: |
3200 doc = _("(no help text available)") | 3199 doc = _("(no help text available)") |
3201 h[f] = doc.splitlines()[0].rstrip() | 3200 h[f] = doc.splitlines()[0].rstrip() |
3202 cmds[f] = c.lstrip("^") | 3201 cmds[f] = c.lstrip("^") |
3203 | 3202 |
3203 rst = [] | |
3204 if not h: | 3204 if not h: |
3205 ui.status(_('no commands defined\n')) | 3205 if not ui.quiet: |
3206 return | 3206 rst.append(_('no commands defined\n')) |
3207 | 3207 return rst |
3208 rst = [] | 3208 |
3209 if not ui.quiet: | 3209 if not ui.quiet: |
3210 rst.append(header) | 3210 rst.append(header) |
3211 fns = sorted(h) | 3211 fns = sorted(h) |
3212 for f in fns: | 3212 for f in fns: |
3213 if ui.verbose: | 3213 if ui.verbose: |
3251 if optlist: | 3251 if optlist: |
3252 for title, options in optlist: | 3252 for title, options in optlist: |
3253 rst.append('\n%s\n' % title) | 3253 rst.append('\n%s\n' % title) |
3254 if options: | 3254 if options: |
3255 rst.append('\n%s\n' % help.optrst(options, ui.verbose)) | 3255 rst.append('\n%s\n' % help.optrst(options, ui.verbose)) |
3256 ui.write(minirst.format(''.join(rst), textwidth)) | 3256 return rst |
3257 | 3257 |
3258 def helptopic(name): | 3258 def helptopic(name): |
3259 for names, header, doc in help.helptable: | 3259 for names, header, doc in help.helptable: |
3260 if name in names: | 3260 if name in names: |
3261 break | 3261 break |
3273 cmdutil.findcmd(name, table) | 3273 cmdutil.findcmd(name, table) |
3274 rst.append(_('\nuse "hg help -c %s" to see help for ' | 3274 rst.append(_('\nuse "hg help -c %s" to see help for ' |
3275 'the %s command\n') % (name, name)) | 3275 'the %s command\n') % (name, name)) |
3276 except error.UnknownCommand: | 3276 except error.UnknownCommand: |
3277 pass | 3277 pass |
3278 ui.write(minirst.format(''.join(rst), textwidth)) | 3278 return rst |
3279 | 3279 |
3280 def helpext(name): | 3280 def helpext(name): |
3281 try: | 3281 try: |
3282 mod = extensions.find(name) | 3282 mod = extensions.find(name) |
3283 doc = gettext(mod.__doc__) or _('no help text available') | 3283 doc = gettext(mod.__doc__) or _('no help text available') |
3289 | 3289 |
3290 if '\n' not in doc: | 3290 if '\n' not in doc: |
3291 head, tail = doc, "" | 3291 head, tail = doc, "" |
3292 else: | 3292 else: |
3293 head, tail = doc.split('\n', 1) | 3293 head, tail = doc.split('\n', 1) |
3294 ui.write(_('%s extension - %s\n\n') % (name.split('.')[-1], head)) | 3294 rst = [_('%s extension - %s\n\n') % (name.split('.')[-1], head)] |
3295 if tail: | 3295 if tail: |
3296 ui.write(minirst.format(tail, textwidth)) | 3296 rst.extend(tail.splitlines(True)) |
3297 ui.status('\n') | 3297 rst.append('\n') |
3298 | 3298 |
3299 if mod: | 3299 if mod: |
3300 try: | 3300 try: |
3301 ct = mod.cmdtable | 3301 ct = mod.cmdtable |
3302 except AttributeError: | 3302 except AttributeError: |
3303 ct = {} | 3303 ct = {} |
3304 modcmds = set([c.split('|', 1)[0] for c in ct]) | 3304 modcmds = set([c.split('|', 1)[0] for c in ct]) |
3305 helplist(modcmds.__contains__) | 3305 rst.extend(helplist(modcmds.__contains__)) |
3306 else: | 3306 else: |
3307 ui.write(_('use "hg help extensions" for information on enabling ' | 3307 rst.append(_('use "hg help extensions" for information on enabling ' |
3308 'extensions\n')) | 3308 'extensions\n')) |
3309 return rst | |
3309 | 3310 |
3310 def helpextcmd(name): | 3311 def helpextcmd(name): |
3311 cmd, ext, mod = extensions.disabledcmd(ui, name, | 3312 cmd, ext, mod = extensions.disabledcmd(ui, name, |
3312 ui.configbool('ui', 'strict')) | 3313 ui.configbool('ui', 'strict')) |
3313 doc = gettext(mod.__doc__).splitlines()[0] | 3314 doc = gettext(mod.__doc__).splitlines()[0] |
3315 rst = help.listexts(_("'%s' is provided by the following " | 3316 rst = help.listexts(_("'%s' is provided by the following " |
3316 "extension:") % cmd, {ext: doc}, indent=4) | 3317 "extension:") % cmd, {ext: doc}, indent=4) |
3317 rst.append('\n') | 3318 rst.append('\n') |
3318 rst.append(_('use "hg help extensions" for information on enabling ' | 3319 rst.append(_('use "hg help extensions" for information on enabling ' |
3319 'extensions\n')) | 3320 'extensions\n')) |
3320 ui.write(minirst.format(''.join(rst), textwidth)) | 3321 return rst |
3321 | 3322 |
3323 | |
3324 rst = [] | |
3322 kw = opts.get('keyword') | 3325 kw = opts.get('keyword') |
3323 if kw: | 3326 if kw: |
3324 matches = help.topicmatch(kw) | 3327 matches = help.topicmatch(kw) |
3325 for t, title in (('topics', _('Topics')), | 3328 for t, title in (('topics', _('Topics')), |
3326 ('commands', _('Commands')), | 3329 ('commands', _('Commands')), |
3327 ('extensions', _('Extensions')), | 3330 ('extensions', _('Extensions')), |
3328 ('extensioncommands', _('Extension Commands'))): | 3331 ('extensioncommands', _('Extension Commands'))): |
3329 if matches[t]: | 3332 if matches[t]: |
3330 ui.write('%s:\n\n' % title) | 3333 rst.append('%s:\n\n' % title) |
3331 rst = ''.join(minirst.maketable(matches[t], 1)) | 3334 rst.extend(minirst.maketable(matches[t], 1)) |
3332 ui.write(minirst.format(rst)) | 3335 elif name and name != 'shortlist': |
3333 return | |
3334 | |
3335 if name and name != 'shortlist': | |
3336 i = None | 3336 i = None |
3337 if unknowncmd: | 3337 if unknowncmd: |
3338 queries = (helpextcmd,) | 3338 queries = (helpextcmd,) |
3339 elif opts.get('extension'): | 3339 elif opts.get('extension'): |
3340 queries = (helpext,) | 3340 queries = (helpext,) |
3342 queries = (helpcmd,) | 3342 queries = (helpcmd,) |
3343 else: | 3343 else: |
3344 queries = (helptopic, helpcmd, helpext, helpextcmd) | 3344 queries = (helptopic, helpcmd, helpext, helpextcmd) |
3345 for f in queries: | 3345 for f in queries: |
3346 try: | 3346 try: |
3347 f(name) | 3347 rst = f(name) |
3348 i = None | 3348 i = None |
3349 break | 3349 break |
3350 except error.UnknownCommand, inst: | 3350 except error.UnknownCommand, inst: |
3351 i = inst | 3351 i = inst |
3352 if i: | 3352 if i: |
3353 raise i | 3353 raise i |
3354 else: | 3354 else: |
3355 # program name | 3355 # program name |
3356 ui.status(_("Mercurial Distributed SCM\n")) | 3356 if not ui.quiet: |
3357 ui.status('\n') | 3357 rst = [_("Mercurial Distributed SCM\n"), '\n'] |
3358 helplist() | 3358 rst.extend(helplist()) |
3359 | |
3360 keep = ui.verbose and ['verbose'] or [] | |
3361 formatted, pruned = minirst.format(''.join(rst), textwidth, keep=keep) | |
3362 ui.write(formatted) | |
3359 | 3363 |
3360 | 3364 |
3361 @command('identify|id', | 3365 @command('identify|id', |
3362 [('r', 'rev', '', | 3366 [('r', 'rev', '', |
3363 _('identify the specified revision'), _('REV')), | 3367 _('identify the specified revision'), _('REV')), |