17
17
18
18
#![ warn( clippy:: all) ]
19
19
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
+
22
23
use std:: fs;
24
+ use std:: io:: { stdin, Read } ;
23
25
24
26
use simple_logger:: SimpleLogger ;
25
27
use sqlparser:: dialect:: * ;
@@ -38,6 +40,9 @@ $ cargo run --example cli FILENAME.sql [--dialectname]
38
40
To print the parse results as JSON:
39
41
$ cargo run --feature json_example --example cli FILENAME.sql [--dialectname]
40
42
43
+ To read from stdin instead of a file:
44
+ $ cargo run --example cli - [--dialectname]
45
+
41
46
"# ,
42
47
) ;
43
48
@@ -57,9 +62,18 @@ $ cargo run --feature json_example --example cli FILENAME.sql [--dialectname]
57
62
s => panic ! ( "Unexpected parameter: {s}" ) ,
58
63
} ;
59
64
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
+ } ;
63
77
let without_bom = if contents. chars ( ) . next ( ) . unwrap ( ) as u64 != 0xfeff {
64
78
contents. as_str ( )
65
79
} else {
0 commit comments