0
# Command Line Interface
1
2
Shell interface for MIME type operations, supporting both type-to-extension and extension-to-type lookups with comprehensive flag support.
3
4
## Installation and Access
5
6
The `mime` command is automatically available after installing the package:
7
8
```bash
9
npm install mime
10
# or
11
npm install -g mime # for global access
12
```
13
14
## Capabilities
15
16
### Basic MIME Type Lookup
17
18
Get MIME type for a file path or extension.
19
20
```bash
21
mime [path_or_extension]
22
```
23
24
**Usage Examples:**
25
26
```bash
27
# File extensions
28
mime js # text/javascript
29
mime json # application/json
30
mime png # image/png
31
32
# File paths
33
mime script.js # text/javascript
34
mime data/config.json # application/json
35
mime images/photo.jpg # image/jpeg
36
mime package.tar.gz # application/gzip
37
38
# Complex paths
39
mime "path with spaces/file.txt" # text/plain
40
mime ./src/index.ts # text/typescript
41
```
42
43
### Reverse Lookup (Type to Extension)
44
45
Get default extension for a MIME type using the `--reverse` flag.
46
47
```bash
48
mime --reverse [mime_type]
49
mime -r [mime_type]
50
```
51
52
**Usage Examples:**
53
54
```bash
55
# Standard MIME types
56
mime -r text/plain # txt
57
mime -r application/json # json
58
mime -r image/jpeg # jpeg
59
mime -r text/javascript # js
60
61
# MIME types with parameters (automatically handled)
62
mime -r "text/html; charset=utf-8" # html
63
mime --reverse "application/json; charset=utf-8" # json
64
```
65
66
### Version Information
67
68
Display package version information.
69
70
```bash
71
mime --version
72
mime -v
73
mime --v
74
```
75
76
**Output Example:**
77
```
78
4.0.7
79
```
80
81
### Package Name
82
83
Display the package name.
84
85
```bash
86
mime --name
87
mime -n
88
mime --n
89
```
90
91
**Output Example:**
92
```
93
mime
94
```
95
96
### Help Information
97
98
Display comprehensive usage help.
99
100
```bash
101
mime --help
102
mime -h
103
mime --h
104
```
105
106
**Help Output:**
107
```
108
mime - A comprehensive library for mime-type mapping
109
110
Usage:
111
112
mime [flags] [path_or_extension]
113
114
Flags:
115
--help, -h Show this message
116
--version, -v Display the version
117
--name, -n Print the name of the program
118
--reverse, -r Print the extension of the mime type
119
120
Note: the command will exit after it executes if a command is specified
121
The path_or_extension is the path to the file or the extension of the file.
122
123
Examples:
124
mime --help
125
mime --version
126
mime --name
127
mime -v
128
mime --reverse application/text
129
mime src/log.js
130
mime new.py
131
mime foo.sh
132
```
133
134
## Exit Codes
135
136
The CLI uses standard exit codes for scripting integration:
137
138
```bash
139
# Success (type/extension found)
140
mime script.js # Exit code: 0, Output: text/javascript
141
mime -r text/plain # Exit code: 0, Output: txt
142
143
# Failure (type/extension not found)
144
mime unknown_extension # Exit code: 1, No output
145
mime -r unknown/mimetype # Exit code: 1, No output
146
147
# Info commands always succeed
148
mime --version # Exit code: 0
149
mime --help # Exit code: 0
150
```
151
152
## Scripting Examples
153
154
### Shell Script Integration
155
156
```bash
157
#!/bin/bash
158
159
# Check if file has expected MIME type
160
file="data.json"
161
expected="application/json"
162
actual=$(mime "$file")
163
164
if [ "$actual" = "$expected" ]; then
165
echo "File type correct: $actual"
166
else
167
echo "Unexpected file type: $actual (expected $expected)"
168
exit 1
169
fi
170
```
171
172
### Batch Processing
173
174
```bash
175
# Process all JavaScript files in a directory
176
for file in src/*.js; do
177
type=$(mime "$file")
178
echo "$file: $type"
179
done
180
181
# Find files by MIME type
182
find . -type f | while read file; do
183
if [ "$(mime "$file" 2>/dev/null)" = "text/javascript" ]; then
184
echo "JavaScript file: $file"
185
fi
186
done
187
```
188
189
### Conditional Processing
190
191
```bash
192
# Process file based on MIME type
193
file="input.data"
194
mime_type=$(mime "$file")
195
196
case "$mime_type" in
197
"application/json")
198
echo "Processing JSON file..."
199
jq . "$file"
200
;;
201
"text/plain")
202
echo "Processing text file..."
203
cat "$file"
204
;;
205
"image/"*)
206
echo "Processing image file..."
207
# image processing logic
208
;;
209
*)
210
echo "Unknown file type: $mime_type"
211
exit 1
212
;;
213
esac
214
```
215
216
### Validate File Extensions
217
218
```bash
219
# Check if file extension matches MIME type
220
check_file_consistency() {
221
local file="$1"
222
local mime_type=$(mime "$file")
223
local expected_ext=$(mime -r "$mime_type")
224
local actual_ext="${file##*.}"
225
226
if [ "$actual_ext" = "$expected_ext" ]; then
227
echo "✓ $file: consistent ($mime_type)"
228
else
229
echo "✗ $file: extension '$actual_ext' doesn't match type '$mime_type' (expected '$expected_ext')"
230
fi
231
}
232
233
# Usage
234
check_file_consistency "script.js" # ✓ script.js: consistent (text/javascript)
235
check_file_consistency "data.js" # ✗ data.js: extension 'js' doesn't match type 'application/json' (expected 'json')
236
```
237
238
## Error Handling
239
240
### Invalid Arguments
241
242
```bash
243
# No arguments (tries to get type of empty string)
244
mime # Exit code: 1, No output
245
246
# Invalid file paths (no extension detected)
247
mime "file_without_extension" # Exit code: 1, No output
248
mime "path/to/directory/" # Exit code: 1, No output
249
```
250
251
### Reverse Lookup Errors
252
253
```bash
254
# Invalid MIME type
255
mime -r "not-a-mime-type" # Exit code: 1, No output
256
mime -r "" # Exit code: 1, No output
257
258
# Missing argument for reverse lookup
259
mime -r # Uses last argument as MIME type
260
```
261
262
### Silent Failure Mode
263
264
The CLI fails silently (no error messages) to facilitate clean scripting:
265
266
```bash
267
# Check if command succeeded without capturing output
268
if mime "unknown.ext" >/dev/null 2>&1; then
269
echo "MIME type found"
270
else
271
echo "MIME type not found"
272
fi
273
```
274
275
## Implementation Details
276
277
The CLI is implemented as a thin wrapper around the main MIME library:
278
279
- **Binary**: `bin/cli.js`
280
- **Implementation**: `src/mime_cli.ts`
281
- **Runtime**: Node.js with ES modules
282
- **Dependencies**: Only the core `mime` library
283
284
The CLI processes arguments sequentially and exits immediately after executing any flag command, making it suitable for both interactive use and shell scripting.