1717
1818#![ warn( clippy:: all) ]
1919
20- /// A small command-line app to run the parser.
21- /// Run with `cargo run --example cli`
20+ //! A small command-line app to run the parser.
21+ //! Run with `cargo run --example cli`
22+
2223use std:: fs;
24+ use std:: io:: { stdin, Read } ;
2325
2426use simple_logger:: SimpleLogger ;
2527use sqlparser:: dialect:: * ;
@@ -38,6 +40,9 @@ $ cargo run --example cli FILENAME.sql [--dialectname]
3840To print the parse results as JSON:
3941$ cargo run --feature json_example --example cli FILENAME.sql [--dialectname]
4042
43+ To read from stdin instead of a file:
44+ $ cargo run --example cli - [--dialectname]
45+
4146"# ,
4247 ) ;
4348
@@ -57,9 +62,18 @@ $ cargo run --feature json_example --example cli FILENAME.sql [--dialectname]
5762 s => panic ! ( "Unexpected parameter: {s}" ) ,
5863 } ;
5964
60- println ! ( "Parsing from file '{}' using {:?}" , & filename, dialect) ;
61- let contents = fs:: read_to_string ( & filename)
62- . unwrap_or_else ( |_| panic ! ( "Unable to read the file {}" , & filename) ) ;
65+ let contents = if filename == "-" {
66+ println ! ( "Parsing from stdin using {:?}" , dialect) ;
67+ let mut buf = Vec :: new ( ) ;
68+ stdin ( )
69+ . read_to_end ( & mut buf)
70+ . expect ( "failed to read from stdin" ) ;
71+ String :: from_utf8 ( buf) . expect ( "stdin content wasn't valid utf8" )
72+ } else {
73+ println ! ( "Parsing from file '{}' using {:?}" , & filename, dialect) ;
74+ fs:: read_to_string ( & filename)
75+ . unwrap_or_else ( |_| panic ! ( "Unable to read the file {}" , & filename) )
76+ } ;
6377 let without_bom = if contents. chars ( ) . next ( ) . unwrap ( ) as u64 != 0xfeff {
6478 contents. as_str ( )
6579 } else {
0 commit comments