diff options
author | Oleg Iarygin <oleg@arhadthedev.net> | 2025-03-13 11:19:22 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2025-03-13 11:19:22 (GMT) |
commit | 328f8b88563d3d259b90969a0793f99b7ea6e9e3 (patch) | |
tree | c4f274dc7456cfe82d0b5fca43da097a6c9caa22 /Lib/mimetypes.py | |
parent | 119bcfad9cac9ac8fa5fa6c0f3ee3ccfca778fe1 (diff) | |
download | cpython-328f8b88563d3d259b90969a0793f99b7ea6e9e3.zip cpython-328f8b88563d3d259b90969a0793f99b7ea6e9e3.tar.gz cpython-328f8b88563d3d259b90969a0793f99b7ea6e9e3.tar.bz2 |
gh-93096: Make `mimetypes` CLI tool public (#93097)
Co-authored-by: Adam Turner <9087854+AA-Turner@users.noreply.github.com>
Co-authored-by: Hugo van Kemenade <1324225+hugovk@users.noreply.github.com>
Diffstat (limited to 'Lib/mimetypes.py')
-rw-r--r-- | Lib/mimetypes.py | 74 |
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__': |