Skip to content

Commit 2e65e19

Browse files
committed
docs: make it easier to add command-line options correctly
1 parent bf8cbe1 commit 2e65e19

File tree

1 file changed

+14
-4
lines changed

1 file changed

+14
-4
lines changed

coverage/cmdline.py

+14-4
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
"""Command-line support for coverage.py."""
55

6-
76
import glob
87
import optparse # pylint: disable=deprecated-module
98
import os
@@ -25,10 +24,15 @@
2524
from coverage.execfile import PyRunner
2625
from coverage.results import Numbers, should_fail_under
2726

27+
# When adding to this file, alphabetization is important. Look for
28+
# "alphabetize" comments throughout.
2829

2930
class Opts:
3031
"""A namespace class for individual options we'll build parsers from."""
3132

33+
# Keep these entries alphabetized (roughly) by the option name as it
34+
# appears on the command line.
35+
3236
append = optparse.make_option(
3337
'-a', '--append', action='store_true',
3438
help="Append coverage data to .coverage, otherwise it starts clean each time.",
@@ -232,6 +236,7 @@ def __init__(self, *args, **kwargs):
232236
add_help_option=False, *args, **kwargs
233237
)
234238
self.set_defaults(
239+
# Keep these arguments alphabetized by their names.
235240
action=None,
236241
append=None,
237242
branch=None,
@@ -337,14 +342,19 @@ def get_prog_name(self):
337342
# Include the sub-command for this parser as part of the command.
338343
return f"{program_name} {self.cmd}"
339344

345+
# In lists of Opts, keep them alphabetized by the option names as they appear
346+
# on the command line, since these lists determine the order of the options in
347+
# the help output.
348+
#
349+
# In COMMANDS, keep the keys (command names) alphabetized.
340350

341351
GLOBAL_ARGS = [
342352
Opts.debug,
343353
Opts.help,
344354
Opts.rcfile,
345355
]
346356

347-
CMDS = {
357+
COMMANDS = {
348358
'annotate': CmdOptionParser(
349359
"annotate",
350360
[
@@ -595,7 +605,7 @@ def command_line(self, argv):
595605
if self.global_option:
596606
parser = GlobalOptionParser()
597607
else:
598-
parser = CMDS.get(argv[0])
608+
parser = COMMANDS.get(argv[0])
599609
if not parser:
600610
show_help(f"Unknown command: {argv[0]!r}")
601611
return ERR
@@ -752,7 +762,7 @@ def do_help(self, options, args, parser):
752762
if options.action == "help":
753763
if args:
754764
for a in args:
755-
parser = CMDS.get(a)
765+
parser = COMMANDS.get(a)
756766
if parser:
757767
show_help(parser=parser)
758768
else:

0 commit comments

Comments
 (0)