Skip to main content

zql_cli/
cli.rs

1use clap::{Args, ColorChoice, Parser, Subcommand};
2use clap_complete::Shell;
3
4// zql config add <alias> <odbc> [--default]
5// zql config remove <alias>
6// zql config clear
7// zql config list
8// zql exec --config=<alias> <sql>
9// zql run --config=<alias> <file>
10// zql format <file>
11// zql --config=<alias> [sql]
12// zql --completion=[bash|powershell]
13
14#[derive(Parser)]
15#[command(name = "zql", version, about, author)]
16#[command(propagate_version = true)]
17pub struct Cli {
18    #[command(subcommand)]
19    pub command: Option<CommandKind>,
20
21    /// Connection to use instead of default.
22    #[arg(long, value_name = "ALIAS", global = true)]
23    pub config: Option<String>,
24
25    /// Show fields in tabulated output.
26    #[arg(long = "table", short = 't', global = true)]
27    pub table: bool,
28
29    /// Hide header in tabulated and CSV output.
30    #[arg(long = "raw", short = 'r', global = true)]
31    pub raw: bool,
32
33    /// Group records by value in tabulated output.
34    #[arg(long = "group", short = 'g', global = true)]
35    pub group: Option<String>,
36
37    /// Show fields in flattened output.
38    #[arg(long = "flatten", short = 'f', global = true)]
39    pub flatten: bool,
40
41    /// Show types in flattened output.
42    #[arg(long = "verbose", short = 'v', global = true)]
43    pub verbose: bool,
44
45    /// Show colored output (default "auto").
46    #[arg(long = "color", global = true)]
47    pub color: Option<ColorChoice>,
48
49    /// SQL queries to execute.
50    pub queries: Vec<String>,
51
52    /// Create completion script.
53    #[arg(long = "completion")]
54    pub shell: Option<Shell>,
55
56    /// Mock file system for readme file.
57    #[cfg(debug_assertions)]
58    #[arg(long = "mock", default_missing_value = "")]
59    pub mock: Option<String>,
60}
61
62#[derive(Subcommand)]
63pub enum CommandKind {
64    /// Configure connection strings for use with "exec" and "run" commands.
65    Config(ConfigArgs),
66
67    /// Execute SQL from command line.
68    Exec(ExecArgs),
69
70    /// Execute SQL from text file.
71    Run(RunArgs),
72
73    /// Format CSV from text file.
74    Format(FormatArgs),
75
76    /// Show database schema and keywords.
77    Show(ShowArgs),
78}
79
80#[derive(Args)]
81pub struct ConfigArgs {
82    #[command(subcommand)]
83    pub command: ConfigKind,
84}
85
86#[derive(Subcommand)]
87pub enum ConfigKind {
88    /// Add a connection string by alias.
89    Add(ConfigAddArgs),
90
91    /// Remove a connection string by alias.
92    Remove(ConfigRemoveArgs),
93
94    /// Remove all connection strings.
95    Clear,
96
97    /// List all connection strings.
98    List,
99}
100
101#[derive(Args)]
102pub struct ConfigAddArgs {
103    /// Connection alias for use with "--config" option.
104    pub alias: String,
105
106    /// ODBC string associated with the connection alias.
107    pub odbc: Option<String>,
108
109    /// Use as default connection if no "--config" option.
110    #[arg(long)]
111    pub default: bool,
112}
113
114#[derive(Args)]
115pub struct ConfigRemoveArgs {
116    /// Connection alias for use with "--config" option.
117    pub alias: String,
118}
119
120#[derive(Args)]
121pub struct ExecArgs {
122    /// Connection to use instead of default.
123    #[arg(from_global)]
124    pub config: Option<String>,
125
126    /// Show fields in tabulated output.
127    #[arg(from_global)]
128    pub table: bool,
129
130    /// Hide header in tabulated and CSV output.
131    #[arg(from_global)]
132    pub raw: bool,
133
134    /// Group records by value in tabulated output.
135    #[arg(from_global)]
136    pub group: Option<String>,
137
138    /// Show fields in flattened output.
139    #[arg(from_global)]
140    pub flatten: bool,
141
142    /// Show types in flattened output.
143    #[arg(from_global)]
144    pub verbose: bool,
145
146    /// Show colored output (default "auto").
147    #[arg(from_global)]
148    pub color: Option<ColorChoice>,
149
150    /// SQL queries to execute.
151    pub queries: Vec<String>,
152}
153
154#[derive(Args)]
155pub struct RunArgs {
156    /// Connection to use instead of default.
157    #[arg(from_global)]
158    pub config: Option<String>,
159
160    /// Show fields in tabulated output.
161    #[arg(from_global)]
162    pub table: bool,
163
164    /// Hide header in tabulated and CSV output.
165    #[arg(from_global)]
166    pub raw: bool,
167
168    /// Group records by value in tabulated output.
169    #[arg(from_global)]
170    pub group: Option<String>,
171
172    /// Show fields in flattened output.
173    #[arg(from_global)]
174    pub flatten: bool,
175
176    /// Show types in flattened output.
177    #[arg(from_global)]
178    pub verbose: bool,
179
180    /// Show colored output (default "auto").
181    #[arg(from_global)]
182    pub color: Option<ColorChoice>,
183
184    /// SQL files to execute.
185    pub paths: Vec<String>,
186}
187
188#[derive(Args)]
189pub struct FormatArgs {
190    /// Show fields in tabulated output.
191    #[arg(from_global)]
192    pub table: bool,
193
194    /// Detect numbers in tabulated output.
195    #[arg(long = "auto", short = 'a')]
196    pub auto: bool,
197
198    /// Hide header in tabulated and CSV output.
199    #[arg(from_global)]
200    pub raw: bool,
201
202    /// Group records by value in tabulated output.
203    #[arg(from_global)]
204    pub group: Option<String>,
205
206    /// Show fields in flattened output.
207    #[arg(from_global)]
208    pub flatten: bool,
209
210    /// Show types in flattened output.
211    #[arg(from_global)]
212    pub verbose: bool,
213
214    /// Show colored output (default "auto").
215    #[arg(from_global)]
216    pub color: Option<ColorChoice>,
217
218    /// CSV files to format.
219    pub paths: Vec<String>,
220}
221
222#[derive(Args)]
223pub struct ShowArgs {
224    #[command(subcommand)]
225    pub command: ShowKind,
226
227    /// Connection to use instead of default.
228    #[arg(from_global)]
229    pub config: Option<String>,
230}
231
232#[derive(Subcommand)]
233pub enum ShowKind {
234    /// Show all databases for the current connection.
235    Databases,
236
237    /// Show all tables for the current database.
238    Tables(SchemaArgs),
239
240    /// Show all columns for the current database.
241    Columns(SchemaArgs),
242
243    /// Show all procedures for the current database.
244    Procedures(SchemaArgs),
245
246    /// Show all keywords implemented by the server.
247    Keywords,
248
249    /// Show all functions implemented by the server.
250    Functions,
251}
252
253#[derive(Args)]
254pub struct SchemaArgs {
255    /// Context for tables, columns and procedures.
256    pub context: Option<String>,
257}