Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 19 additions & 5 deletions examples/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@

#![warn(clippy::all)]

/// A small command-line app to run the parser.
/// Run with `cargo run --example cli`
//! A small command-line app to run the parser.
//! Run with `cargo run --example cli`

use std::fs;
use std::io::{stdin, Read};

use simple_logger::SimpleLogger;
use sqlparser::dialect::*;
Expand All @@ -38,6 +40,9 @@ $ cargo run --example cli FILENAME.sql [--dialectname]
To print the parse results as JSON:
$ cargo run --feature json_example --example cli FILENAME.sql [--dialectname]

To read from stdin instead of a file:
$ cargo run --example cli - [--dialectname]

"#,
);

Expand All @@ -57,9 +62,18 @@ $ cargo run --feature json_example --example cli FILENAME.sql [--dialectname]
s => panic!("Unexpected parameter: {s}"),
};

println!("Parsing from file '{}' using {:?}", &filename, dialect);
let contents = fs::read_to_string(&filename)
.unwrap_or_else(|_| panic!("Unable to read the file {}", &filename));
let contents = if filename == "-" {
println!("Parsing from stdin using {:?}", dialect);
let mut buf = Vec::new();
stdin()
.read_to_end(&mut buf)
.expect("failed to read from stdin");
String::from_utf8(buf).expect("stdin content wasn't valid utf8")
} else {
println!("Parsing from file '{}' using {:?}", &filename, dialect);
fs::read_to_string(&filename)
.unwrap_or_else(|_| panic!("Unable to read the file {}", &filename))
};
let without_bom = if contents.chars().next().unwrap() as u64 != 0xfeff {
contents.as_str()
} else {
Expand Down
Loading