Define and parse `optparse` options for command-line usage.
()
| 25 | """ % EXECUTABLE_NAME_FOR_USAGE |
| 26 | |
| 27 | def parse_options(): |
| 28 | """ |
| 29 | Define and parse `optparse` options for command-line usage. |
| 30 | """ |
| 31 | |
| 32 | try: |
| 33 | optparse = __import__("optparse") |
| 34 | except: |
| 35 | if len(sys.argv) == 2: |
| 36 | return {'input': sys.argv[1], |
| 37 | 'output': None, |
| 38 | 'safe': False, |
| 39 | 'extensions': [], |
| 40 | 'encoding': None }, CRITICAL |
| 41 | else: |
| 42 | print(OPTPARSE_WARNING) |
| 43 | return None, None |
| 44 | |
| 45 | parser = optparse.OptionParser(usage="%prog INPUTFILE [options]") |
| 46 | parser.add_option("-f", "--file", dest="filename", default=sys.stdout, |
| 47 | help="write output to OUTPUT_FILE", |
| 48 | metavar="OUTPUT_FILE") |
| 49 | parser.add_option("-e", "--encoding", dest="encoding", |
| 50 | help="encoding for input and output files",) |
| 51 | parser.add_option("-q", "--quiet", default = CRITICAL, |
| 52 | action="store_const", const=CRITICAL+10, dest="verbose", |
| 53 | help="suppress all messages") |
| 54 | parser.add_option("-v", "--verbose", |
| 55 | action="store_const", const=INFO, dest="verbose", |
| 56 | help="print info messages") |
| 57 | parser.add_option("-s", "--safe", dest="safe", default=False, |
| 58 | metavar="SAFE_MODE", |
| 59 | help="safe mode ('replace', 'remove' or 'escape' user's HTML tag)") |
| 60 | parser.add_option("-o", "--output_format", dest="output_format", |
| 61 | default='xhtml1', metavar="OUTPUT_FORMAT", |
| 62 | help="Format of output. One of 'xhtml1' (default) or 'html4'.") |
| 63 | parser.add_option("--noisy", |
| 64 | action="store_const", const=DEBUG, dest="verbose", |
| 65 | help="print debug messages") |
| 66 | parser.add_option("-x", "--extension", action="append", dest="extensions", |
| 67 | help = "load extension EXTENSION", metavar="EXTENSION") |
| 68 | |
| 69 | (options, args) = parser.parse_args() |
| 70 | |
| 71 | if not len(args) == 1: |
| 72 | parser.print_help() |
| 73 | return None, None |
| 74 | else: |
| 75 | input_file = args[0] |
| 76 | |
| 77 | if not options.extensions: |
| 78 | options.extensions = [] |
| 79 | |
| 80 | return {'input': input_file, |
| 81 | 'output': options.filename, |
| 82 | 'safe_mode': options.safe, |
| 83 | 'extensions': options.extensions, |
| 84 | 'encoding': options.encoding, |