@@ -3855,13 +3855,23 @@ def _expand_codes(codes, ignore_codes):
38553855 return ret
38563856
38573857
3858+ def _parser_error_with_code (
3859+ parser : argparse .ArgumentParser , code : int , msg : str ,
3860+ ) -> None :
3861+ """wrap parser.error with exit code"""
3862+ parser .print_usage (sys .stderr )
3863+ parser .exit (code , f"{ msg } \n " )
3864+
3865+
38583866def parse_args (arguments , apply_config = False ):
38593867 """Parse command-line options."""
38603868 parser = create_parser ()
38613869 args = parser .parse_args (arguments )
38623870
38633871 if not args .files and not args .list_fixes :
3864- parser .exit (EXIT_CODE_ARGPARSE_ERROR , 'incorrect number of arguments' )
3872+ _parser_error_with_code (
3873+ parser , EXIT_CODE_ARGPARSE_ERROR , 'incorrect number of arguments' ,
3874+ )
38653875
38663876 args .files = [decode_filename (name ) for name in args .files ]
38673877
@@ -3879,56 +3889,65 @@ def parse_args(arguments, apply_config=False):
38793889
38803890 if '-' in args .files :
38813891 if len (args .files ) > 1 :
3882- parser .exit (
3892+ _parser_error_with_code (
3893+ parser ,
38833894 EXIT_CODE_ARGPARSE_ERROR ,
38843895 'cannot mix stdin and regular files' ,
38853896 )
38863897
38873898 if args .diff :
3888- parser .exit (
3899+ _parser_error_with_code (
3900+ parser ,
38893901 EXIT_CODE_ARGPARSE_ERROR ,
38903902 '--diff cannot be used with standard input' ,
38913903 )
38923904
38933905 if args .in_place :
3894- parser .exit (
3906+ _parser_error_with_code (
3907+ parser ,
38953908 EXIT_CODE_ARGPARSE_ERROR ,
38963909 '--in-place cannot be used with standard input' ,
38973910 )
38983911
38993912 if args .recursive :
3900- parser .exit (
3913+ _parser_error_with_code (
3914+ parser ,
39013915 EXIT_CODE_ARGPARSE_ERROR ,
39023916 '--recursive cannot be used with standard input' ,
39033917 )
39043918
39053919 if len (args .files ) > 1 and not (args .in_place or args .diff ):
3906- parser .exit (
3920+ _parser_error_with_code (
3921+ parser ,
39073922 EXIT_CODE_ARGPARSE_ERROR ,
39083923 'autopep8 only takes one filename as argument '
39093924 'unless the "--in-place" or "--diff" args are used' ,
39103925 )
39113926
39123927 if args .recursive and not (args .in_place or args .diff ):
3913- parser .exit (
3928+ _parser_error_with_code (
3929+ parser ,
39143930 EXIT_CODE_ARGPARSE_ERROR ,
39153931 '--recursive must be used with --in-place or --diff' ,
39163932 )
39173933
39183934 if args .in_place and args .diff :
3919- parser .exit (
3935+ _parser_error_with_code (
3936+ parser ,
39203937 EXIT_CODE_ARGPARSE_ERROR ,
39213938 '--in-place and --diff are mutually exclusive' ,
39223939 )
39233940
39243941 if args .max_line_length <= 0 :
3925- parser .exit (
3942+ _parser_error_with_code (
3943+ parser ,
39263944 EXIT_CODE_ARGPARSE_ERROR ,
39273945 '--max-line-length must be greater than 0' ,
39283946 )
39293947
39303948 if args .indent_size <= 0 :
3931- parser .exit (
3949+ _parser_error_with_code (
3950+ parser ,
39323951 EXIT_CODE_ARGPARSE_ERROR ,
39333952 '--indent-size must be greater than 0' ,
39343953 )
@@ -3968,19 +3987,22 @@ def parse_args(arguments, apply_config=False):
39683987 args .jobs = multiprocessing .cpu_count ()
39693988
39703989 if args .jobs > 1 and not (args .in_place or args .diff ):
3971- parser .exit (
3990+ _parser_error_with_code (
3991+ parser ,
39723992 EXIT_CODE_ARGPARSE_ERROR ,
39733993 'parallel jobs requires --in-place' ,
39743994 )
39753995
39763996 if args .line_range :
39773997 if args .line_range [0 ] <= 0 :
3978- parser .exit (
3998+ _parser_error_with_code (
3999+ parser ,
39794000 EXIT_CODE_ARGPARSE_ERROR ,
39804001 '--range must be positive numbers' ,
39814002 )
39824003 if args .line_range [0 ] > args .line_range [1 ]:
3983- parser .exit (
4004+ _parser_error_with_code (
4005+ parser ,
39844006 EXIT_CODE_ARGPARSE_ERROR ,
39854007 'First value of --range should be less than or equal '
39864008 'to the second' ,
0 commit comments