| 41 | |
| 42 | |
| 43 | def main(args=None): |
| 44 | if args is None: |
| 45 | args = sys.argv[1:] |
| 46 | |
| 47 | version = metadata.version("qrcode") |
| 48 | parser = optparse.OptionParser(usage=(__doc__ or "").strip(), version=version) |
| 49 | |
| 50 | # Wrap parser.error in a typed NoReturn method for better typing. |
| 51 | def raise_error(msg: str) -> NoReturn: |
| 52 | parser.error(msg) |
| 53 | raise # pragma: no cover |
| 54 | |
| 55 | parser.add_option( |
| 56 | "--factory", |
| 57 | help="Full python path to the image factory class to " |
| 58 | "create the image with. You can use the following shortcuts to the " |
| 59 | f"built-in image factory classes: {commas(default_factories)}.", |
| 60 | ) |
| 61 | parser.add_option( |
| 62 | "--factory-drawer", |
| 63 | help=f"Use an alternate drawer. {get_drawer_help()}.", |
| 64 | ) |
| 65 | parser.add_option( |
| 66 | "--optimize", |
| 67 | type=int, |
| 68 | help="Optimize the data by looking for chunks " |
| 69 | "of at least this many characters that could use a more efficient " |
| 70 | "encoding method. Use 0 to turn off chunk optimization.", |
| 71 | ) |
| 72 | parser.add_option( |
| 73 | "--error-correction", |
| 74 | type="choice", |
| 75 | choices=sorted(error_correction.keys()), |
| 76 | default="M", |
| 77 | help="The error correction level to use. Choices are L (7%), " |
| 78 | "M (15%, default), Q (25%), and H (30%).", |
| 79 | ) |
| 80 | parser.add_option( |
| 81 | "--ascii", help="Print as ascii even if stdout is piped.", action="store_true" |
| 82 | ) |
| 83 | parser.add_option( |
| 84 | "--output", |
| 85 | help="The output file. If not specified, the image is sent to " |
| 86 | "the standard output.", |
| 87 | ) |
| 88 | |
| 89 | opts, args = parser.parse_args(args) |
| 90 | |
| 91 | if opts.factory: |
| 92 | module = default_factories.get(opts.factory, opts.factory) |
| 93 | try: |
| 94 | image_factory = get_factory(module) |
| 95 | except ValueError as e: |
| 96 | raise_error(str(e)) |
| 97 | else: |
| 98 | image_factory = None |
| 99 | |
| 100 | qr = qrcode.QRCode( |