0
# CLI Interface
1
2
Command-line interface for batch SVG optimization with support for file/folder processing, recursive operations, and extensive configuration options.
3
4
## Capabilities
5
6
### Basic Usage
7
8
The SVGO command-line interface provides powerful batch processing capabilities.
9
10
```bash { .api }
11
# Basic syntax
12
svgo [INPUT...] [OPTIONS]
13
14
# Common usage patterns
15
svgo input.svg # Optimize single file
16
svgo input.svg -o output.svg # Specify output file
17
svgo *.svg # Optimize multiple files
18
svgo -f icons/ -o optimized/ # Optimize folder
19
svgo -f icons/ -r # Optimize folder recursively
20
svgo -s '<svg>...</svg>' # Optimize from string
21
svgo - < input.svg > output.svg # Use stdin/stdout
22
```
23
24
### Input Options
25
26
Various ways to provide SVG input to the CLI.
27
28
```bash { .api }
29
# File inputs
30
-i, --input <INPUT...> # Input files, "-" for STDIN
31
-s, --string <STRING> # Input SVG data string
32
-f, --folder <FOLDER> # Input folder, optimize all *.svg files
33
34
# Examples
35
svgo -i file1.svg file2.svg # Multiple input files
36
svgo -s '<svg><rect/></svg>' # Direct SVG string
37
svgo -f ./icons/ # Process entire folder
38
echo '<svg>...</svg>' | svgo - # From stdin
39
```
40
41
### Output Options
42
43
Control where optimized SVGs are written.
44
45
```bash { .api }
46
# Output destinations
47
-o, --output <OUTPUT...> # Output file or folder, "-" for STDOUT
48
49
# Examples
50
svgo input.svg -o output.svg # Specific output file
51
svgo *.svg -o optimized/ # Output to folder
52
svgo input.svg -o - # Output to stdout
53
svgo -f icons/ -o dist/icons/ # Folder to folder
54
```
55
56
### Optimization Options
57
58
Configure the optimization process.
59
60
```bash { .api }
61
# Precision and quality
62
-p, --precision <INTEGER> # Set floating point precision
63
--multipass # Multiple optimization passes
64
65
# Plugin configuration
66
--config <CONFIG> # Custom config file (.js, .mjs, .cjs)
67
68
# Output format
69
--datauri <FORMAT> # Output as Data URI (base64|enc|unenc)
70
--pretty # Pretty-print SVG output
71
--indent <INTEGER> # Indentation for pretty printing
72
--eol <EOL> # Line breaks (lf|crlf)
73
--final-newline # Ensure final newline
74
75
# Examples
76
svgo input.svg -p 2 # 2 decimal places
77
svgo input.svg --multipass # Multiple passes
78
svgo input.svg --pretty --indent 2 # Pretty formatted
79
svgo input.svg --datauri base64 # Data URI output
80
svgo input.svg --config ./my-config.js # Custom config
81
```
82
83
### Folder Processing
84
85
Advanced folder processing options.
86
87
```bash { .api }
88
# Folder options
89
-f, --folder <FOLDER> # Input folder
90
-r, --recursive # Process subfolders recursively
91
--exclude <PATTERN...> # Exclude files matching regex pattern
92
93
# Examples
94
svgo -f icons/ # Process icons folder
95
svgo -f icons/ -r # Include subfolders
96
svgo -f icons/ -r -o dist/ # Recursive with output folder
97
svgo -f src/ -r --exclude "temp" "draft.*" # Exclude patterns
98
```
99
100
### Utility Options
101
102
Additional utility and output control options.
103
104
```bash { .api }
105
# Information and control
106
-q, --quiet # Only error messages
107
--show-plugins # List available plugins and exit
108
--no-color # Disable colored output
109
-v, --version # Show version
110
-h, --help # Show help
111
112
# Examples
113
svgo --show-plugins # List all plugins
114
svgo input.svg -q # Quiet mode
115
svgo --version # Show SVGO version
116
```
117
118
### Usage Examples
119
120
**Single File Optimization:**
121
122
```bash
123
# Basic optimization
124
svgo icon.svg
125
126
# With custom output
127
svgo icon.svg -o icon-optimized.svg
128
129
# With precision control
130
svgo icon.svg -p 3 -o icon-optimized.svg
131
132
# Pretty formatted output
133
svgo icon.svg --pretty --indent 2 -o icon-pretty.svg
134
```
135
136
**Multiple File Processing:**
137
138
```bash
139
# Process multiple files
140
svgo file1.svg file2.svg file3.svg
141
142
# Using wildcards
143
svgo *.svg
144
svgo icons/*.svg
145
146
# With output directory
147
svgo *.svg -o optimized/
148
```
149
150
**Folder Processing:**
151
152
```bash
153
# Process all SVGs in folder
154
svgo -f ./icons/
155
156
# Recursive processing
157
svgo -f ./src/ -r
158
159
# With output folder
160
svgo -f ./src/icons/ -o ./dist/icons/
161
162
# Exclude certain files
163
svgo -f ./icons/ -r --exclude "temp.*" "draft-.*"
164
```
165
166
**Data URI Output:**
167
168
```bash
169
# Base64 Data URI
170
svgo icon.svg --datauri base64
171
172
# URL encoded Data URI
173
svgo icon.svg --datauri enc
174
175
# Unencoded Data URI
176
svgo icon.svg --datauri unenc
177
```
178
179
**Configuration Files:**
180
181
```bash
182
# Use custom config
183
svgo input.svg --config ./svgo.config.js
184
185
# Config file examples:
186
# svgo.config.js
187
export default {
188
multipass: true,
189
plugins: [
190
'preset-default',
191
{
192
name: 'removeAttrs',
193
params: { attrs: ['data-*'] }
194
}
195
]
196
};
197
```
198
199
**Pipeline Usage:**
200
201
```bash
202
# Using stdin/stdout
203
cat input.svg | svgo - > output.svg
204
205
# In shell pipelines
206
find . -name "*.svg" | xargs svgo
207
208
# With other tools
209
curl -s https://example.com/icon.svg | svgo - --datauri base64
210
```
211
212
### Advanced Examples
213
214
**Batch Processing with Custom Settings:**
215
216
```bash
217
# Optimize all SVGs with high precision and multipass
218
svgo -f ./assets/icons/ -r -p 3 --multipass -o ./dist/icons/
219
220
# Process with exclusions and pretty output
221
svgo -f ./src/ -r --exclude "node_modules" "temp.*" --pretty --indent 2 -o ./build/
222
```
223
224
**Development vs Production:**
225
226
```bash
227
# Development: pretty output, keep IDs
228
svgo input.svg --pretty --indent 2 --config ./dev-config.js
229
230
# Production: maximum optimization
231
svgo input.svg --multipass -p 2 --config ./prod-config.js
232
```
233
234
**Integration with Build Tools:**
235
236
```bash
237
# npm scripts in package.json
238
{
239
"scripts": {
240
"optimize-icons": "svgo -f src/icons/ -o dist/icons/",
241
"optimize-dev": "svgo -f src/icons/ --pretty --config dev.config.js -o dist/icons/",
242
"optimize-prod": "svgo -f src/icons/ --multipass --config prod.config.js -o dist/icons/"
243
}
244
}
245
246
# Usage
247
npm run optimize-icons
248
npm run optimize-prod
249
```
250
251
### Exit Codes
252
253
The CLI returns standard exit codes for scripting integration.
254
255
```bash { .api }
256
# Exit codes
257
0 # Success
258
1 # Error (invalid input, file not found, etc.)
259
260
# Example usage in scripts
261
if svgo input.svg -q; then
262
echo "Optimization successful"
263
else
264
echo "Optimization failed"
265
exit 1
266
fi
267
```
268
269
### Performance Tips
270
271
**For Large Batches:**
272
273
```bash
274
# Use folder mode instead of wildcards for better performance
275
svgo -f ./icons/ # Better
276
svgo ./icons/*.svg # Slower for many files
277
278
# Quiet mode reduces output overhead
279
svgo -f ./icons/ -q
280
281
# Avoid pretty printing for production
282
svgo -f ./icons/ --multipass # Fast
283
svgo -f ./icons/ --pretty # Slower
284
```
285
286
**Configuration Optimization:**
287
288
```bash
289
# Pre-create config file instead of inline options
290
svgo -f ./icons/ --config ./fast-config.js
291
292
# fast-config.js
293
export default {
294
plugins: [
295
// Only essential plugins for speed
296
'removeComments',
297
'removeEmptyAttrs',
298
'cleanupNumericValues'
299
]
300
};
301
```
302
303
### Error Handling
304
305
Common CLI errors and solutions.
306
307
```bash
308
# File not found
309
svgo nonexistent.svg # Error: file not found
310
311
# Invalid SVG
312
svgo invalid.svg # Shows parsing error with line/column
313
314
# Permission errors
315
svgo readonly.svg -o /protected/ # Error: permission denied
316
317
# Invalid options
318
svgo input.svg --invalid-option # Error: unknown option
319
320
# Check for errors in scripts
321
svgo input.svg || echo "Optimization failed"
322
```
323
324
### Integration Examples
325
326
**Makefile Integration:**
327
328
```makefile
329
# Makefile
330
optimize-icons:
331
svgo -f src/icons/ -o dist/icons/ --multipass -q
332
333
clean-icons:
334
rm -rf dist/icons/
335
336
.PHONY: optimize-icons clean-icons
337
```
338
339
**Shell Script Integration:**
340
341
```bash
342
#!/bin/bash
343
# optimize-assets.sh
344
345
echo "Optimizing SVG assets..."
346
347
# Create output directory
348
mkdir -p dist/assets/
349
350
# Optimize with error handling
351
if svgo -f src/assets/ -r -o dist/assets/ --multipass -q; then
352
echo "✅ SVG optimization completed"
353
354
# Count files
355
original=$(find src/assets/ -name "*.svg" | wc -l)
356
optimized=$(find dist/assets/ -name "*.svg" | wc -l)
357
echo "Processed $original files → $optimized files"
358
else
359
echo "❌ SVG optimization failed"
360
exit 1
361
fi
362
```
363
364
**Docker Integration:**
365
366
```dockerfile
367
# Dockerfile
368
FROM node:alpine
369
RUN npm install -g svgo
370
371
COPY assets/ /input/
372
RUN svgo -f /input/ -o /output/ --multipass -q
373
374
COPY --from=optimizer /output/ /app/assets/
375
```