(*args)
| 87 | |
| 88 | |
| 89 | def main(*args): |
| 90 | parser = ArgumentParser( |
| 91 | description="Python sqlite3 CLI", |
| 92 | color=True, |
| 93 | ) |
| 94 | parser.add_argument( |
| 95 | "filename", type=str, default=":memory:", nargs="?", |
| 96 | help=( |
| 97 | "SQLite database to open (defaults to ':memory:'). " |
| 98 | "A new database is created if the file does not previously exist." |
| 99 | ), |
| 100 | ) |
| 101 | parser.add_argument( |
| 102 | "sql", type=str, nargs="?", |
| 103 | help=( |
| 104 | "An SQL query to execute. " |
| 105 | "Any returned rows are printed to stdout." |
| 106 | ), |
| 107 | ) |
| 108 | parser.add_argument( |
| 109 | "-v", "--version", action="version", |
| 110 | version=f"SQLite version {sqlite3.sqlite_version}", |
| 111 | help="Print underlying SQLite library version", |
| 112 | ) |
| 113 | args = parser.parse_args(*args) |
| 114 | |
| 115 | if args.filename == ":memory:": |
| 116 | db_name = "a transient in-memory database" |
| 117 | else: |
| 118 | db_name = repr(args.filename) |
| 119 | |
| 120 | # Prepare REPL banner and prompts. |
| 121 | if sys.platform == "win32" and "idlelib.run" not in sys.modules: |
| 122 | eofkey = "CTRL-Z" |
| 123 | else: |
| 124 | eofkey = "CTRL-D" |
| 125 | banner = dedent(f""" |
| 126 | sqlite3 shell, running on SQLite version {sqlite3.sqlite_version} |
| 127 | Connected to {db_name} |
| 128 | |
| 129 | Each command will be run using execute() on the cursor. |
| 130 | Type ".help" for more information; type ".quit" or {eofkey} to quit. |
| 131 | """).strip() |
| 132 | |
| 133 | theme = get_theme() |
| 134 | s = theme.syntax |
| 135 | |
| 136 | # Use RL_PROMPT_START_IGNORE (\001) and RL_PROMPT_END_IGNORE (\002) to |
| 137 | # bracket non-printing characters. This tells readline to ignore them |
| 138 | # when calculating screen space for redisplay during history scrolling. |
| 139 | sys.ps1 = f"\001{s.prompt}\002sqlite> \001{s.reset}\002" |
| 140 | sys.ps2 = f"\001{s.prompt}\002 ... \001{s.reset}\002" |
| 141 | |
| 142 | con = sqlite3.connect(args.filename, isolation_level=None) |
| 143 | try: |
| 144 | if args.sql: |
| 145 | # SQL statement provided on the command-line; execute it directly. |
| 146 | execute(con, args.sql, suppress_errors=False, theme=theme) |
no test coverage detected
searching dependent graphs…