Skip to content

Commit

Permalink
+support for parsing stdin in CLI (#880)
Browse files Browse the repository at this point in the history
  • Loading branch information
mgradowski committed Jan 3, 2023
1 parent ec5c809 commit f89b38e
Showing 1 changed file with 15 additions and 3 deletions.
18 changes: 15 additions & 3 deletions sqlglot/__main__.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
import argparse
import sys

import sqlglot

parser = argparse.ArgumentParser(description="Transpile SQL")
parser.add_argument("sql", metavar="sql", type=str, help="SQL string to transpile")
parser.add_argument(
"sql",
metavar="sql",
type=str,
help="SQL statement(s) to transpile, or - to parse stdin.",
)
parser.add_argument(
"--read",
dest="read",
Expand Down Expand Up @@ -48,14 +54,20 @@
args = parser.parse_args()
error_level = sqlglot.ErrorLevel[args.error_level.upper()]

sql = sys.stdin.read() if args.sql == "-" else args.sql

if args.parse:
sqls = [
repr(expression)
for expression in sqlglot.parse(args.sql, read=args.read, error_level=error_level)
for expression in sqlglot.parse(
sql,
read=args.read,
error_level=error_level,
)
]
else:
sqls = sqlglot.transpile(
args.sql,
sql,
read=args.read,
write=args.write,
identify=args.identify,
Expand Down

0 comments on commit f89b38e

Please sign in to comment.