0
# Command Line Interface
1
2
CLI tool accessible via the `sdp` command for processing DDL files from the command line with various output options and batch processing capabilities. The CLI provides convenient access to all parser functionality without requiring Python programming.
3
4
## Capabilities
5
6
### Main CLI Entry Point
7
8
Primary CLI function exposed as the `sdp` system command for command-line DDL parsing.
9
10
```python { .api }
11
def main():
12
"""
13
Main CLI entry point exposed as 'sdp' command.
14
Handles command-line argument parsing and execution.
15
"""
16
```
17
18
### Argument Parser Creation
19
20
Create and configure the command-line argument parser with all available options.
21
22
```python { .api }
23
def cli():
24
"""
25
Create and configure argument parser for CLI interface.
26
27
Returns:
28
ArgumentParser configured with all CLI options
29
"""
30
```
31
32
### File Processing
33
34
Process individual DDL files with specified options and output formatting.
35
36
```python { .api }
37
def run_for_file(args):
38
"""
39
Process a single DDL file with specified arguments.
40
41
Parameters:
42
- args: Parsed command-line arguments containing file path and options
43
"""
44
```
45
46
### File Extension Validation
47
48
Validate file extensions to ensure appropriate files are processed.
49
50
```python { .api }
51
def correct_extension(file_name: str) -> bool:
52
"""
53
Check if file has a valid DDL file extension.
54
55
Parameters:
56
- file_name (str): Name of file to check
57
58
Returns:
59
bool: True if file has valid extension (.sql, .ddl, etc.)
60
"""
61
```
62
63
## CLI Usage
64
65
### Basic Usage
66
67
```bash
68
# Parse a single DDL file
69
sdp schema.sql
70
71
# Parse with specific output dialect
72
sdp schema.sql --output-mode postgres
73
74
# Parse directory of DDL files (processes all .sql, .ddl, .hql, .bql files)
75
sdp /path/to/ddl/directory
76
```
77
78
### Output Options
79
80
```bash
81
# Save output to custom directory (default: schemas/)
82
sdp schema.sql --target ./output
83
84
# Print results to console only (no file output)
85
sdp schema.sql --no-dump
86
87
# Enable verbose output to console
88
sdp schema.sql -v
89
```
90
91
### Dialect-Specific Processing
92
93
```bash
94
# MySQL-specific output
95
sdp mysql_schema.sql --output-mode mysql
96
97
# PostgreSQL-specific output
98
sdp postgres_schema.sql --output-mode postgres
99
100
# BigQuery-specific output
101
sdp bigquery_schema.sql --output-mode bigquery
102
103
# Oracle-specific output
104
sdp oracle_schema.sql --output-mode oracle
105
106
# Available output modes: sql, mysql, postgres, oracle, mssql, hql,
107
# snowflake, bigquery, redshift, spark_sql, databricks, sqlite,
108
# vertics, ibm_db2, athena
109
```
110
111
## Command Line Arguments
112
113
### Positional Arguments
114
115
- **ddl_file_path**: Path to DDL file or directory to parse
116
117
### Optional Arguments
118
119
- **-t, --target**: Target directory to save parse results in .json files (default: "schemas")
120
- **-v**: Verbose mode - print results to console
121
- **--no-dump**: Parse without saving files, only print to console
122
- **-o, --output-mode**: Output dialect mode for formatting results (default: "sql")
123
124
## Examples
125
126
### Simple Table Parsing
127
128
```bash
129
# Input file: simple_table.sql
130
CREATE TABLE users (
131
id INTEGER PRIMARY KEY,
132
name VARCHAR(50) NOT NULL,
133
email VARCHAR(100) UNIQUE
134
);
135
136
# Command:
137
sdp simple_table.sql
138
139
# Result: Creates simple_table_schema.json in schemas/ directory
140
```
141
142
### Verbose Output
143
144
```bash
145
# Parse and display results in terminal
146
sdp schema.sql -v
147
148
# Parse without creating files, only show in terminal
149
sdp schema.sql --no-dump
150
```
151
152
### Custom Output Directory
153
154
```bash
155
# Save results to custom directory
156
sdp schema.sql --target ./my_output_dir
157
158
# Parse with specific dialect formatting
159
sdp postgres_schema.sql --output-mode postgres --target ./postgres_schemas
160
```
161
162
### Directory Processing
163
164
```bash
165
# Process all DDL files in a directory
166
sdp /path/to/ddl/files/
167
168
# Process directory with verbose output
169
sdp /path/to/ddl/files/ -v --output-mode mysql
170
```
171
172
### Multi-Dialect Processing
173
174
```bash
175
# Process same schema for different target databases
176
sdp schema.sql --output-mode mysql --target ./mysql_output
177
sdp schema.sql --output-mode postgres --target ./postgres_output
178
sdp schema.sql --output-mode oracle --target ./oracle_output
179
```
180
181
## Integration with Python
182
183
The CLI functionality can also be accessed programmatically:
184
185
```python
186
from simple_ddl_parser.cli import main, cli, run_for_file
187
import sys
188
189
# Simulate CLI call
190
sys.argv = ['sdp', 'schema.sql', '--output-mode', 'postgres']
191
main()
192
193
# Or use individual functions
194
parser = cli()
195
args = parser.parse_args(['schema.sql', '--target', './output'])
196
run_for_file(args)
197
```