Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 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
13 changes: 9 additions & 4 deletions Lib/mimetypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -722,18 +722,23 @@ def _main(args=None):

args, help_text = _parse_args(args)

results = []
if args.extension:
for gtype in args.type:
guess = guess_extension(gtype, not args.lenient)
if guess:
return str(guess)
sys.exit(f"error: unknown type {gtype}")
results.append(str(guess))
else:
sys.exit(f"error: unknown type {gtype}")
return '\n'.join(results)
else:
for gtype in args.type:
guess, encoding = guess_type(gtype, not args.lenient)
if guess:
return f"type: {guess} encoding: {encoding}"
sys.exit(f"error: media type unknown for {gtype}")
results.append(f"type: {guess} encoding: {encoding}")
else:
sys.exit(f"error: media type unknown for {gtype}")
return '\n'.join(results)
return help_text
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is return help_text line used?

Either:

  • delete return help_text (that branch of the code is never used)
  • change return help_text to return "\n".join(results) and delete the other return calls at the end of each for type in args.type block.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not used, IDE already given a hint

In addition, test failure case have been added



Expand Down
8 changes: 8 additions & 0 deletions Lib/test/test_mimetypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -470,6 +470,14 @@ def test_parse_args(self):
self.assertFalse(args.lenient)
self.assertEqual(args.type, ["foo.pic"])

def test_multiple_inputs(self):
result = mimetypes._main(shlex.split("foo.pdf foo.png"))
self.assertEqual(
result,
"type: application/pdf encoding: None\n"
"type: image/png encoding: None"
)

def test_invocation(self):
for command, expected in [
("-l -e image/jpg", ".jpg"),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix mimetypes CLI to handle multiple file parameters.
Loading