Skip to content

Commit c135f85

Browse files
authored
New command line flag: --fix (#554)
2 parents 1be6cb0 + 4120857 commit c135f85

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ optional arguments:
106106
-h, --help show this help message and exit
107107
-o, --output OUTPUT
108108
File to write SQL output (defaults to stdout)
109+
--fix Update the file in-place
109110
-l, --language {bigquery,db2,hive,mariadb,mysql,n1ql,plsql,postgresql,redshift,singlestoredb,snowflake,spark,sql,sqlite,trino,tsql}
110111
SQL dialect (defaults to basic sql)
111112
-c, --config CONFIG

bin/sql-formatter-cli.cjs

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class PrettierSQLArgs {
1616

1717
this.query = this.getInput();
1818
const formattedQuery = format(this.query, this.cfg).trim() + '\n';
19-
this.writeOutput(this.args.output, formattedQuery);
19+
this.writeOutput(this.getOutputFile(this.args), formattedQuery);
2020
}
2121

2222
getParser() {
@@ -35,6 +35,12 @@ class PrettierSQLArgs {
3535
help: 'File to write SQL output (defaults to stdout)',
3636
});
3737

38+
parser.add_argument('--fix', {
39+
help: 'Update the file in-place',
40+
action: 'store_const',
41+
const: true,
42+
});
43+
3844
parser.add_argument('-l', '--language', {
3945
help: 'SQL Formatter dialect (defaults to basic sql)',
4046
choices: supportedDialects,
@@ -104,6 +110,22 @@ class PrettierSQLArgs {
104110
}
105111
}
106112

113+
getOutputFile(args) {
114+
if (args.output && args.fix) {
115+
console.error('Error: Cannot use both --output and --fix options simultaneously');
116+
process.exit(1);
117+
}
118+
if (args.fix && !args.file) {
119+
console.error('Error: The --fix option cannot be used without a filename');
120+
process.exit(1);
121+
}
122+
if (args.fix) {
123+
return args.file;
124+
} else {
125+
return args.output;
126+
}
127+
}
128+
107129
writeOutput(file, query) {
108130
if (!file) {
109131
// No output file, write to console

0 commit comments

Comments
 (0)