Skip to content

Commit 170335c

Browse files
authored
Merge pull request #2429 from pre-commit/remove-config-option-when-unused
remove unused --config options from commands which don't use it
2 parents c69ae26 + 53643de commit 170335c

File tree

1 file changed

+33
-50
lines changed

1 file changed

+33
-50
lines changed

pre_commit/main.py

Lines changed: 33 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -181,11 +181,15 @@ def main(argv: Sequence[str] | None = None) -> int:
181181

182182
subparsers = parser.add_subparsers(dest='command')
183183

184-
autoupdate_parser = subparsers.add_parser(
184+
def _add_cmd(name: str, *, help: str) -> argparse.ArgumentParser:
185+
parser = subparsers.add_parser(name, help=help)
186+
add_color_option(parser)
187+
return parser
188+
189+
autoupdate_parser = _add_cmd(
185190
'autoupdate',
186191
help="Auto-update pre-commit config to the latest repos' versions.",
187192
)
188-
add_color_option(autoupdate_parser)
189193
_add_config_option(autoupdate_parser)
190194
autoupdate_parser.add_argument(
191195
'--bleeding-edge', action='store_true',
@@ -203,34 +207,17 @@ def main(argv: Sequence[str] | None = None) -> int:
203207
help='Only update this repository -- may be specified multiple times.',
204208
)
205209

206-
clean_parser = subparsers.add_parser(
207-
'clean', help='Clean out pre-commit files.',
208-
)
209-
add_color_option(clean_parser)
210-
_add_config_option(clean_parser)
211-
212-
hook_impl_parser = subparsers.add_parser('hook-impl')
213-
add_color_option(hook_impl_parser)
214-
_add_config_option(hook_impl_parser)
215-
hook_impl_parser.add_argument('--hook-type')
216-
hook_impl_parser.add_argument('--hook-dir')
217-
hook_impl_parser.add_argument(
218-
'--skip-on-missing-config', action='store_true',
219-
)
220-
hook_impl_parser.add_argument(dest='rest', nargs=argparse.REMAINDER)
210+
_add_cmd('clean', help='Clean out pre-commit files.')
221211

222-
gc_parser = subparsers.add_parser('gc', help='Clean unused cached repos.')
223-
add_color_option(gc_parser)
224-
_add_config_option(gc_parser)
212+
_add_cmd('gc', help='Clean unused cached repos.')
225213

226-
init_templatedir_parser = subparsers.add_parser(
214+
init_templatedir_parser = _add_cmd(
227215
'init-templatedir',
228216
help=(
229217
'Install hook script in a directory intended for use with '
230218
'`git config init.templateDir`.'
231219
),
232220
)
233-
add_color_option(init_templatedir_parser)
234221
_add_config_option(init_templatedir_parser)
235222
init_templatedir_parser.add_argument(
236223
'directory', help='The directory in which to write the hook script.',
@@ -243,10 +230,7 @@ def main(argv: Sequence[str] | None = None) -> int:
243230
)
244231
_add_hook_type_option(init_templatedir_parser)
245232

246-
install_parser = subparsers.add_parser(
247-
'install', help='Install the pre-commit script.',
248-
)
249-
add_color_option(install_parser)
233+
install_parser = _add_cmd('install', help='Install the pre-commit script.')
250234
_add_config_option(install_parser)
251235
install_parser.add_argument(
252236
'-f', '--overwrite', action='store_true',
@@ -268,40 +252,32 @@ def main(argv: Sequence[str] | None = None) -> int:
268252
),
269253
)
270254

271-
install_hooks_parser = subparsers.add_parser(
255+
install_hooks_parser = _add_cmd(
272256
'install-hooks',
273257
help=(
274258
'Install hook environments for all environments in the config '
275259
'file. You may find `pre-commit install --install-hooks` more '
276260
'useful.'
277261
),
278262
)
279-
add_color_option(install_hooks_parser)
280263
_add_config_option(install_hooks_parser)
281264

282-
migrate_config_parser = subparsers.add_parser(
265+
migrate_config_parser = _add_cmd(
283266
'migrate-config',
284267
help='Migrate list configuration to new map configuration.',
285268
)
286-
add_color_option(migrate_config_parser)
287269
_add_config_option(migrate_config_parser)
288270

289-
run_parser = subparsers.add_parser('run', help='Run hooks.')
290-
add_color_option(run_parser)
271+
run_parser = _add_cmd('run', help='Run hooks.')
291272
_add_config_option(run_parser)
292273
_add_run_options(run_parser)
293274

294-
sample_config_parser = subparsers.add_parser(
295-
'sample-config', help=f'Produce a sample {C.CONFIG_FILE} file',
296-
)
297-
add_color_option(sample_config_parser)
298-
_add_config_option(sample_config_parser)
275+
_add_cmd('sample-config', help=f'Produce a sample {C.CONFIG_FILE} file')
299276

300-
try_repo_parser = subparsers.add_parser(
277+
try_repo_parser = _add_cmd(
301278
'try-repo',
302279
help='Try the hooks in a repository, useful for developing new hooks.',
303280
)
304-
add_color_option(try_repo_parser)
305281
_add_config_option(try_repo_parser)
306282
try_repo_parser.add_argument(
307283
'repo', help='Repository to source hooks from.',
@@ -315,32 +291,39 @@ def main(argv: Sequence[str] | None = None) -> int:
315291
)
316292
_add_run_options(try_repo_parser)
317293

318-
uninstall_parser = subparsers.add_parser(
294+
uninstall_parser = _add_cmd(
319295
'uninstall', help='Uninstall the pre-commit script.',
320296
)
321-
add_color_option(uninstall_parser)
322297
_add_config_option(uninstall_parser)
323298
_add_hook_type_option(uninstall_parser)
324299

325-
validate_config_parser = subparsers.add_parser(
300+
validate_config_parser = _add_cmd(
326301
'validate-config', help='Validate .pre-commit-config.yaml files',
327302
)
328-
add_color_option(validate_config_parser)
329-
_add_config_option(validate_config_parser)
330303
validate_config_parser.add_argument('filenames', nargs='*')
331304

332-
validate_manifest_parser = subparsers.add_parser(
305+
validate_manifest_parser = _add_cmd(
333306
'validate-manifest', help='Validate .pre-commit-hooks.yaml files',
334307
)
335-
add_color_option(validate_manifest_parser)
336-
_add_config_option(validate_manifest_parser)
337308
validate_manifest_parser.add_argument('filenames', nargs='*')
338309

310+
# does not use `_add_cmd` because it doesn't use `--color`
339311
help = subparsers.add_parser(
340312
'help', help='Show help for a specific command.',
341313
)
342314
help.add_argument('help_cmd', nargs='?', help='Command to show help for.')
343315

316+
# not intended for users to call this directly
317+
hook_impl_parser = subparsers.add_parser('hook-impl')
318+
add_color_option(hook_impl_parser)
319+
_add_config_option(hook_impl_parser)
320+
hook_impl_parser.add_argument('--hook-type')
321+
hook_impl_parser.add_argument('--hook-dir')
322+
hook_impl_parser.add_argument(
323+
'--skip-on-missing-config', action='store_true',
324+
)
325+
hook_impl_parser.add_argument(dest='rest', nargs=argparse.REMAINDER)
326+
344327
# argparse doesn't really provide a way to use a `default` subparser
345328
if len(argv) == 0:
346329
argv = ['run']
@@ -354,11 +337,11 @@ def main(argv: Sequence[str] | None = None) -> int:
354337
with error_handler(), logging_handler(args.color):
355338
git.check_for_cygwin_mismatch()
356339

340+
store = Store()
341+
357342
if args.command not in COMMANDS_NO_GIT:
358343
_adjust_args_and_chdir(args)
359-
360-
store = Store()
361-
store.mark_config_used(args.config)
344+
store.mark_config_used(args.config)
362345

363346
if args.command == 'autoupdate':
364347
return autoupdate(

0 commit comments

Comments
 (0)