Skip to content

Commit 597b512

Browse files
authored
Merge pull request #758 from hhatto/fix-issue-713
Fix argument parser errors are printed without a trailing newline
2 parents c55f3e0 + 02592cc commit 597b512

2 files changed

Lines changed: 57 additions & 15 deletions

File tree

autopep8.py

Lines changed: 35 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
38583866
def 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',

test/test_autopep8.py

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5513,8 +5513,12 @@ def test_diff_with_standard_in(self):
55135513

55145514
def test_indent_size_is_zero(self):
55155515
line = "'abc'\n"
5516-
with autopep8_subprocess(line, ['--indent-size=0']) as (_, retcode):
5517-
self.assertEqual(retcode, autopep8.EXIT_CODE_ARGPARSE_ERROR)
5516+
p = Popen(list(AUTOPEP8_CMD_TUPLE) + ['--indent-size=0', '-'],
5517+
stdout=PIPE, stderr=PIPE)
5518+
_, _err = p.communicate(line.encode('utf-8'))
5519+
self.assertEqual(p.returncode, autopep8.EXIT_CODE_ARGPARSE_ERROR)
5520+
self.assertIn('indent-size must be greater than 0\n', _err.decode('utf-8'))
5521+
self.assertIn('usage: autopep8', _err.decode('utf-8'))
55185522

55195523
def test_exit_code_with_io_error(self):
55205524
line = "import sys\ndef a():\n print(1)\n"
@@ -5940,6 +5944,22 @@ def test_exit_code_should_be_set_when_standard_in(self):
59405944
process.returncode,
59415945
autopep8.EXIT_CODE_EXISTS_DIFF)
59425946

5947+
def test_non_args(self):
5948+
process = Popen(list(AUTOPEP8_CMD_TUPLE) +
5949+
[],
5950+
stderr=PIPE,
5951+
stdout=PIPE,
5952+
stdin=PIPE)
5953+
_output, _error = process.communicate()
5954+
self.assertEqual(
5955+
process.returncode,
5956+
autopep8.EXIT_CODE_ARGPARSE_ERROR)
5957+
self.assertEqual(_output.decode("utf-8"), "")
5958+
self.assertIn(
5959+
"incorrect number of arguments\n",
5960+
_error.decode("utf-8"),
5961+
)
5962+
59435963

59445964
class ConfigurationTests(unittest.TestCase):
59455965

0 commit comments

Comments
 (0)