Define and parse `optparse` options for command-line usage.
(args=None, values=None)
| 43 | |
| 44 | |
| 45 | def parse_options(args=None, values=None): |
| 46 | """ |
| 47 | Define and parse `optparse` options for command-line usage. |
| 48 | """ |
| 49 | usage = """%prog [options] [INPUTFILE] |
| 50 | (STDIN is assumed if no INPUTFILE is given)""" |
| 51 | desc = "A Python implementation of John Gruber's Markdown. " \ |
| 52 | "https://python-markdown.github.io/" |
| 53 | ver = "%%prog %s" % markdown.__version__ |
| 54 | epilog = "WARNING: The Python-Markdown library does NOT sanitize its HTML output. If " \ |
| 55 | "you are processing Markdown input from an untrusted source, it is your " \ |
| 56 | "responsibility to ensure that it is properly sanitized. For more " \ |
| 57 | "information see <https://python-markdown.github.io/sanitization/>." |
| 58 | |
| 59 | parser = optparse.OptionParser(usage=usage, description=desc, version=ver, epilog=epilog) |
| 60 | parser.add_option("-f", "--file", dest="filename", default=None, |
| 61 | help="Write output to OUTPUT_FILE. Defaults to STDOUT.", |
| 62 | metavar="OUTPUT_FILE") |
| 63 | parser.add_option("-e", "--encoding", dest="encoding", |
| 64 | help="Encoding for input and output files.",) |
| 65 | parser.add_option("-o", "--output_format", dest="output_format", |
| 66 | default='xhtml', metavar="OUTPUT_FORMAT", |
| 67 | help="Use output format 'xhtml' (default) or 'html'.") |
| 68 | parser.add_option("-n", "--no_lazy_ol", dest="lazy_ol", |
| 69 | action='store_false', default=True, |
| 70 | help="Observe number of first item of ordered lists.") |
| 71 | parser.add_option("-x", "--extension", action="append", dest="extensions", |
| 72 | help="Load extension EXTENSION.", metavar="EXTENSION") |
| 73 | parser.add_option("-c", "--extension_configs", |
| 74 | dest="configfile", default=None, |
| 75 | help="Read extension configurations from CONFIG_FILE. " |
| 76 | "CONFIG_FILE must be of JSON or YAML format. YAML " |
| 77 | "format requires that a python YAML library be " |
| 78 | "installed. The parsed JSON or YAML must result in a " |
| 79 | "python dictionary which would be accepted by the " |
| 80 | "'extension_configs' keyword on the markdown.Markdown " |
| 81 | "class. The extensions must also be loaded with the " |
| 82 | "`--extension` option.", |
| 83 | metavar="CONFIG_FILE") |
| 84 | parser.add_option("-q", "--quiet", default=CRITICAL, |
| 85 | action="store_const", const=CRITICAL+10, dest="verbose", |
| 86 | help="Suppress all warnings.") |
| 87 | parser.add_option("-v", "--verbose", |
| 88 | action="store_const", const=WARNING, dest="verbose", |
| 89 | help="Print all warnings.") |
| 90 | parser.add_option("--noisy", |
| 91 | action="store_const", const=DEBUG, dest="verbose", |
| 92 | help="Print debug messages.") |
| 93 | |
| 94 | (options, args) = parser.parse_args(args, values) |
| 95 | |
| 96 | if len(args) == 0: |
| 97 | input_file = None |
| 98 | else: |
| 99 | input_file = args[0] |
| 100 | |
| 101 | if not options.extensions: |
| 102 | options.extensions = [] |
no outgoing calls
searching dependent graphs…