|
| 1 | +import Foundation |
| 2 | +import SQLiteKit |
| 3 | + |
| 4 | +/// Hand-rolled argv parser for the `sqlite3` CLI. SQLite uses single-dash |
| 5 | +/// long options (`-csv`, `-header`, `-separator X`), which ArgumentParser |
| 6 | +/// can't express, so — like the `rg` / `fd` ports — we parse argv directly. |
| 7 | +enum Parser { |
| 8 | + struct Options { |
| 9 | + var databasePath: String? |
| 10 | + var sql: [String] = [] |
| 11 | + var mode: OutputMode = .list |
| 12 | + var showHeader = false |
| 13 | + var headerExplicit = false |
| 14 | + var separator = "|" |
| 15 | + var nullValue = "" |
| 16 | + var readonly = false |
| 17 | + var interactive = false |
| 18 | + var echo = false |
| 19 | + var bail = false |
| 20 | + var safe = false |
| 21 | + var initFile: String? |
| 22 | + var commands: [String] = [] |
| 23 | + var special: Special = .none |
| 24 | + } |
| 25 | + |
| 26 | + enum Special { case none, help, version } |
| 27 | + |
| 28 | + struct ArgError: Error { let message: String } |
| 29 | + |
| 30 | + // SQLite's single-dash long options are parsed by one flat switch over argv, |
| 31 | + // inherently a long, high-branch function. |
| 32 | + // swiftlint:disable:next cyclomatic_complexity function_body_length |
| 33 | + static func parse(_ argv: [String]) throws -> Options { |
| 34 | + var options = Options() |
| 35 | + var positionals: [String] = [] |
| 36 | + var i = 0 |
| 37 | + |
| 38 | + func value(for flag: String) throws -> String { |
| 39 | + guard i + 1 < argv.count else { |
| 40 | + throw ArgError(message: "option requires an argument: \(flag)") |
| 41 | + } |
| 42 | + i += 1 |
| 43 | + return argv[i] |
| 44 | + } |
| 45 | + |
| 46 | + while i < argv.count { |
| 47 | + let arg = argv[i] |
| 48 | + switch arg { |
| 49 | + case "-help", "--help", "-?": options.special = .help |
| 50 | + case "-version", "--version": options.special = .version |
| 51 | + case "-csv": options.mode = .csv |
| 52 | + case "-json": options.mode = .json |
| 53 | + case "-line": options.mode = .line |
| 54 | + case "-column": options.mode = .column |
| 55 | + case "-list": options.mode = .list |
| 56 | + case "-tabs": options.mode = .tabs |
| 57 | + case "-ascii": options.mode = .ascii |
| 58 | + case "-html": options.mode = .html |
| 59 | + case "-markdown": options.mode = .markdown |
| 60 | + case "-table": options.mode = .table |
| 61 | + case "-box": options.mode = .box |
| 62 | + case "-quote": options.mode = .quote |
| 63 | + case "-header", "-headers": options.showHeader = true; options.headerExplicit = true |
| 64 | + case "-noheader", "-noheaders": options.showHeader = false; options.headerExplicit = true |
| 65 | + case "-readonly": options.readonly = true |
| 66 | + case "-batch": options.interactive = false |
| 67 | + case "-interactive": options.interactive = true |
| 68 | + case "-echo": options.echo = true |
| 69 | + case "-bail": options.bail = true |
| 70 | + case "-safe": options.safe = true |
| 71 | + case "-separator": options.separator = try value(for: arg) |
| 72 | + case "-nullvalue": options.nullValue = try value(for: arg) |
| 73 | + case "-init": options.initFile = try value(for: arg) |
| 74 | + case "-cmd": options.commands.append(try value(for: arg)) |
| 75 | + default: |
| 76 | + if arg.hasPrefix("-") && arg.count > 1 { |
| 77 | + throw ArgError(message: "unknown option: \(arg)") |
| 78 | + } |
| 79 | + positionals.append(arg) |
| 80 | + } |
| 81 | + i += 1 |
| 82 | + } |
| 83 | + |
| 84 | + if let first = positionals.first { |
| 85 | + options.databasePath = first |
| 86 | + options.sql = Array(positionals.dropFirst()) |
| 87 | + } |
| 88 | + return options |
| 89 | + } |
| 90 | + |
| 91 | + static let helpText = """ |
| 92 | + Usage: sqlite3 [OPTIONS] FILENAME [SQL] |
| 93 | +
|
| 94 | + FILENAME is the SQLite database to open. Omit it (or use ":memory:") |
| 95 | + for a transient in-memory database. A trailing SQL argument runs and |
| 96 | + then exits; otherwise SQL is read from standard input. |
| 97 | +
|
| 98 | + OPTIONS: |
| 99 | + -version show the SQLite library version and exit |
| 100 | + -help show this message and exit |
| 101 | + -readonly open the database read-only |
| 102 | + -init FILE run FILE before reading the main input |
| 103 | + -cmd COMMAND run COMMAND before reading the main input |
| 104 | + -echo print each statement before running it |
| 105 | + -bail stop after the first error |
| 106 | + -batch non-interactive mode |
| 107 | + -interactive interactive mode (prompts; SQL run line-by-line) |
| 108 | + -safe refuse dot-commands that touch the filesystem/shell |
| 109 | +
|
| 110 | + -list values separated by .separator (default) |
| 111 | + -csv comma-separated values |
| 112 | + -tabs tab-separated values |
| 113 | + -ascii 0x1F/0x1E separated values |
| 114 | + -column left-aligned columns |
| 115 | + -markdown Markdown table |
| 116 | + -table ASCII-art table |
| 117 | + -box Unicode box-drawing table |
| 118 | + -line one value per line |
| 119 | + -json JSON array of objects |
| 120 | + -html HTML <TR>/<TD> rows |
| 121 | + -quote SQL-literal values |
| 122 | + -header / -noheader show or hide column headers |
| 123 | + -separator SEP field separator for -list mode (default "|") |
| 124 | + -nullvalue STR text to print for NULL values (default "") |
| 125 | +
|
| 126 | + Dot-commands (at a statement boundary): |
| 127 | + .tables [PATTERN] list tables and views |
| 128 | + .schema [TABLE] show CREATE statements |
| 129 | + .databases list attached databases |
| 130 | + .indexes [TABLE] list indexes |
| 131 | + .mode MODE [TABLE] set output mode (list/csv/tabs/ascii/column/ |
| 132 | + markdown/table/box/line/json/html/quote/insert) |
| 133 | + .headers on|off show or hide headers |
| 134 | + .separator SEP set the -list separator |
| 135 | + .nullvalue STR set the NULL placeholder |
| 136 | + .width N1 N2 ... set column widths (negative right-justifies, 0 auto) |
| 137 | + .limit [NAME [VAL]] show or set run-time limits |
| 138 | + .dump [TABLE] dump the database (or one table) as SQL |
| 139 | + .fullschema show the schema plus the ANALYZE (stat) tables |
| 140 | + .echo on|off echo each statement before running it |
| 141 | + .bail on|off stop after an error |
| 142 | + .changes on|off report changed-row counts after each statement |
| 143 | + .eqp on|off print the query plan before each statement |
| 144 | + .print TEXT... print TEXT |
| 145 | + .import FILE TABLE import delimited FILE into TABLE |
| 146 | + .output [FILE] send output to FILE (stdout if omitted) |
| 147 | + .once FILE send the next command's output to FILE |
| 148 | + .read FILE run SQL from FILE |
| 149 | + .open FILE close the current database and open FILE |
| 150 | + .backup [DB] FILE back up the database to FILE |
| 151 | + .restore [DB] FILE restore the database from FILE |
| 152 | + .show show current settings |
| 153 | + .help show this message |
| 154 | + .quit / .exit exit |
| 155 | +
|
| 156 | + """ |
| 157 | +} |
0 commit comments