summaryrefslogtreecommitdiffstats
path: root/Lib/mimetypes.py
diff options
context:
space:
mode:
Diffstat (limited to 'Lib/mimetypes.py')
-rw-r--r--Lib/mimetypes.py74
1 files changed, 31 insertions, 43 deletions
diff --git a/Lib/mimetypes.py b/Lib/mimetypes.py
index ebad9a9..6b94fe3 100644
--- a/Lib/mimetypes.py
+++ b/Lib/mimetypes.py
@@ -670,50 +670,38 @@ _default_mime_types()
def _main():
- import getopt
+ """Run the mimetypes command-line interface."""
import sys
-
- USAGE = """\
-Usage: mimetypes.py [options] type
-
-Options:
- --help / -h -- print this message and exit
- --lenient / -l -- additionally search of some common, but non-standard
- types.
- --extension / -e -- guess extension instead of type
-
-More than one type argument may be given.
-"""
-
- def usage(code, msg=''):
- print(USAGE)
- if msg: print(msg)
- sys.exit(code)
-
- try:
- opts, args = getopt.getopt(sys.argv[1:], 'hle',
- ['help', 'lenient', 'extension'])
- except getopt.error as msg:
- usage(1, msg)
-
- strict = 1
- extension = 0
- for opt, arg in opts:
- if opt in ('-h', '--help'):
- usage(0)
- elif opt in ('-l', '--lenient'):
- strict = 0
- elif opt in ('-e', '--extension'):
- extension = 1
- for gtype in args:
- if extension:
- guess = guess_extension(gtype, strict)
- if not guess: print("I don't know anything about type", gtype)
- else: print(guess)
- else:
- guess, encoding = guess_type(gtype, strict)
- if not guess: print("I don't know anything about type", gtype)
- else: print('type:', guess, 'encoding:', encoding)
+ from argparse import ArgumentParser
+
+ parser = ArgumentParser(description='map filename extensions to MIME types')
+ parser.add_argument(
+ '-e', '--extension',
+ action='store_true',
+ help='guess extension instead of type'
+ )
+ parser.add_argument(
+ '-l', '--lenient',
+ action='store_true',
+ help='additionally search for common but non-standard types'
+ )
+ parser.add_argument('type', nargs='+', help='a type to search')
+ args = parser.parse_args()
+
+ if args.extension:
+ for gtype in args.type:
+ guess = guess_extension(gtype, not args.lenient)
+ if guess:
+ print(guess)
+ else:
+ sys.exit(f"error: unknown type {gtype}")
+ else:
+ for gtype in args.type:
+ guess, encoding = guess_type(gtype, not args.lenient)
+ if guess:
+ print('type:', guess, 'encoding:', encoding)
+ else:
+ sys.exit(f"error: media type unknown for {gtype}")
if __name__ == '__main__':