@wwkit/shared 1.0.23 → 1.0.25

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wwkit/shared",
3
- "version": "1.0.23",
3
+ "version": "1.0.25",
4
4
  "author": "bluesliu <langcai163@163.com>",
5
5
  "description": "Shared utilities for @wwkit packages — config loader, platform detection",
6
6
  "type": "module",
package/src/args.js ADDED
@@ -0,0 +1,79 @@
1
+ /**
2
+ * 共享命令行参数解析器 — @wwkit/shared 统一参数解析
3
+ *
4
+ * 支持以下格式的参数:
5
+ * --flag value 长标志(空格分隔)
6
+ * --flag=value 长标志(等号形式)
7
+ * -f value 短标志(空格分隔)
8
+ * -f=value 短标志(等号形式)
9
+ * positional 位置参数(不以 - 开头)
10
+ *
11
+ * 布尔标志(见 DEFAULT_BOOLEAN_FLAGS 与 opts.booleans)不消费下一个参数:
12
+ * --json、-g、-y 等后跟普通 token 时,token 保持为位置参数/值参数,
13
+ * 避免 `opm npm install -g typescript` 将 typescript 吞为 -g 的值。
14
+ * 上下文相关的布尔标志(如 proc 的 -f/--force)由
15
+ * 调用方通过 opts.booleans 声明。
16
+ *
17
+ * @param {string[]} argv - 原始参数数组
18
+ * @param {{ booleans?: string[] }} [opts] - 上下文布尔标志名(不含 -/-- 前缀)
19
+ * @returns {{ flags: Record<string, string>, positional: string[] }}
20
+ */
21
+ export const DEFAULT_BOOLEAN_FLAGS = new Set([
22
+ 'h', 'help',
23
+ 'g', 'global',
24
+ 'y', 'yes',
25
+ 'a', 'all',
26
+ 'j', 'json',
27
+ 'exact',
28
+ 'dry-run',
29
+ 'backup',
30
+ 'delete',
31
+ ])
32
+
33
+ export function parseFlags(argv, opts = {}) {
34
+ const booleans = new Set([...DEFAULT_BOOLEAN_FLAGS, ...(opts.booleans || [])])
35
+ const flags = {}
36
+ const positional = []
37
+
38
+ for (let i = 0; i < argv.length; i++) {
39
+ const arg = argv[i]
40
+
41
+ if (arg.startsWith('--') && arg.includes('=')) {
42
+ const idx = arg.indexOf('=')
43
+ flags[arg.slice(2, idx)] = arg.slice(idx + 1)
44
+ } else if (arg.startsWith('--')) {
45
+ const name = arg.slice(2)
46
+ if (booleans.has(name)) {
47
+ flags[name] = 'true'
48
+ } else {
49
+ const next = argv[i + 1]
50
+ if (next !== undefined && !next.startsWith('-')) {
51
+ flags[name] = next
52
+ i++
53
+ } else {
54
+ flags[name] = 'true'
55
+ }
56
+ }
57
+ } else if (arg.startsWith('-') && arg.includes('=')) {
58
+ const idx = arg.indexOf('=')
59
+ flags[arg.slice(1, idx)] = arg.slice(idx + 1)
60
+ } else if (arg.startsWith('-') && arg.length > 1) {
61
+ const name = arg.slice(1)
62
+ if (booleans.has(name)) {
63
+ flags[name] = 'true'
64
+ } else {
65
+ const next = argv[i + 1]
66
+ if (next !== undefined && !next.startsWith('-')) {
67
+ flags[name] = next
68
+ i++
69
+ } else {
70
+ flags[name] = 'true'
71
+ }
72
+ }
73
+ } else {
74
+ positional.push(arg)
75
+ }
76
+ }
77
+
78
+ return { flags, positional }
79
+ }
package/src/index.js CHANGED
@@ -1,3 +1,4 @@
1
1
  export { createConfigLoader, getXdgConfigDir, deepMerge, resolveEnvVars, resolvePkgVars } from './config-loader.js'
2
2
  export { Shell } from './shell.js'
3
- export { parseProxy, openTunnel, fetchText, downloadToFile } from './http.js'
3
+ export { parseProxy, openTunnel, fetchText, downloadToFile } from './http.js'
4
+ export { parseFlags, DEFAULT_BOOLEAN_FLAGS } from './args.js'