Skip to content

Commit 465fdc0

Browse files
authored
gh-67248: cmd: Sort miscellaneous help topics (#92254)
Closes #67248
1 parent 9badc86 commit 465fdc0

File tree

3 files changed

+34
-9
lines changed

3 files changed

+34
-9
lines changed

Lib/cmd.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -310,10 +310,10 @@ def do_help(self, arg):
310310
names = self.get_names()
311311
cmds_doc = []
312312
cmds_undoc = []
313-
help = {}
313+
topics = set()
314314
for name in names:
315315
if name[:5] == 'help_':
316-
help[name[5:]]=1
316+
topics.add(name[5:])
317317
names.sort()
318318
# There can be duplicates if routines overridden
319319
prevname = ''
@@ -323,16 +323,16 @@ def do_help(self, arg):
323323
continue
324324
prevname = name
325325
cmd=name[3:]
326-
if cmd in help:
326+
if cmd in topics:
327327
cmds_doc.append(cmd)
328-
del help[cmd]
328+
topics.remove(cmd)
329329
elif getattr(self, name).__doc__:
330330
cmds_doc.append(cmd)
331331
else:
332332
cmds_undoc.append(cmd)
333333
self.stdout.write("%s\n"%str(self.doc_leader))
334334
self.print_topics(self.doc_header, cmds_doc, 15,80)
335-
self.print_topics(self.misc_header, list(help.keys()),15,80)
335+
self.print_topics(self.misc_header, sorted(topics),15,80)
336336
self.print_topics(self.undoc_header, cmds_undoc, 15,80)
337337

338338
def print_topics(self, header, cmds, cmdlen, maxcol):

Lib/test/test_cmd.py

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ class samplecmdclass(cmd.Cmd):
7070
>>> mycmd.complete_help("12")
7171
[]
7272
>>> sorted(mycmd.complete_help(""))
73-
['add', 'exit', 'help', 'shell']
73+
['add', 'exit', 'help', 'life', 'meaning', 'shell']
7474
7575
Test for the function do_help():
7676
>>> mycmd.do_help("testet")
@@ -79,12 +79,20 @@ class samplecmdclass(cmd.Cmd):
7979
help text for add
8080
>>> mycmd.onecmd("help add")
8181
help text for add
82+
>>> mycmd.onecmd("help meaning") # doctest: +NORMALIZE_WHITESPACE
83+
Try and be nice to people, avoid eating fat, read a good book every
84+
now and then, get some walking in, and try to live together in peace
85+
and harmony with people of all creeds and nations.
8286
>>> mycmd.do_help("")
8387
<BLANKLINE>
8488
Documented commands (type help <topic>):
8589
========================================
8690
add help
8791
<BLANKLINE>
92+
Miscellaneous help topics:
93+
==========================
94+
life meaning
95+
<BLANKLINE>
8896
Undocumented commands:
8997
======================
9098
exit shell
@@ -115,17 +123,22 @@ class samplecmdclass(cmd.Cmd):
115123
This test includes the preloop(), postloop(), default(), emptyline(),
116124
parseline(), do_help() functions
117125
>>> mycmd.use_rawinput=0
118-
>>> mycmd.cmdqueue=["", "add", "add 4 5", "help", "help add","exit"]
119-
>>> mycmd.cmdloop()
126+
127+
>>> mycmd.cmdqueue=["add", "add 4 5", "", "help", "help add", "exit"]
128+
>>> mycmd.cmdloop() # doctest: +REPORT_NDIFF
120129
Hello from preloop
121-
help text for add
122130
*** invalid number of arguments
123131
9
132+
9
124133
<BLANKLINE>
125134
Documented commands (type help <topic>):
126135
========================================
127136
add help
128137
<BLANKLINE>
138+
Miscellaneous help topics:
139+
==========================
140+
life meaning
141+
<BLANKLINE>
129142
Undocumented commands:
130143
======================
131144
exit shell
@@ -165,6 +178,17 @@ def help_add(self):
165178
print("help text for add")
166179
return
167180

181+
def help_meaning(self):
182+
print("Try and be nice to people, avoid eating fat, read a "
183+
"good book every now and then, get some walking in, "
184+
"and try to live together in peace and harmony with "
185+
"people of all creeds and nations.")
186+
return
187+
188+
def help_life(self):
189+
print("Always look on the bright side of life")
190+
return
191+
168192
def do_exit(self, arg):
169193
return True
170194

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Sort the miscellaneous topics in Cmd.do_help()

0 commit comments

Comments
 (0)