Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix argument parser errors are printed without a trailing newline #758

Merged
merged 2 commits into from
Jun 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 35 additions & 13 deletions autopep8.py
Original file line number Diff line number Diff line change
Expand Up @@ -3855,13 +3855,23 @@ def _expand_codes(codes, ignore_codes):
return ret


def _parser_error_with_code(
parser: argparse.ArgumentParser, code: int, msg: str,
) -> None:
"""wrap parser.error with exit code"""
parser.print_usage(sys.stderr)
parser.exit(code, f"{msg}\n")


def parse_args(arguments, apply_config=False):
"""Parse command-line options."""
parser = create_parser()
args = parser.parse_args(arguments)

if not args.files and not args.list_fixes:
parser.exit(EXIT_CODE_ARGPARSE_ERROR, 'incorrect number of arguments')
_parser_error_with_code(
parser, EXIT_CODE_ARGPARSE_ERROR, 'incorrect number of arguments',
)

args.files = [decode_filename(name) for name in args.files]

Expand All @@ -3879,56 +3889,65 @@ def parse_args(arguments, apply_config=False):

if '-' in args.files:
if len(args.files) > 1:
parser.exit(
_parser_error_with_code(
parser,
EXIT_CODE_ARGPARSE_ERROR,
'cannot mix stdin and regular files',
)

if args.diff:
parser.exit(
_parser_error_with_code(
parser,
EXIT_CODE_ARGPARSE_ERROR,
'--diff cannot be used with standard input',
)

if args.in_place:
parser.exit(
_parser_error_with_code(
parser,
EXIT_CODE_ARGPARSE_ERROR,
'--in-place cannot be used with standard input',
)

if args.recursive:
parser.exit(
_parser_error_with_code(
parser,
EXIT_CODE_ARGPARSE_ERROR,
'--recursive cannot be used with standard input',
)

if len(args.files) > 1 and not (args.in_place or args.diff):
parser.exit(
_parser_error_with_code(
parser,
EXIT_CODE_ARGPARSE_ERROR,
'autopep8 only takes one filename as argument '
'unless the "--in-place" or "--diff" args are used',
)

if args.recursive and not (args.in_place or args.diff):
parser.exit(
_parser_error_with_code(
parser,
EXIT_CODE_ARGPARSE_ERROR,
'--recursive must be used with --in-place or --diff',
)

if args.in_place and args.diff:
parser.exit(
_parser_error_with_code(
parser,
EXIT_CODE_ARGPARSE_ERROR,
'--in-place and --diff are mutually exclusive',
)

if args.max_line_length <= 0:
parser.exit(
_parser_error_with_code(
parser,
EXIT_CODE_ARGPARSE_ERROR,
'--max-line-length must be greater than 0',
)

if args.indent_size <= 0:
parser.exit(
_parser_error_with_code(
parser,
EXIT_CODE_ARGPARSE_ERROR,
'--indent-size must be greater than 0',
)
Expand Down Expand Up @@ -3968,19 +3987,22 @@ def parse_args(arguments, apply_config=False):
args.jobs = multiprocessing.cpu_count()

if args.jobs > 1 and not (args.in_place or args.diff):
parser.exit(
_parser_error_with_code(
parser,
EXIT_CODE_ARGPARSE_ERROR,
'parallel jobs requires --in-place',
)

if args.line_range:
if args.line_range[0] <= 0:
parser.exit(
_parser_error_with_code(
parser,
EXIT_CODE_ARGPARSE_ERROR,
'--range must be positive numbers',
)
if args.line_range[0] > args.line_range[1]:
parser.exit(
_parser_error_with_code(
parser,
EXIT_CODE_ARGPARSE_ERROR,
'First value of --range should be less than or equal '
'to the second',
Expand Down
24 changes: 22 additions & 2 deletions test/test_autopep8.py
Original file line number Diff line number Diff line change
Expand Up @@ -5513,8 +5513,12 @@ def test_diff_with_standard_in(self):

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

def test_exit_code_with_io_error(self):
line = "import sys\ndef a():\n print(1)\n"
Expand Down Expand Up @@ -5940,6 +5944,22 @@ def test_exit_code_should_be_set_when_standard_in(self):
process.returncode,
autopep8.EXIT_CODE_EXISTS_DIFF)

def test_non_args(self):
process = Popen(list(AUTOPEP8_CMD_TUPLE) +
[],
stderr=PIPE,
stdout=PIPE,
stdin=PIPE)
_output, _error = process.communicate()
self.assertEqual(
process.returncode,
autopep8.EXIT_CODE_ARGPARSE_ERROR)
self.assertEqual(_output.decode("utf-8"), "")
self.assertIn(
"incorrect number of arguments\n",
_error.decode("utf-8"),
)


class ConfigurationTests(unittest.TestCase):

Expand Down