-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.ts
More file actions
233 lines (201 loc) · 5.68 KB
/
main.ts
File metadata and controls
233 lines (201 loc) · 5.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
import { search } from "./commands/search.ts";
import { show } from "./commands/show.ts";
import { groups } from "./commands/groups.ts";
import { create } from "./commands/create.ts";
import { update } from "./commands/update.ts";
import { me } from "./commands/me.ts";
export interface Flags {
[key: string]: string;
}
export interface ParsedArgs {
flags: Flags;
positional: string[];
}
const booleanFlags = new Set(["json", "help", "h"]);
/** Check if a token looks like a flag (--long or -X single letter). */
function isFlag(token: string): boolean {
if (token.startsWith("--")) return true;
return token.startsWith("-") && token.length === 2;
}
export function parseArgs(args: string[]): ParsedArgs {
const flags: Flags = {};
const positional: string[] = [];
for (let i = 0; i < args.length; i++) {
const arg = args[i];
if (arg === "--") {
positional.push(...args.slice(i + 1));
break;
}
if (arg.startsWith("--")) {
const key = arg.slice(2);
if (booleanFlags.has(key)) {
flags[key] = "true";
} else if (i + 1 < args.length && !isFlag(args[i + 1])) {
flags[key] = args[++i];
} else {
die(`flag --${key} requires a value`);
}
} else if (arg.startsWith("-") && arg.length === 2) {
const key = arg.slice(1);
if (booleanFlags.has(key)) {
flags[key] = "true";
} else if (i + 1 < args.length && !isFlag(args[i + 1])) {
flags[key] = args[++i];
} else {
die(`flag -${key} requires a value`);
}
} else {
positional.push(arg);
}
}
return { flags, positional };
}
export function die(msg: string): never {
console.error(`nanocontacts: ${msg}`);
Deno.exit(1);
}
export function parseIntFlag(
value: string,
name: string,
_fallback: number,
): number {
const n = parseInt(value, 10);
if (isNaN(n)) die(`--${name} must be a number, got: ${value}`);
return n;
}
export function parseLimit(flags: Flags, fallback: number): number {
return Math.max(
1,
parseIntFlag(flags.limit || String(fallback), "limit", fallback),
);
}
const validCommands = new Set([
"search",
"show",
"me",
"groups",
"create",
"update",
]);
function printUsage(): void {
console.log(`Usage: nanocontacts <command> [options]
Commands:
search Search contacts by name or organization
show Show full contact details
me Show your own contact card
groups List contact groups
create Create a new contact
update Update an existing contact
help Show help for a command
Global options:
--json Output JSON instead of human-readable text
Run 'nanocontacts help <command>' for details.`);
}
function printCommandHelp(command: string): void {
switch (command) {
case "search":
console.log(`Usage: nanocontacts search <query> [options]
Search contacts by name or organization (case-insensitive).
To search by email or phone, pipe JSON output through grep:
nanocontacts search "" --json | grep "phone-number"
Options:
--limit <n> Max results (default: 20)
--json Output JSON`);
break;
case "show":
console.log(`Usage: nanocontacts show --id <id> [--json]
Show full contact details including emails, phones, addresses, and URLs.
Options:
--id <id> Contact ID (required)
--json Output JSON`);
break;
case "me":
console.log(`Usage: nanocontacts me [--json]
Show your own contact card (the "My Card" in Contacts.app).`);
break;
case "groups":
console.log(`Usage: nanocontacts groups [--json]
List all contact groups.`);
break;
case "create":
console.log(`Usage: nanocontacts create --first <name> --last <name> [options]
Create a new contact. At least --first or --last is required.
Options:
--first <name> First name
--last <name> Last name
--org <name> Organization
--title <title> Job title
--email <addr> Email address
--phone <number> Phone number
--note <text> Note
--json Output JSON`);
break;
case "update":
console.log(`Usage: nanocontacts update --id <id> [options]
Update an existing contact.
Options:
--id <id> Contact ID (required)
--first <name> Set first name
--last <name> Set last name
--org <name> Set organization
--title <title> Set job title
--note <text> Set note
--add-email <addr> Add email address
--add-phone <number> Add phone number
--json Output JSON`);
break;
default:
die(`unknown command: ${command}`);
}
}
async function main() {
const [command, ...rest] = Deno.args;
if (!command || command === "--help" || command === "-h") {
printUsage();
Deno.exit(0);
}
if (command === "help") {
const sub = rest[0];
if (sub) {
printCommandHelp(sub);
} else {
printUsage();
}
Deno.exit(0);
}
if (!validCommands.has(command)) {
die(`unknown command: ${command}`);
}
const { flags, positional } = parseArgs(rest);
const json = flags.json === "true";
if (flags.help === "true" || flags.h === "true") {
printCommandHelp(command);
Deno.exit(0);
}
try {
switch (command) {
case "search":
await search(flags, positional, json);
break;
case "show":
await show(flags, json);
break;
case "me":
await me(json);
break;
case "groups":
await groups(json);
break;
case "create":
await create(flags, json);
break;
case "update":
await update(flags, json);
break;
}
} catch (err: unknown) {
const msg = err instanceof Error ? err.message : String(err);
die(msg);
}
}
main();