1 | #include <iostream> |
2 | #include <boost/program_options.hpp> |
3 | |
4 | #include <IO/ReadBufferFromFileDescriptor.h> |
5 | #include <IO/ReadHelpers.h> |
6 | #include <Parsers/ParserQuery.h> |
7 | #include <Parsers/parseQuery.h> |
8 | #include <Parsers/formatAST.h> |
9 | #include <Common/TerminalSize.h> |
10 | |
11 | #pragma GCC diagnostic ignored "-Wunused-function" |
12 | #pragma GCC diagnostic ignored "-Wmissing-declarations" |
13 | |
14 | int mainEntryClickHouseFormat(int argc, char ** argv) |
15 | { |
16 | using namespace DB; |
17 | |
18 | boost::program_options::options_description desc = createOptionsDescription("Allowed options" , getTerminalWidth()); |
19 | desc.add_options() |
20 | ("help,h" , "produce help message" ) |
21 | ("hilite" , "add syntax highlight with ANSI terminal escape sequences" ) |
22 | ("oneline" , "format in single line" ) |
23 | ("quiet,q" , "just check syntax, no output on success" ) |
24 | ; |
25 | |
26 | boost::program_options::variables_map options; |
27 | boost::program_options::store(boost::program_options::parse_command_line(argc, argv, desc), options); |
28 | |
29 | if (options.count("help" )) |
30 | { |
31 | std::cout << "Usage: " << argv[0] << " [options] < query" << std::endl; |
32 | std::cout << desc << std::endl; |
33 | return 1; |
34 | } |
35 | |
36 | try |
37 | { |
38 | bool hilite = options.count("hilite" ); |
39 | bool oneline = options.count("oneline" ); |
40 | bool quiet = options.count("quiet" ); |
41 | |
42 | if (quiet && (hilite || oneline)) |
43 | { |
44 | std::cerr << "Options 'hilite' or 'oneline' have no sense in 'quiet' mode." << std::endl; |
45 | return 2; |
46 | } |
47 | |
48 | String query; |
49 | ReadBufferFromFileDescriptor in(STDIN_FILENO); |
50 | readStringUntilEOF(query, in); |
51 | |
52 | const char * pos = query.data(); |
53 | const char * end = pos + query.size(); |
54 | |
55 | ParserQuery parser(end); |
56 | ASTPtr res = parseQuery(parser, pos, end, "query" , 0); |
57 | |
58 | if (!quiet) |
59 | { |
60 | formatAST(*res, std::cout, hilite, oneline); |
61 | std::cout << std::endl; |
62 | } |
63 | } |
64 | catch (...) |
65 | { |
66 | std::cerr << getCurrentExceptionMessage(true); |
67 | return getCurrentExceptionCode(); |
68 | } |
69 | |
70 | return 0; |
71 | } |
72 | |