|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | + |
| 3 | +import argparse |
| 4 | + |
| 5 | +from src import arch |
| 6 | +from src.api import errmsg |
| 7 | +from src.api.config import OPTIONS |
| 8 | + |
| 9 | +from .version import VERSION |
| 10 | + |
| 11 | + |
| 12 | +def parse_warning_option(code: str) -> str: |
| 13 | + if not errmsg.is_valid_warning_code(code): |
| 14 | + raise argparse.ArgumentTypeError(f"Invalid warning option 'W{code}'") |
| 15 | + return code |
| 16 | + |
| 17 | + |
| 18 | +# ------------------------------------------------------------ |
| 19 | +# Command line parser |
| 20 | +# ------------------------------------------------------------ |
| 21 | +def parser() -> argparse.ArgumentParser: |
| 22 | + parser_ = argparse.ArgumentParser( |
| 23 | + prefix_chars='-+' |
| 24 | + ) |
| 25 | + parser_.add_argument('PROGRAM', type=str, |
| 26 | + help='BASIC program file') |
| 27 | + parser_.add_argument('-d', '--debug', dest='debug', default=OPTIONS.Debug, action='count', |
| 28 | + help='Enable verbosity/debugging output. Additional -d increase verbosity/debug level') |
| 29 | + parser_.add_argument('-O', '--optimize', type=int, default=OPTIONS.optimization, |
| 30 | + help='Sets optimization level. ' |
| 31 | + '0 = None (default level is {0})'.format(OPTIONS.optimization)) |
| 32 | + parser_.add_argument('-o', '--output', type=str, dest='output_file', default=None, |
| 33 | + help='Sets output file. Default is input filename with .bin extension') |
| 34 | + parser_.add_argument('-T', '--tzx', action='store_true', |
| 35 | + help="Sets output format to tzx (default is .bin)") |
| 36 | + parser_.add_argument('-t', '--tap', action='store_true', |
| 37 | + help="Sets output format to tap (default is .bin)") |
| 38 | + parser_.add_argument('-B', '--BASIC', action='store_true', dest='basic', |
| 39 | + help="Creates a BASIC loader which loads the rest of the CODE. Requires -T ot -t") |
| 40 | + parser_.add_argument('-a', '--autorun', action='store_true', |
| 41 | + help="Sets the program to be run once loaded") |
| 42 | + parser_.add_argument('-A', '--asm', action='store_true', |
| 43 | + help="Sets output format to asm") |
| 44 | + parser_.add_argument('-S', '--org', type=str, default=str(OPTIONS.org), |
| 45 | + help="Start of machine code. By default %i" % OPTIONS.org) |
| 46 | + parser_.add_argument('-e', '--errmsg', type=str, dest='stderr', default=OPTIONS.StdErrFileName, |
| 47 | + help='Error messages file (standard error console by default)') |
| 48 | + parser_.add_argument('--array-base', type=int, default=OPTIONS.array_base, |
| 49 | + help='Default lower index for arrays ({0} by default)'.format(OPTIONS.array_base)) |
| 50 | + parser_.add_argument('--string-base', type=int, default=OPTIONS.string_base, |
| 51 | + help='Default lower index for strings ({0} by default)'.format(OPTIONS.array_base)) |
| 52 | + parser_.add_argument('-Z', '--sinclair', action='store_true', |
| 53 | + help='Enable by default some more original ZX Spectrum Sinclair BASIC features:' |
| 54 | + ' ATTR, SCREEN$, POINT') |
| 55 | + parser_.add_argument('-H', '--heap-size', type=int, default=OPTIONS.heap_size, |
| 56 | + help='Sets heap size in bytes (default {0} bytes)'.format(OPTIONS.heap_size)) |
| 57 | + parser_.add_argument('--debug-memory', action='store_true', |
| 58 | + help='Enables out-of-memory debug') |
| 59 | + parser_.add_argument('--debug-array', action='store_true', |
| 60 | + help='Enables array boundary checking') |
| 61 | + parser_.add_argument('--strict-bool', action='store_true', |
| 62 | + help='Enforce boolean values to be 0 or 1') |
| 63 | + parser_.add_argument('--enable-break', action='store_true', |
| 64 | + help='Enables program execution BREAK detection') |
| 65 | + parser_.add_argument('-E', '--emit-backend', action='store_true', |
| 66 | + help='Emits backend code instead of ASM or binary') |
| 67 | + parser_.add_argument('--explicit', action='store_true', |
| 68 | + help='Requires all variables and functions to be declared before used') |
| 69 | + parser_.add_argument('-D', '--define', type=str, dest='defines', action='append', |
| 70 | + help='Defines de given macro. Eg. -D MYDEBUG or -D NAME=Value') |
| 71 | + parser_.add_argument('-M', '--mmap', type=str, dest='memory_map', default=None, |
| 72 | + help='Generate label memory map') |
| 73 | + parser_.add_argument('-i', '--ignore-case', action='store_true', |
| 74 | + help='Ignore case. Makes variable names are case insensitive') |
| 75 | + parser_.add_argument('-I', '--include-path', type=str, default='', |
| 76 | + help='Add colon separated list of directories to add to include path. e.g. -I dir1:dir2') |
| 77 | + parser_.add_argument('--strict', action='store_true', |
| 78 | + help='Enables strict mode. Force explicit type declaration') |
| 79 | + parser_.add_argument('--headerless', action='store_true', |
| 80 | + help='Header-less mode: omit asm prologue and epilogue') |
| 81 | + parser_.add_argument('--version', action='version', version='%(prog)s {0}'.format(VERSION)) |
| 82 | + parser_.add_argument('--parse-only', action='store_true', |
| 83 | + help='Only parses to check for syntax and semantic errors') |
| 84 | + parser_.add_argument('--append-binary', default=[], action='append', |
| 85 | + help='Appends binary to tape file (only works with -t or -T)') |
| 86 | + parser_.add_argument('--append-headless-binary', default=[], action='append', |
| 87 | + help='Appends binary to tape file (only works with -t or -T)') |
| 88 | + parser_.add_argument('-N', '--zxnext', action='store_true', |
| 89 | + help='Enables ZX Next asm extended opcodes') |
| 90 | + parser_.add_argument('--arch', type=str, default=arch.AVAILABLE_ARCHITECTURES[0], |
| 91 | + help=f"Target architecture (defaults is'{arch.AVAILABLE_ARCHITECTURES[0]}'). " |
| 92 | + f"Available architectures: {','.join(arch.AVAILABLE_ARCHITECTURES)}") |
| 93 | + parser_.add_argument('--expect-warnings', default=OPTIONS.expect_warnings, type=int, |
| 94 | + help='Expects N warnings: first N warnings will be silenced') |
| 95 | + parser_.add_argument('-W', '--disable-warning', type=parse_warning_option, action='append', |
| 96 | + help='Disables warning WXXX (i.e. -W100 disables warning with code W100)') |
| 97 | + parser_.add_argument('+W', '--enable-warning', type=parse_warning_option, action='append', |
| 98 | + help='Disables warning WXXX (i.e. -W100 disables warning with code W100)') |
| 99 | + parser_.add_argument('--hide-warning-codes', action='store_true', |
| 100 | + help='Hides WXXX codes') |
| 101 | + return parser_ |
0 commit comments