diff options
Diffstat (limited to 'tests/test_subcmds.py')
-rw-r--r-- | tests/test_subcmds.py | 41 |
1 files changed, 41 insertions, 0 deletions
diff --git a/tests/test_subcmds.py b/tests/test_subcmds.py index 5ce0776f..2d680fb7 100644 --- a/tests/test_subcmds.py +++ b/tests/test_subcmds.py | |||
@@ -89,3 +89,44 @@ class AllCommands(unittest.TestCase): | |||
89 | msg=f"subcmds/{name}.py: {opt}: only use dashes in " | 89 | msg=f"subcmds/{name}.py: {opt}: only use dashes in " |
90 | "options, not underscores", | 90 | "options, not underscores", |
91 | ) | 91 | ) |
92 | |||
93 | def test_cli_option_dest(self): | ||
94 | """Block redundant dest= arguments.""" | ||
95 | |||
96 | def _check_dest(opt): | ||
97 | if opt.dest is None or not opt._long_opts: | ||
98 | return | ||
99 | |||
100 | long = opt._long_opts[0] | ||
101 | assert long.startswith("--") | ||
102 | # This matches optparse's behavior. | ||
103 | implicit_dest = long[2:].replace("-", "_") | ||
104 | if implicit_dest == opt.dest: | ||
105 | bad_opts.append((str(opt), opt.dest)) | ||
106 | |||
107 | # Hook the option check list. | ||
108 | optparse.Option.CHECK_METHODS.insert(0, _check_dest) | ||
109 | |||
110 | # Gather all the bad options up front so people can see all bad options | ||
111 | # instead of failing at the first one. | ||
112 | all_bad_opts = {} | ||
113 | for name, cls in subcmds.all_commands.items(): | ||
114 | bad_opts = all_bad_opts[name] = [] | ||
115 | cmd = cls() | ||
116 | # Trigger construction of parser. | ||
117 | cmd.OptionParser | ||
118 | |||
119 | errmsg = None | ||
120 | for name, bad_opts in sorted(all_bad_opts.items()): | ||
121 | if bad_opts: | ||
122 | if not errmsg: | ||
123 | errmsg = "Omit redundant dest= when defining options.\n" | ||
124 | errmsg += f"\nSubcommand {name} (subcmds/{name}.py):\n" | ||
125 | errmsg += "".join( | ||
126 | f" {opt}: dest='{dest}'\n" for opt, dest in bad_opts | ||
127 | ) | ||
128 | if errmsg: | ||
129 | self.fail(errmsg) | ||
130 | |||
131 | # Make sure we aren't popping the wrong stuff. | ||
132 | assert optparse.Option.CHECK_METHODS.pop(0) is _check_dest | ||