@aronnaxlin/bgm-cli 1.0.0 → 1.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/utils/args.js CHANGED
@@ -2,13 +2,18 @@
2
2
  * CLI argument parsing utilities.
3
3
  */
4
4
 
5
+ import { CommandError } from "../core/output.js";
6
+
5
7
  export function parseGlobalArgs(argv) {
6
8
  const args = [];
7
9
  let json = false;
8
10
  let init = false;
9
11
  let version = false;
12
+ let profile;
13
+ let url;
10
14
 
11
- for (const arg of argv) {
15
+ for (let index = 0; index < argv.length; index += 1) {
16
+ const arg = argv[index];
12
17
  if (arg === "--json") {
13
18
  json = true;
14
19
  continue;
@@ -21,10 +26,44 @@ export function parseGlobalArgs(argv) {
21
26
  version = true;
22
27
  continue;
23
28
  }
29
+ if (arg === "--profile") {
30
+ const next = argv[index + 1];
31
+ if (next === undefined || next.startsWith("--")) {
32
+ throw new CommandError("Usage: bgm --profile <name> <command> ...");
33
+ }
34
+ profile = next;
35
+ index += 1;
36
+ continue;
37
+ }
38
+ if (arg === "--url" || arg === "-url") {
39
+ const next = argv[index + 1];
40
+ if (next === undefined || next.startsWith("-")) {
41
+ throw new CommandError("Usage: bgm --url <bangumi_url> [--dry-run]");
42
+ }
43
+ url = next;
44
+ index += 1;
45
+ continue;
46
+ }
47
+ if (arg.startsWith("--url=") || arg.startsWith("-url=")) {
48
+ const value = arg.slice(arg.indexOf("=") + 1);
49
+ if (value === "") {
50
+ throw new CommandError("Usage: bgm --url <bangumi_url> [--dry-run]");
51
+ }
52
+ url = value;
53
+ continue;
54
+ }
55
+ if (arg.startsWith("--profile=")) {
56
+ const value = arg.slice("--profile=".length);
57
+ if (value === "") {
58
+ throw new CommandError("Usage: bgm --profile <name> <command> ...");
59
+ }
60
+ profile = value;
61
+ continue;
62
+ }
24
63
  args.push(arg);
25
64
  }
26
65
 
27
- return { args, json, init, version };
66
+ return { args, json, init, version, profile, url };
28
67
  }
29
68
 
30
69
  export function parseFlags(args) {
@@ -0,0 +1,650 @@
1
+ /**
2
+ * Bangumi URL parsing and command resolution.
3
+ *
4
+ * Turns a pasted bgm.tv / bangumi.tv / chii.in / next.bgm.tv / api.bgm.tv link
5
+ * into the bgm CLI command that shows the same resource. Resolution is pure and
6
+ * offline: no request is made while parsing.
7
+ */
8
+
9
+ import { CommandError } from "../core/output.js";
10
+
11
+ export const WEB_HOSTS = new Set(["bgm.tv", "bangumi.tv", "chii.in", "next.bgm.tv"]);
12
+ export const API_HOSTS = new Set(["api.bgm.tv"]);
13
+
14
+ /** Page size assumed when a URL carries ?page=n and no explicit limit. */
15
+ export const URL_PAGE_SIZE = 20;
16
+
17
+ const SUBJECT_TYPE_SLUGS = new Set(["book", "anime", "music", "game", "real"]);
18
+
19
+ const SUBJECT_SEARCH_CATEGORIES = {
20
+ 1: "book",
21
+ 2: "anime",
22
+ 3: "music",
23
+ 4: "game",
24
+ 6: "real",
25
+ };
26
+
27
+ /** Old-site collection status path segments -> CLI --status values. */
28
+ const COLLECTION_STATUS_SLUGS = {
29
+ wish: "wish",
30
+ collect: "collect",
31
+ do: "doing",
32
+ on_hold: "on_hold",
33
+ dropped: "dropped",
34
+ };
35
+
36
+ const SUBJECT_SUBPAGES = {
37
+ comments: ["subject", "comments"],
38
+ reviews: ["subject", "reviews"],
39
+ board: ["subject", "topics"],
40
+ characters: ["subject", "characters"],
41
+ persons: ["subject", "staff"],
42
+ collections: ["subject", "collects"],
43
+ index: ["subject", "indexes"],
44
+ ep: ["episode", "list"],
45
+ };
46
+
47
+ const CHARACTER_SUBPAGES = {
48
+ collections: "collects",
49
+ indices: "indexes",
50
+ album: "photos",
51
+ };
52
+
53
+ const PERSON_SUBPAGES = {
54
+ collections: "collects",
55
+ indices: "indexes",
56
+ album: "photos",
57
+ works: "works",
58
+ collabs: "relations",
59
+ };
60
+
61
+ const SUBJECT_LIST_SORTS = new Set(["date", "rank"]);
62
+ const SUBJECT_SEARCH_SORTS = new Set(["match", "heat", "rank", "score"]);
63
+ const COLLECTION_SORTS = new Set(["updated", "name", "rank", "community_score", "user_score", "date"]);
64
+
65
+ /**
66
+ * Parse a possible Bangumi URL. Returns null when the value is not a URL on a
67
+ * supported host, so callers can fall back to normal command parsing.
68
+ */
69
+ export function parseBangumiUrl(value) {
70
+ if (typeof value !== "string") {
71
+ return null;
72
+ }
73
+
74
+ const trimmed = value.trim();
75
+ if (trimmed === "" || /\s/.test(trimmed)) {
76
+ return null;
77
+ }
78
+
79
+ const withScheme = /^https?:\/\//i.test(trimmed) ? trimmed : `https://${trimmed}`;
80
+ let parsed;
81
+ try {
82
+ parsed = new URL(withScheme);
83
+ } catch {
84
+ return null;
85
+ }
86
+
87
+ if (parsed.protocol !== "http:" && parsed.protocol !== "https:") {
88
+ return null;
89
+ }
90
+
91
+ const host = parsed.hostname.toLowerCase().replace(/^www\./, "");
92
+ if (!WEB_HOSTS.has(host) && !API_HOSTS.has(host)) {
93
+ return null;
94
+ }
95
+
96
+ const segments = parsed.pathname
97
+ .split("/")
98
+ .filter(Boolean)
99
+ .map(decodeSegment);
100
+
101
+ const query = {};
102
+ for (const [key, entry] of parsed.searchParams.entries()) {
103
+ if (query[key] === undefined) {
104
+ query[key] = entry;
105
+ }
106
+ }
107
+
108
+ return {
109
+ url: trimmed,
110
+ host,
111
+ kind: API_HOSTS.has(host) ? "api" : "web",
112
+ path: `/${segments.join("/")}`,
113
+ segments,
114
+ query,
115
+ hash: parsed.hash.replace(/^#/, ""),
116
+ };
117
+ }
118
+
119
+ export function looksLikeBangumiUrl(value) {
120
+ return parseBangumiUrl(value) !== null;
121
+ }
122
+
123
+ /**
124
+ * Resolve a Bangumi URL into { group, command, args }.
125
+ * Throws a CommandError (with a `bgm ...` suggestion when one can be derived)
126
+ * for supported hosts whose path has no CLI equivalent.
127
+ */
128
+ export function resolveBangumiUrl(value) {
129
+ const parsed = parseBangumiUrl(value);
130
+ if (!parsed) {
131
+ throw new CommandError(
132
+ `Not a supported Bangumi URL: ${value}\nSupported hosts: ${[...WEB_HOSTS, ...API_HOSTS].join(", ")}`,
133
+ );
134
+ }
135
+
136
+ const resolved = parsed.kind === "api" ? resolveApiPath(parsed) : resolveWebPath(parsed);
137
+ return {
138
+ ...resolved,
139
+ url: parsed.url,
140
+ site: parsed.host,
141
+ };
142
+ }
143
+
144
+ export function formatResolvedCommand(resolved) {
145
+ return ["bgm", resolved.group, resolved.command, ...resolved.args]
146
+ .filter((entry) => entry !== undefined && entry !== "")
147
+ .map(quoteArg)
148
+ .join(" ");
149
+ }
150
+
151
+ function resolveWebPath(parsed) {
152
+ const { segments } = parsed;
153
+ if (segments.length === 0) {
154
+ throw unsupported(parsed, "The site homepage has no single CLI equivalent.");
155
+ }
156
+
157
+ switch (segments[0]) {
158
+ case "subject":
159
+ return resolveSubject(parsed);
160
+ case "group":
161
+ return resolveGroup(parsed);
162
+ case "ep":
163
+ return command("episode", "get", [requireId(segments[1], "episode id", parsed)], parsed);
164
+ case "character":
165
+ return resolveMono(parsed, "character", CHARACTER_SUBPAGES);
166
+ case "person":
167
+ return resolveMono(parsed, "person", PERSON_SUBPAGES);
168
+ case "user":
169
+ return resolveUser(parsed);
170
+ case "blog":
171
+ return resolveBlog(parsed);
172
+ case "index":
173
+ return resolveIndex(parsed);
174
+ case "subject_search":
175
+ return resolveSubjectSearch(parsed);
176
+ case "mono_search":
177
+ return resolveMonoSearch(parsed);
178
+ case "calendar":
179
+ return command("calendar", "all", [], parsed);
180
+ case "timeline":
181
+ return command("timeline", "list", pagingArgs(parsed, { untilKey: "until" }), parsed);
182
+ case "notify":
183
+ return command("notify", "list", pagingArgs(parsed, { limitOnly: true }), parsed);
184
+ case "rakuen":
185
+ return resolveRakuen(parsed);
186
+ default:
187
+ if (SUBJECT_TYPE_SLUGS.has(segments[0])) {
188
+ return resolveTypeSection(parsed);
189
+ }
190
+ throw unsupported(parsed);
191
+ }
192
+ }
193
+
194
+ function resolveSubject(parsed) {
195
+ const { segments } = parsed;
196
+
197
+ if (segments[1] === "topic") {
198
+ const topicId = requireId(segments[2], "topic id", parsed);
199
+ const postId = postIdFromHash(parsed.hash);
200
+ if (postId) {
201
+ return command("subject", "post", [postId], parsed);
202
+ }
203
+ return command("subject", "topic", [topicId], parsed);
204
+ }
205
+
206
+ const subjectId = requireId(segments[1], "subject id", parsed);
207
+ const sub = segments[2];
208
+
209
+ if (sub === undefined) {
210
+ return command("subject", "get", [subjectId], parsed);
211
+ }
212
+
213
+ const mapped = SUBJECT_SUBPAGES[sub];
214
+ if (!mapped || segments.length > 3) {
215
+ throw unsupported(parsed, undefined, `bgm subject get ${subjectId}`);
216
+ }
217
+
218
+ const [group, name] = mapped;
219
+ const args = [subjectId, ...pagingArgs(parsed)];
220
+ if (group === "subject" && name === "comments") {
221
+ const type = collectionStatusFromQuery(parsed.query.type ?? parsed.query.filter);
222
+ if (type) {
223
+ args.push("--type", type);
224
+ }
225
+ }
226
+ return command(group, name, args, parsed);
227
+ }
228
+
229
+ function resolveGroup(parsed) {
230
+ const { segments } = parsed;
231
+
232
+ if (segments[1] === "topic") {
233
+ const topicId = requireId(segments[2], "topic id", parsed);
234
+ const postId = postIdFromHash(parsed.hash);
235
+ if (postId) {
236
+ return command("group", "post", [postId], parsed);
237
+ }
238
+ return command("group", "topic", [topicId], parsed);
239
+ }
240
+
241
+ if (segments[1] === "category") {
242
+ const args = pagingArgs(parsed);
243
+ if (segments[2] === "all" || segments[2] === undefined) {
244
+ args.unshift("--mode", "all");
245
+ }
246
+ return command("group", "list", args, parsed);
247
+ }
248
+
249
+ const name = requireSlug(segments[1], "group name", parsed);
250
+ const sub = segments[2];
251
+
252
+ if (sub === undefined) {
253
+ return command("group", "get", [name], parsed);
254
+ }
255
+ if (sub === "forum" && segments.length === 3) {
256
+ return command("group", "topics", [name, ...pagingArgs(parsed)], parsed);
257
+ }
258
+ if (sub === "members" && segments.length === 3) {
259
+ const args = [name, ...pagingArgs(parsed)];
260
+ if (typeof parsed.query.role === "string" && parsed.query.role !== "") {
261
+ args.push("--role", parsed.query.role);
262
+ }
263
+ return command("group", "members", args, parsed);
264
+ }
265
+
266
+ throw unsupported(parsed, undefined, `bgm group get ${name}`);
267
+ }
268
+
269
+ function resolveMono(parsed, kind, subpages) {
270
+ const { segments } = parsed;
271
+ const id = requireId(segments[1], `${kind} id`, parsed);
272
+ const sub = segments[2];
273
+
274
+ if (sub === undefined) {
275
+ return command(kind, "get", [id], parsed);
276
+ }
277
+
278
+ const mapped = subpages[sub];
279
+ if (!mapped || (segments.length > 3 && sub !== "works")) {
280
+ throw unsupported(parsed, undefined, `bgm ${kind} get ${id}`);
281
+ }
282
+
283
+ return command(kind, mapped, [id, ...pagingArgs(parsed)], parsed);
284
+ }
285
+
286
+ function resolveUser(parsed) {
287
+ const { segments } = parsed;
288
+ const username = requireSlug(segments[1], "username", parsed);
289
+ const sub = segments[2];
290
+
291
+ if (sub === undefined) {
292
+ return command("user", "get", [username], parsed);
293
+ }
294
+
295
+ if (segments.length === 3) {
296
+ switch (sub) {
297
+ case "timeline":
298
+ return command("timeline", "user", [username, ...pagingArgs(parsed, { untilKey: "until" })], parsed);
299
+ case "blog":
300
+ return command("blog", "list", ["--user", username, ...pagingArgs(parsed)], parsed);
301
+ case "index":
302
+ return command("index", "user", [username, ...pagingArgs(parsed)], parsed);
303
+ case "friends":
304
+ return command("user", "friends", [username, ...pagingArgs(parsed)], parsed);
305
+ case "followers":
306
+ return command("user", "followers", [username, ...pagingArgs(parsed)], parsed);
307
+ case "groups":
308
+ return command("group", "user", [username, ...pagingArgs(parsed)], parsed);
309
+ case "mono":
310
+ throw unsupported(
311
+ parsed,
312
+ "This page mixes character and person collections.",
313
+ `bgm collection characters --user ${username}\n bgm collection persons --user ${username}`,
314
+ );
315
+ default:
316
+ break;
317
+ }
318
+ }
319
+
320
+ if (sub === "mono" && segments.length === 4) {
321
+ if (segments[3] === "character") {
322
+ return command("collection", "characters", ["--user", username, ...pagingArgs(parsed)], parsed);
323
+ }
324
+ if (segments[3] === "person") {
325
+ return command("collection", "persons", ["--user", username, ...pagingArgs(parsed)], parsed);
326
+ }
327
+ }
328
+
329
+ throw unsupported(parsed, undefined, `bgm user get ${username}`);
330
+ }
331
+
332
+ function resolveBlog(parsed) {
333
+ const { segments } = parsed;
334
+ const blogId = requireId(segments[1], "blog id", parsed);
335
+
336
+ if (segments.length === 2) {
337
+ return command("blog", "get", [blogId], parsed);
338
+ }
339
+ if (segments[2] === "photos" && segments.length === 3) {
340
+ return command("blog", "photos", [blogId, ...pagingArgs(parsed)], parsed);
341
+ }
342
+
343
+ throw unsupported(parsed, undefined, `bgm blog get ${blogId}`);
344
+ }
345
+
346
+ function resolveIndex(parsed) {
347
+ const { segments } = parsed;
348
+ const indexId = requireId(segments[1], "index id", parsed);
349
+
350
+ if (segments.length === 2) {
351
+ return command("index", "get", [indexId], parsed);
352
+ }
353
+ if (segments[2] === "comments" && segments.length === 3) {
354
+ return command("index", "comments", [indexId], parsed);
355
+ }
356
+
357
+ throw unsupported(parsed, undefined, `bgm index get ${indexId}`);
358
+ }
359
+
360
+ function resolveSubjectSearch(parsed) {
361
+ const keyword = requireKeyword(parsed.segments[1], parsed);
362
+ const args = [keyword];
363
+
364
+ const type = SUBJECT_SEARCH_CATEGORIES[Number(parsed.query.cat)];
365
+ if (type) {
366
+ args.push("--type", type);
367
+ }
368
+ if (SUBJECT_SEARCH_SORTS.has(parsed.query.sort)) {
369
+ args.push("--sort", parsed.query.sort);
370
+ }
371
+
372
+ return command("subject", "search", args, parsed);
373
+ }
374
+
375
+ function resolveMonoSearch(parsed) {
376
+ const keyword = requireKeyword(parsed.segments[1], parsed);
377
+ const cat = Number(parsed.query.cat);
378
+ const group = cat === 2 ? "person" : "character";
379
+ return command(group, "search", [keyword, ...pagingArgs(parsed)], parsed);
380
+ }
381
+
382
+ function resolveTypeSection(parsed) {
383
+ const { segments } = parsed;
384
+ const type = segments[0];
385
+ const section = segments[1];
386
+
387
+ if (section === "list") {
388
+ const username = requireSlug(segments[2], "username", parsed);
389
+ const args = ["--user", username, "--type", type];
390
+ const status = COLLECTION_STATUS_SLUGS[segments[3] ?? ""];
391
+ if (segments[3] !== undefined && !status) {
392
+ throw unsupported(parsed, undefined, `bgm collection list --user ${username} --type ${type}`);
393
+ }
394
+ if (status) {
395
+ args.push("--status", status);
396
+ }
397
+ if (typeof parsed.query.tag === "string" && parsed.query.tag !== "") {
398
+ args.push("--tag", parsed.query.tag);
399
+ }
400
+ if (COLLECTION_SORTS.has(parsed.query.orderby)) {
401
+ args.push("--sort", parsed.query.orderby);
402
+ }
403
+ args.push(...pagingArgs(parsed));
404
+ return command("collection", "list", args, parsed);
405
+ }
406
+
407
+ if (section === "browser" && segments.length === 2) {
408
+ const args = ["--type", type];
409
+ if (SUBJECT_LIST_SORTS.has(parsed.query.sort)) {
410
+ args.push("--sort", parsed.query.sort);
411
+ }
412
+ args.push(...pagingArgs(parsed, { limitOnly: true }));
413
+ return command("subject", "list", args, parsed);
414
+ }
415
+
416
+ if (section === "tag" && segments.length === 3) {
417
+ const args = ["--type", type, "--tag", segments[2]];
418
+ if (SUBJECT_LIST_SORTS.has(parsed.query.sort)) {
419
+ args.push("--sort", parsed.query.sort);
420
+ }
421
+ args.push(...pagingArgs(parsed, { limitOnly: true }));
422
+ return command("subject", "list", args, parsed);
423
+ }
424
+
425
+ throw unsupported(parsed, undefined, `bgm subject list --type ${type}`);
426
+ }
427
+
428
+ function resolveRakuen(parsed) {
429
+ const { segments } = parsed;
430
+ if (segments[1] === "topic" && segments[3] !== undefined) {
431
+ const id = String(segments[3]);
432
+ if (/^\d+$/.test(id) && (segments[2] === "group" || segments[2] === "subject")) {
433
+ throw unsupported(parsed, undefined, `bgm ${segments[2]} topic ${id}`);
434
+ }
435
+ }
436
+ throw unsupported(parsed);
437
+ }
438
+
439
+ function resolveApiPath(parsed) {
440
+ const { segments } = parsed;
441
+
442
+ if (segments[0] === "calendar") {
443
+ return command("calendar", "all", [], parsed);
444
+ }
445
+ if (segments[0] !== "v0") {
446
+ throw unsupported(parsed);
447
+ }
448
+
449
+ const resource = segments[1];
450
+ const id = segments[2];
451
+ const sub = segments[3];
452
+
453
+ switch (resource) {
454
+ case "me":
455
+ return command("user", "me", [], parsed);
456
+ case "subjects": {
457
+ const subjectId = requireId(id, "subject id", parsed);
458
+ if (sub === undefined) {
459
+ return command("subject", "get", [subjectId], parsed);
460
+ }
461
+ if (sub === "persons") {
462
+ return command("subject", "staff", [subjectId, ...pagingArgs(parsed)], parsed);
463
+ }
464
+ if (sub === "characters") {
465
+ return command("subject", "characters", [subjectId, ...pagingArgs(parsed)], parsed);
466
+ }
467
+ if (sub === "subjects") {
468
+ return command("subject", "relations", [subjectId, ...pagingArgs(parsed)], parsed);
469
+ }
470
+ throw unsupported(parsed, undefined, `bgm subject get ${subjectId}`);
471
+ }
472
+ case "episodes": {
473
+ if (id === undefined) {
474
+ const subjectId = requireId(parsed.query.subject_id, "subject id", parsed);
475
+ const args = [subjectId, ...pagingArgs(parsed)];
476
+ return command("episode", "list", args, parsed);
477
+ }
478
+ return command("episode", "get", [requireId(id, "episode id", parsed)], parsed);
479
+ }
480
+ case "characters": {
481
+ const characterId = requireId(id, "character id", parsed);
482
+ if (sub === undefined) {
483
+ return command("character", "get", [characterId], parsed);
484
+ }
485
+ if (sub === "subjects" || sub === "persons") {
486
+ return command("character", "casts", [characterId, ...pagingArgs(parsed)], parsed);
487
+ }
488
+ throw unsupported(parsed, undefined, `bgm character get ${characterId}`);
489
+ }
490
+ case "persons": {
491
+ const personId = requireId(id, "person id", parsed);
492
+ if (sub === undefined) {
493
+ return command("person", "get", [personId], parsed);
494
+ }
495
+ if (sub === "subjects") {
496
+ return command("person", "works", [personId, ...pagingArgs(parsed)], parsed);
497
+ }
498
+ if (sub === "characters") {
499
+ return command("person", "casts", [personId, ...pagingArgs(parsed)], parsed);
500
+ }
501
+ throw unsupported(parsed, undefined, `bgm person get ${personId}`);
502
+ }
503
+ case "users": {
504
+ const username = requireSlug(id, "username", parsed);
505
+ if (sub === undefined) {
506
+ return command("user", "get", [username], parsed);
507
+ }
508
+ if (sub === "collections") {
509
+ const args = ["--user", username];
510
+ const type = SUBJECT_TYPE_SLUGS.has(parsed.query.subject_type)
511
+ ? parsed.query.subject_type
512
+ : SUBJECT_SEARCH_CATEGORIES[Number(parsed.query.subject_type)];
513
+ if (type) {
514
+ args.push("--type", type);
515
+ }
516
+ args.push(...pagingArgs(parsed));
517
+ return command("collection", "list", args, parsed);
518
+ }
519
+ throw unsupported(parsed, undefined, `bgm user get ${username}`);
520
+ }
521
+ case "indices": {
522
+ const indexId = requireId(id, "index id", parsed);
523
+ if (sub === undefined) {
524
+ return command("index", "get", [indexId], parsed);
525
+ }
526
+ if (sub === "subjects") {
527
+ return command("index", "related", [indexId, "--cat", "subject", ...pagingArgs(parsed)], parsed);
528
+ }
529
+ throw unsupported(parsed, undefined, `bgm index get ${indexId}`);
530
+ }
531
+ default:
532
+ throw unsupported(parsed);
533
+ }
534
+ }
535
+
536
+ function command(group, name, args, parsed) {
537
+ return {
538
+ group,
539
+ command: name,
540
+ args: args.map(String),
541
+ path: parsed.path,
542
+ };
543
+ }
544
+
545
+ /**
546
+ * Translate ?page= / ?limit= / ?offset= into CLI paging flags.
547
+ * A page beyond the first also pins --limit so the offset stays meaningful.
548
+ */
549
+ function pagingArgs(parsed, { limitOnly = false, untilKey } = {}) {
550
+ const args = [];
551
+ const explicitLimit = positiveIntOrNull(parsed.query.limit);
552
+ const page = positiveIntOrNull(parsed.query.page);
553
+ const offset = nonNegativeIntOrNull(parsed.query.offset);
554
+ const limit = explicitLimit ?? (page !== null && page > 1 ? URL_PAGE_SIZE : null);
555
+
556
+ if (limit !== null) {
557
+ args.push("--limit", String(limit));
558
+ }
559
+
560
+ if (untilKey && typeof parsed.query[untilKey] === "string" && /^\d+$/.test(parsed.query[untilKey])) {
561
+ args.push("--until", parsed.query[untilKey]);
562
+ return args;
563
+ }
564
+
565
+ if (limitOnly) {
566
+ return args;
567
+ }
568
+
569
+ if (offset !== null) {
570
+ args.push("--offset", String(offset));
571
+ } else if (page !== null && page > 1) {
572
+ args.push("--offset", String((page - 1) * (limit ?? URL_PAGE_SIZE)));
573
+ }
574
+
575
+ return args;
576
+ }
577
+
578
+ function collectionStatusFromQuery(value) {
579
+ if (typeof value !== "string" || value === "") {
580
+ return undefined;
581
+ }
582
+ return COLLECTION_STATUS_SLUGS[value.toLowerCase()];
583
+ }
584
+
585
+ function postIdFromHash(hash) {
586
+ const match = /^post_(\d+)$/.exec(String(hash ?? ""));
587
+ return match ? match[1] : undefined;
588
+ }
589
+
590
+ function decodeSegment(segment) {
591
+ try {
592
+ return decodeURIComponent(segment);
593
+ } catch {
594
+ return segment;
595
+ }
596
+ }
597
+
598
+ function positiveIntOrNull(value) {
599
+ if (typeof value !== "string" || !/^\d+$/.test(value)) {
600
+ return null;
601
+ }
602
+ const parsed = Number(value);
603
+ return parsed > 0 ? parsed : null;
604
+ }
605
+
606
+ function nonNegativeIntOrNull(value) {
607
+ if (typeof value !== "string" || !/^\d+$/.test(value)) {
608
+ return null;
609
+ }
610
+ return Number(value);
611
+ }
612
+
613
+ function requireId(value, label, parsed) {
614
+ if (typeof value !== "string" || !/^\d+$/.test(value)) {
615
+ throw unsupported(parsed, `Expected a numeric ${label}.`);
616
+ }
617
+ return value;
618
+ }
619
+
620
+ function requireSlug(value, label, parsed) {
621
+ if (typeof value !== "string" || !/^[A-Za-z0-9_-]+$/.test(value)) {
622
+ throw unsupported(parsed, `Expected a valid ${label}.`);
623
+ }
624
+ return value;
625
+ }
626
+
627
+ function requireKeyword(value, parsed) {
628
+ if (typeof value !== "string" || value === "") {
629
+ throw unsupported(parsed, "Expected a search keyword in the URL path.");
630
+ }
631
+ return value;
632
+ }
633
+
634
+ function unsupported(parsed, reason, suggestion) {
635
+ const lines = [`Unsupported Bangumi URL path: ${parsed.path || "/"}`];
636
+ if (reason) {
637
+ lines.push(reason);
638
+ }
639
+ if (suggestion) {
640
+ lines.push(`Did you mean: ${suggestion}`);
641
+ } else {
642
+ lines.push("Run `bgm url --help` to see the supported link shapes.");
643
+ }
644
+ return new CommandError(lines.join("\n"));
645
+ }
646
+
647
+ function quoteArg(value) {
648
+ const text = String(value);
649
+ return /[\s"'#]/.test(text) ? JSON.stringify(text) : text;
650
+ }