1515from pre_commit .commands .install_uninstall import uninstall
1616from pre_commit .commands .run import run
1717from pre_commit .error_handler import error_handler
18+ from pre_commit .logging_handler import add_logging_handler
1819from pre_commit .runner import Runner
1920
2021
2526os .environ .pop ('__PYVENV_LAUNCHER__' , None )
2627
2728
29+ def _add_color_option (parser ):
30+ parser .add_argument (
31+ '--color' , default = 'auto' , type = color .use_color ,
32+ metavar = '{' + ',' .join (color .COLOR_CHOICES ) + '}' ,
33+ help = 'Whether to use color in output. Defaults to `%(default)s`.' ,
34+ )
35+
36+
2837def main (argv = None ):
2938 argv = argv if argv is not None else sys .argv [1 :]
3039 argv = [five .to_text (arg ) for arg in argv ]
@@ -44,6 +53,7 @@ def main(argv=None):
4453 install_parser = subparsers .add_parser (
4554 'install' , help = 'Install the pre-commit script.' ,
4655 )
56+ _add_color_option (install_parser )
4757 install_parser .add_argument (
4858 '-f' , '--overwrite' , action = 'store_true' ,
4959 help = 'Overwrite existing hooks / remove migration mode.' ,
@@ -63,25 +73,26 @@ def main(argv=None):
6373 uninstall_parser = subparsers .add_parser (
6474 'uninstall' , help = 'Uninstall the pre-commit script.' ,
6575 )
76+ _add_color_option (uninstall_parser )
6677 uninstall_parser .add_argument (
6778 '-t' , '--hook-type' , choices = ('pre-commit' , 'pre-push' ),
6879 default = 'pre-commit' ,
6980 )
7081
71- subparsers .add_parser ('clean' , help = 'Clean out pre-commit files.' )
82+ clean_parser = subparsers .add_parser (
83+ 'clean' , help = 'Clean out pre-commit files.' ,
84+ )
85+ _add_color_option (clean_parser )
7286
73- subparsers .add_parser (
87+ autoupdate_parser = subparsers .add_parser (
7488 'autoupdate' ,
7589 help = "Auto-update pre-commit config to the latest repos' versions." ,
7690 )
91+ _add_color_option (autoupdate_parser )
7792
7893 run_parser = subparsers .add_parser ('run' , help = 'Run hooks.' )
94+ _add_color_option (run_parser )
7995 run_parser .add_argument ('hook' , nargs = '?' , help = 'A single hook-id to run' )
80- run_parser .add_argument (
81- '--color' , default = 'auto' , type = color .use_color ,
82- metavar = '{' + ',' .join (color .COLOR_CHOICES ) + '}' ,
83- help = 'Whether to use color in output. Defaults to `%(default)s`.' ,
84- )
8596 run_parser .add_argument (
8697 '--no-stash' , default = False , action = 'store_true' ,
8798 help = 'Use this option to prevent auto stashing of unstaged files.' ,
@@ -140,6 +151,7 @@ def main(argv=None):
140151 parser .parse_args (['--help' ])
141152
142153 with error_handler ():
154+ add_logging_handler (args .color )
143155 runner = Runner .create ()
144156
145157 if args .command == 'install' :
0 commit comments