@@ -191,7 +191,7 @@ An example usage of the module::
191191
192192.. _mimetypes-objects :
193193
194- MimeTypes Objects
194+ MimeTypes objects
195195-----------------
196196
197197The :class: `MimeTypes ` class may be useful for applications which may want more
@@ -307,3 +307,97 @@ than one MIME-type database; it provides an interface similar to the one of the
307307
308308 When *strict * is ``True `` (the default), the mapping will be added to the
309309 official MIME types, otherwise to the non-standard ones.
310+
311+
312+ .. _mimetypes-cli :
313+
314+ Command-line usage
315+ ------------------
316+
317+ The :mod: `!mimetypes ` module can be executed as a script from the command line.
318+
319+ .. code-block :: sh
320+
321+ python -m mimetypes [-h] [-e] [-l] type [type ...]
322+
323+ The following options are accepted:
324+
325+ .. program :: mimetypes
326+
327+ .. cmdoption :: -h
328+ --help
329+
330+ Show the help message and exit.
331+
332+ .. cmdoption :: -e
333+ --extension
334+
335+ Guess extension instead of type.
336+
337+ .. cmdoption :: -l
338+ --lenient
339+
340+ Additionally search for some common, but non-standard types.
341+
342+ By default the script converts MIME types to file extensions.
343+ However, if ``--extension `` is specified,
344+ it converts file extensions to MIME types.
345+
346+ For each ``type `` entry, the script writes a line into the standard output
347+ stream. If an unknown type occurs, it writes an error message into the
348+ standard error stream and exits with the return code ``1 ``.
349+
350+
351+ .. mimetypes-cli-example:
352+
353+ Command-line example
354+ --------------------
355+
356+ Here are some examples of typical usage of the :mod: `!mimetypes ` command-line
357+ interface:
358+
359+ .. code-block :: console
360+
361+ $ # get a MIME type by a file name
362+ $ python -m mimetypes filename.png
363+ type: image/png encoding: None
364+
365+ $ # get a MIME type by a URL
366+ $ python -m mimetypes https://example.com/filename.txt
367+ type: text/plain encoding: None
368+
369+ $ # get a complex MIME type
370+ $ python -m mimetypes filename.tar.gz
371+ type: application/x-tar encoding: gzip
372+
373+ $ # get a MIME type for a rare file extension
374+ $ python -m mimetypes filename.pict
375+ error: unknown extension of filename.pict
376+
377+ $ # now look in the extended database built into Python
378+ $ python -m mimetypes --lenient filename.pict
379+ type: image/pict encoding: None
380+
381+ $ # get a file extension by a MIME type
382+ $ python -m mimetypes --extension text/javascript
383+ .js
384+
385+ $ # get a file extension by a rare MIME type
386+ $ python -m mimetypes --extension text/xul
387+ error: unknown type text/xul
388+
389+ $ # now look in the extended database again
390+ $ python -m mimetypes --extension --lenient text/xul
391+ .xul
392+
393+ $ # try to feed an unknown file extension
394+ $ python -m mimetypes filename.sh filename.nc filename.xxx filename.txt
395+ type: application/x-sh encoding: None
396+ type: application/x-netcdf encoding: None
397+ error: unknown extension of filename.xxx
398+
399+ $ # try to feed an unknown MIME type
400+ $ python -m mimetypes --extension audio/aac audio/opus audio/future audio/x-wav
401+ .aac
402+ .opus
403+ error: unknown type audio/future
0 commit comments