@lppx/nlearn 1.1.0
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/dist/scripts/sync-repo.js +100 -0
- package/dist/src/cli/cli.js +9 -0
- package/dist/src/demo/commander/01-/345/237/272/347/241/200/346/246/202/345/277/265.js +174 -0
- package/dist/src/demo/commander/02-/345/221/275/344/273/244/347/263/273/347/273/237.js +260 -0
- package/dist/src/demo/commander/03-/351/253/230/347/272/247/347/211/271/346/200/247.js +285 -0
- package/dist/src/demo/commander/04-/345/256/236/346/210/230/346/241/210/344/276/213.js +408 -0
- package/dist/src/demo/esm/01-/345/237/272/347/241/200/346/246/202/345/277/265.js +226 -0
- package/dist/src/demo/esm/02-/345/257/274/345/207/272/350/257/255/346/263/225.js +281 -0
- package/dist/src/demo/esm/03-/345/257/274/345/205/245/350/257/255/346/263/225.js +366 -0
- package/dist/src/demo/esm/04-/345/256/236/346/210/230/346/241/210/344/276/213.js +509 -0
- package/dist/src/demo/inquirer/01-/345/237/272/347/241/200/346/246/202/345/277/265.js +135 -0
- package/dist/src/demo/inquirer/02-/351/200/211/346/213/251/347/261/273/345/236/213.js +143 -0
- package/dist/src/demo/inquirer/03-/351/253/230/347/272/247/347/211/271/346/200/247.js +211 -0
- package/dist/src/demo/inquirer/04-/345/256/236/346/210/230/346/241/210/344/276/213.js +343 -0
- package/dist/src/demo/yargs/01-/345/237/272/347/241/200/346/246/202/345/277/265.js +142 -0
- package/dist/src/demo/yargs/02-/345/221/275/344/273/244/347/263/273/347/273/237.js +211 -0
- package/dist/src/demo/yargs/03-/351/253/230/347/272/247/347/211/271/346/200/247.js +205 -0
- package/dist/src/demo/yargs/04-/345/256/236/346/210/230/346/241/210/344/276/213.js +276 -0
- package/package.json +39 -0
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* yargs 基础概念
|
|
4
|
+
* ================
|
|
5
|
+
* yargs 是一个强大的命令行参数解析库,用于构建交互式命令行工具
|
|
6
|
+
* 适用版本: yargs ^17.0.0 或 ^18.0.0
|
|
7
|
+
*/
|
|
8
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
9
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
10
|
+
};
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
const yargs_1 = __importDefault(require("yargs"));
|
|
13
|
+
const helpers_1 = require("yargs/helpers");
|
|
14
|
+
// #region 示例1: 最简单的参数解析
|
|
15
|
+
function demoSimpleArgumentParsing() {
|
|
16
|
+
console.log('\n=== 示例1: 最简单的参数解析 ===');
|
|
17
|
+
// 解析命令行参数
|
|
18
|
+
// 运行: ts-node 01-基础概念.ts --name=张三 --age=25
|
|
19
|
+
const argv = (0, yargs_1.default)((0, helpers_1.hideBin)(process.argv)).parseSync();
|
|
20
|
+
console.log('解析的参数:', argv);
|
|
21
|
+
console.log('name:', argv.name);
|
|
22
|
+
console.log('age:', argv.age);
|
|
23
|
+
}
|
|
24
|
+
// #endregion
|
|
25
|
+
// #region 示例2: 定义选项类型
|
|
26
|
+
function demoOptionTypes() {
|
|
27
|
+
console.log('\n=== 示例2: 定义选项类型 ===');
|
|
28
|
+
// 使用 .option() 定义参数的类型和描述
|
|
29
|
+
const argv = (0, yargs_1.default)((0, helpers_1.hideBin)(process.argv))
|
|
30
|
+
.option('name', {
|
|
31
|
+
alias: 'n', // 别名,可以用 -n 代替 --name
|
|
32
|
+
type: 'string', // 参数类型
|
|
33
|
+
description: '用户名称',
|
|
34
|
+
demandOption: true // 必填参数
|
|
35
|
+
})
|
|
36
|
+
.option('age', {
|
|
37
|
+
alias: 'a',
|
|
38
|
+
type: 'number',
|
|
39
|
+
description: '用户年龄',
|
|
40
|
+
default: 18 // 默认值
|
|
41
|
+
})
|
|
42
|
+
.parseSync();
|
|
43
|
+
console.log(`姓名: ${argv.name}, 年龄: ${argv.age}`);
|
|
44
|
+
}
|
|
45
|
+
// #endregion
|
|
46
|
+
// #region 示例3: 布尔类型参数
|
|
47
|
+
function demoBooleanOptions() {
|
|
48
|
+
console.log('\n=== 示例3: 布尔类型参数 ===');
|
|
49
|
+
// 布尔参数不需要值,存在即为 true
|
|
50
|
+
// 运行: ts-node 01-基础概念.ts --verbose --debug
|
|
51
|
+
const argv = (0, yargs_1.default)((0, helpers_1.hideBin)(process.argv))
|
|
52
|
+
.option('verbose', {
|
|
53
|
+
alias: 'v',
|
|
54
|
+
type: 'boolean',
|
|
55
|
+
description: '显示详细信息',
|
|
56
|
+
default: false
|
|
57
|
+
// demandOption: true // 必填参数
|
|
58
|
+
})
|
|
59
|
+
.option('debug', {
|
|
60
|
+
alias: 'd',
|
|
61
|
+
type: 'boolean',
|
|
62
|
+
description: '调试模式',
|
|
63
|
+
default: false
|
|
64
|
+
})
|
|
65
|
+
.parseSync();
|
|
66
|
+
console.log('详细模式:', argv.verbose);
|
|
67
|
+
console.log('调试模式:', argv.debug);
|
|
68
|
+
if (argv.verbose) {
|
|
69
|
+
console.log('正在执行详细输出...');
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
// #endregion
|
|
73
|
+
// #region 示例4: 数组类型参数
|
|
74
|
+
function demoArrayOptions() {
|
|
75
|
+
console.log('\n=== 示例4: 数组类型参数 ===');
|
|
76
|
+
// 数组参数可以接收多个值
|
|
77
|
+
// 运行: ts-node 01-基础概念.ts --tags=nodejs --tags=typescript --tags=yargs
|
|
78
|
+
const argv = (0, yargs_1.default)((0, helpers_1.hideBin)(process.argv))
|
|
79
|
+
.option('tags', {
|
|
80
|
+
alias: 't',
|
|
81
|
+
type: 'array',
|
|
82
|
+
description: '标签列表',
|
|
83
|
+
default: []
|
|
84
|
+
})
|
|
85
|
+
.parseSync();
|
|
86
|
+
console.log('标签列表:', argv.tags);
|
|
87
|
+
console.log('标签数量:', argv.tags?.length);
|
|
88
|
+
}
|
|
89
|
+
// #endregion
|
|
90
|
+
// #region 示例5: 帮助信息
|
|
91
|
+
function demoHelpInfo() {
|
|
92
|
+
console.log('\n=== 示例5: 帮助信息 ===');
|
|
93
|
+
// yargs 自动生成帮助信息
|
|
94
|
+
// 运行: ts-node 01-基础概念.ts --help
|
|
95
|
+
(0, yargs_1.default)((0, helpers_1.hideBin)(process.argv))
|
|
96
|
+
.option('name', {
|
|
97
|
+
alias: 'n',
|
|
98
|
+
type: 'string',
|
|
99
|
+
description: '用户名称',
|
|
100
|
+
demandOption: true
|
|
101
|
+
})
|
|
102
|
+
.option('age', {
|
|
103
|
+
alias: 'a',
|
|
104
|
+
type: 'number',
|
|
105
|
+
description: '用户年龄',
|
|
106
|
+
default: 18
|
|
107
|
+
})
|
|
108
|
+
.help() // 启用 --help 选项
|
|
109
|
+
.alias('help', 'h') // 添加 -h 别名
|
|
110
|
+
.parseSync();
|
|
111
|
+
console.log('帮助信息已显示(如果使用了 --help)');
|
|
112
|
+
}
|
|
113
|
+
// #endregion
|
|
114
|
+
if (require.main === module) {
|
|
115
|
+
const exampleNum = process.argv[2];
|
|
116
|
+
switch (exampleNum) {
|
|
117
|
+
case '1':
|
|
118
|
+
demoSimpleArgumentParsing();
|
|
119
|
+
break;
|
|
120
|
+
case '2':
|
|
121
|
+
demoOptionTypes();
|
|
122
|
+
break;
|
|
123
|
+
case '3':
|
|
124
|
+
demoBooleanOptions();
|
|
125
|
+
break;
|
|
126
|
+
case '4':
|
|
127
|
+
demoArrayOptions();
|
|
128
|
+
break;
|
|
129
|
+
case '5':
|
|
130
|
+
demoHelpInfo();
|
|
131
|
+
break;
|
|
132
|
+
default:
|
|
133
|
+
console.log('请指定示例编号 (1-5)');
|
|
134
|
+
console.log('用法: ts-node 01-基础概念.ts <示例编号> [参数...]');
|
|
135
|
+
console.log('\n可用示例:');
|
|
136
|
+
console.log(' 1 - 最简单的参数解析');
|
|
137
|
+
console.log(' 2 - 定义选项类型');
|
|
138
|
+
console.log(' 3 - 布尔类型参数');
|
|
139
|
+
console.log(' 4 - 数组类型参数');
|
|
140
|
+
console.log(' 5 - 帮助信息');
|
|
141
|
+
}
|
|
142
|
+
}
|
|
@@ -0,0 +1,211 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* yargs 命令系统
|
|
4
|
+
* ================
|
|
5
|
+
* 学习如何使用 yargs 构建多命令 CLI 工具(类似 git、npm 等)
|
|
6
|
+
* 适用版本: yargs ^17.0.0 或 ^18.0.0
|
|
7
|
+
*/
|
|
8
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
9
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
10
|
+
};
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
const yargs_1 = __importDefault(require("yargs"));
|
|
13
|
+
const helpers_1 = require("yargs/helpers");
|
|
14
|
+
// #region 示例1: 基础命令定义
|
|
15
|
+
function demoBasicCommand() {
|
|
16
|
+
console.log('\n=== 示例1: 基础命令定义 ===');
|
|
17
|
+
// 定义一个简单的命令
|
|
18
|
+
// 运行: ts-node 02-命令系统.ts 1 greet --name=张三
|
|
19
|
+
(0, yargs_1.default)((0, helpers_1.hideBin)(process.argv.slice(1))) // slice(1) 跳过示例编号
|
|
20
|
+
.command('greet', // 命令名称
|
|
21
|
+
'打招呼', // 命令描述
|
|
22
|
+
(yargs) => {
|
|
23
|
+
// 配置命令的选项
|
|
24
|
+
return yargs.option('name', {
|
|
25
|
+
alias: 'n',
|
|
26
|
+
type: 'string',
|
|
27
|
+
description: '名字',
|
|
28
|
+
demandOption: true
|
|
29
|
+
});
|
|
30
|
+
}, (argv) => {
|
|
31
|
+
// 命令处理函数
|
|
32
|
+
console.log(`你好, ${argv.name}!`);
|
|
33
|
+
})
|
|
34
|
+
.demandCommand(1, '请指定一个命令')
|
|
35
|
+
.help()
|
|
36
|
+
.argv;
|
|
37
|
+
}
|
|
38
|
+
// #endregion
|
|
39
|
+
// #region 示例2: 多个命令
|
|
40
|
+
function demoMultipleCommands() {
|
|
41
|
+
console.log('\n=== 示例2: 多个命令 ===');
|
|
42
|
+
// 定义多个命令,类似 git add、git commit
|
|
43
|
+
// 运行: ts-node 02-命令系统.ts 2 add --file=test.ts
|
|
44
|
+
// 运行: ts-node 02-命令系统.ts 2 remove --file=test.ts
|
|
45
|
+
(0, yargs_1.default)((0, helpers_1.hideBin)(process.argv.slice(1)))
|
|
46
|
+
.command('add', '添加文件', (yargs) => {
|
|
47
|
+
return yargs.option('file', {
|
|
48
|
+
alias: 'f',
|
|
49
|
+
type: 'string',
|
|
50
|
+
description: '文件路径',
|
|
51
|
+
demandOption: true
|
|
52
|
+
});
|
|
53
|
+
}, (argv) => {
|
|
54
|
+
console.log(`✓ 已添加文件: ${argv.file}`);
|
|
55
|
+
})
|
|
56
|
+
.command('remove', '删除文件', (yargs) => {
|
|
57
|
+
return yargs.option('file', {
|
|
58
|
+
alias: 'f',
|
|
59
|
+
type: 'string',
|
|
60
|
+
description: '文件路径',
|
|
61
|
+
demandOption: true
|
|
62
|
+
});
|
|
63
|
+
}, (argv) => {
|
|
64
|
+
console.log(`✓ 已删除文件: ${argv.file}`);
|
|
65
|
+
})
|
|
66
|
+
.demandCommand(1, '请指定一个命令 (add 或 remove)')
|
|
67
|
+
.help()
|
|
68
|
+
.argv;
|
|
69
|
+
}
|
|
70
|
+
// #endregion
|
|
71
|
+
// #region 示例3: 位置参数
|
|
72
|
+
function demoPositionalArguments() {
|
|
73
|
+
console.log('\n=== 示例3: 位置参数 ===');
|
|
74
|
+
// 使用位置参数(不需要 -- 前缀)
|
|
75
|
+
// 运行: ts-node 02-命令系统.ts 3 copy source.txt dest.txt
|
|
76
|
+
(0, yargs_1.default)((0, helpers_1.hideBin)(process.argv.slice(1)))
|
|
77
|
+
.command('copy <source> <destination>', // <> 表示必需的位置参数
|
|
78
|
+
'复制文件', (yargs) => {
|
|
79
|
+
return yargs
|
|
80
|
+
.positional('source', {
|
|
81
|
+
describe: '源文件路径',
|
|
82
|
+
type: 'string'
|
|
83
|
+
})
|
|
84
|
+
.positional('destination', {
|
|
85
|
+
describe: '目标文件路径',
|
|
86
|
+
type: 'string'
|
|
87
|
+
});
|
|
88
|
+
}, (argv) => {
|
|
89
|
+
console.log(`复制 ${argv.source} 到 ${argv.destination}`);
|
|
90
|
+
})
|
|
91
|
+
.demandCommand(1)
|
|
92
|
+
.help()
|
|
93
|
+
.argv;
|
|
94
|
+
}
|
|
95
|
+
// #endregion
|
|
96
|
+
// #region 示例4: 可选位置参数
|
|
97
|
+
function demoOptionalPositionalArgs() {
|
|
98
|
+
console.log('\n=== 示例4: 可选位置参数 ===');
|
|
99
|
+
// 使用可选位置参数
|
|
100
|
+
// 运行: ts-node 02-命令系统.ts 4 list
|
|
101
|
+
// 运行: ts-node 02-命令系统.ts 4 list /home/user
|
|
102
|
+
(0, yargs_1.default)((0, helpers_1.hideBin)(process.argv.slice(1)))
|
|
103
|
+
.command('list [directory]', // [] 表示可选的位置参数
|
|
104
|
+
'列出目录内容', (yargs) => {
|
|
105
|
+
return yargs.positional('directory', {
|
|
106
|
+
describe: '目录路径',
|
|
107
|
+
type: 'string',
|
|
108
|
+
default: '.'
|
|
109
|
+
});
|
|
110
|
+
}, (argv) => {
|
|
111
|
+
console.log(`列出目录: ${argv.directory}`);
|
|
112
|
+
})
|
|
113
|
+
.demandCommand(1)
|
|
114
|
+
.help()
|
|
115
|
+
.argv;
|
|
116
|
+
}
|
|
117
|
+
// #endregion
|
|
118
|
+
// #region 示例5: 子命令
|
|
119
|
+
function demoSubcommands() {
|
|
120
|
+
console.log('\n=== 示例5: 子命令 ===');
|
|
121
|
+
// 创建嵌套的子命令结构
|
|
122
|
+
// 运行: ts-node 02-命令系统.ts 5 user create --name=张三 --email=zhangsan@example.com
|
|
123
|
+
// 运行: ts-node 02-命令系统.ts 5 user delete --id=123
|
|
124
|
+
(0, yargs_1.default)((0, helpers_1.hideBin)(process.argv.slice(1)))
|
|
125
|
+
.command('user', '用户管理', (yargs) => {
|
|
126
|
+
return yargs
|
|
127
|
+
.command('create', '创建用户', (yargs) => {
|
|
128
|
+
return yargs
|
|
129
|
+
.option('name', {
|
|
130
|
+
type: 'string',
|
|
131
|
+
description: '用户名',
|
|
132
|
+
demandOption: true
|
|
133
|
+
})
|
|
134
|
+
.option('email', {
|
|
135
|
+
type: 'string',
|
|
136
|
+
description: '邮箱',
|
|
137
|
+
demandOption: true
|
|
138
|
+
});
|
|
139
|
+
}, (argv) => {
|
|
140
|
+
console.log(`✓ 创建用户: ${argv.name} (${argv.email})`);
|
|
141
|
+
})
|
|
142
|
+
.command('delete', '删除用户', (yargs) => {
|
|
143
|
+
return yargs.option('id', {
|
|
144
|
+
type: 'number',
|
|
145
|
+
description: '用户ID',
|
|
146
|
+
demandOption: true
|
|
147
|
+
});
|
|
148
|
+
}, (argv) => {
|
|
149
|
+
console.log(`✓ 删除用户 ID: ${argv.id}`);
|
|
150
|
+
})
|
|
151
|
+
.demandCommand(1, '请指定子命令 (create 或 delete)');
|
|
152
|
+
})
|
|
153
|
+
.demandCommand(1)
|
|
154
|
+
.help()
|
|
155
|
+
.argv;
|
|
156
|
+
}
|
|
157
|
+
// #endregion
|
|
158
|
+
// #region 示例6: 命令别名
|
|
159
|
+
function demoCommandAliases() {
|
|
160
|
+
console.log('\n=== 示例6: 命令别名 ===');
|
|
161
|
+
// 为命令设置别名
|
|
162
|
+
// 运行: ts-node 02-命令系统.ts 6 install express
|
|
163
|
+
// 运行: ts-node 02-命令系统.ts 6 i express (使用别名)
|
|
164
|
+
(0, yargs_1.default)((0, helpers_1.hideBin)(process.argv.slice(1)))
|
|
165
|
+
.command(['install <package>', 'i <package>'], // 第二个是别名
|
|
166
|
+
'安装包', (yargs) => {
|
|
167
|
+
return yargs.positional('package', {
|
|
168
|
+
describe: '包名称',
|
|
169
|
+
type: 'string'
|
|
170
|
+
});
|
|
171
|
+
}, (argv) => {
|
|
172
|
+
console.log(`正在安装: ${argv.package}`);
|
|
173
|
+
})
|
|
174
|
+
.demandCommand(1)
|
|
175
|
+
.help()
|
|
176
|
+
.argv;
|
|
177
|
+
}
|
|
178
|
+
// #endregion
|
|
179
|
+
if (require.main === module) {
|
|
180
|
+
const exampleNum = process.argv[2];
|
|
181
|
+
switch (exampleNum) {
|
|
182
|
+
case '1':
|
|
183
|
+
demoBasicCommand();
|
|
184
|
+
break;
|
|
185
|
+
case '2':
|
|
186
|
+
demoMultipleCommands();
|
|
187
|
+
break;
|
|
188
|
+
case '3':
|
|
189
|
+
demoPositionalArguments();
|
|
190
|
+
break;
|
|
191
|
+
case '4':
|
|
192
|
+
demoOptionalPositionalArgs();
|
|
193
|
+
break;
|
|
194
|
+
case '5':
|
|
195
|
+
demoSubcommands();
|
|
196
|
+
break;
|
|
197
|
+
case '6':
|
|
198
|
+
demoCommandAliases();
|
|
199
|
+
break;
|
|
200
|
+
default:
|
|
201
|
+
console.log('请指定示例编号 (1-6)');
|
|
202
|
+
console.log('用法: ts-node 02-命令系统.ts <示例编号> [命令] [参数...]');
|
|
203
|
+
console.log('\n可用示例:');
|
|
204
|
+
console.log(' 1 - 基础命令定义');
|
|
205
|
+
console.log(' 2 - 多个命令');
|
|
206
|
+
console.log(' 3 - 位置参数');
|
|
207
|
+
console.log(' 4 - 可选位置参数');
|
|
208
|
+
console.log(' 5 - 子命令');
|
|
209
|
+
console.log(' 6 - 命令别名');
|
|
210
|
+
}
|
|
211
|
+
}
|
|
@@ -0,0 +1,205 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* yargs 高级特性
|
|
4
|
+
* ================
|
|
5
|
+
* 学习 yargs 的高级功能:验证、中间件、配置文件等
|
|
6
|
+
* 适用版本: yargs ^17.0.0 或 ^18.0.0
|
|
7
|
+
*/
|
|
8
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
9
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
10
|
+
};
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
const yargs_1 = __importDefault(require("yargs"));
|
|
13
|
+
const helpers_1 = require("yargs/helpers");
|
|
14
|
+
// #region 示例1: 参数验证
|
|
15
|
+
function demoArgumentValidation() {
|
|
16
|
+
console.log('\n=== 示例1: 参数验证 ===');
|
|
17
|
+
// 使用 .check() 进行自定义验证
|
|
18
|
+
// 运行: ts-node 03-高级特性.ts 1 --age=25 --email=test@example.com
|
|
19
|
+
(0, yargs_1.default)((0, helpers_1.hideBin)(process.argv.slice(1)))
|
|
20
|
+
.option('age', {
|
|
21
|
+
type: 'number',
|
|
22
|
+
description: '年龄',
|
|
23
|
+
demandOption: true
|
|
24
|
+
})
|
|
25
|
+
.option('email', {
|
|
26
|
+
type: 'string',
|
|
27
|
+
description: '邮箱',
|
|
28
|
+
demandOption: true
|
|
29
|
+
})
|
|
30
|
+
.check((argv) => {
|
|
31
|
+
// 验证年龄范围
|
|
32
|
+
if (argv.age < 0 || argv.age > 150) {
|
|
33
|
+
throw new Error('年龄必须在 0-150 之间');
|
|
34
|
+
}
|
|
35
|
+
// 验证邮箱格式
|
|
36
|
+
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
|
|
37
|
+
if (!emailRegex.test(argv.email)) {
|
|
38
|
+
throw new Error('邮箱格式不正确');
|
|
39
|
+
}
|
|
40
|
+
return true;
|
|
41
|
+
})
|
|
42
|
+
.argv;
|
|
43
|
+
console.log('✓ 参数验证通过');
|
|
44
|
+
}
|
|
45
|
+
// #endregion
|
|
46
|
+
// #region 示例2: 参数冲突
|
|
47
|
+
function demoArgumentConflicts() {
|
|
48
|
+
console.log('\n=== 示例2: 参数冲突 ===');
|
|
49
|
+
// 使用 .conflicts() 定义互斥的参数
|
|
50
|
+
// 运行: ts-node 03-高级特性.ts 2 --json (正确)
|
|
51
|
+
// 运行: ts-node 03-高级特性.ts 2 --json --xml (错误,会报冲突)
|
|
52
|
+
(0, yargs_1.default)((0, helpers_1.hideBin)(process.argv.slice(1)))
|
|
53
|
+
.option('json', {
|
|
54
|
+
type: 'boolean',
|
|
55
|
+
description: '输出 JSON 格式'
|
|
56
|
+
})
|
|
57
|
+
.option('xml', {
|
|
58
|
+
type: 'boolean',
|
|
59
|
+
description: '输出 XML 格式'
|
|
60
|
+
})
|
|
61
|
+
.conflicts('json', 'xml') // json 和 xml 不能同时使用
|
|
62
|
+
.argv;
|
|
63
|
+
console.log('✓ 参数冲突检查通过');
|
|
64
|
+
}
|
|
65
|
+
// #endregion
|
|
66
|
+
// #region 示例3: 参数依赖
|
|
67
|
+
function demoArgumentDependencies() {
|
|
68
|
+
console.log('\n=== 示例3: 参数依赖 ===');
|
|
69
|
+
// 使用 .implies() 定义参数依赖关系
|
|
70
|
+
// 运行: ts-node 03-高级特性.ts 3 --ssl --cert=cert.pem --key=key.pem
|
|
71
|
+
(0, yargs_1.default)((0, helpers_1.hideBin)(process.argv.slice(1)))
|
|
72
|
+
.option('ssl', {
|
|
73
|
+
type: 'boolean',
|
|
74
|
+
description: '启用 SSL'
|
|
75
|
+
})
|
|
76
|
+
.option('cert', {
|
|
77
|
+
type: 'string',
|
|
78
|
+
description: 'SSL 证书路径'
|
|
79
|
+
})
|
|
80
|
+
.option('key', {
|
|
81
|
+
type: 'string',
|
|
82
|
+
description: 'SSL 密钥路径'
|
|
83
|
+
})
|
|
84
|
+
.implies('ssl', ['cert', 'key']) // 如果使用 ssl,必须提供 cert 和 key
|
|
85
|
+
.argv;
|
|
86
|
+
console.log('✓ 参数依赖检查通过');
|
|
87
|
+
}
|
|
88
|
+
// #endregion
|
|
89
|
+
// #region 示例4: 中间件
|
|
90
|
+
function demoMiddleware() {
|
|
91
|
+
console.log('\n=== 示例4: 中间件 ===');
|
|
92
|
+
// 使用 .middleware() 在命令执行前处理参数
|
|
93
|
+
// 运行: ts-node 03-高级特性.ts 4 --name=张三
|
|
94
|
+
(0, yargs_1.default)((0, helpers_1.hideBin)(process.argv.slice(1)))
|
|
95
|
+
.option('name', {
|
|
96
|
+
type: 'string',
|
|
97
|
+
description: '名字',
|
|
98
|
+
demandOption: true
|
|
99
|
+
})
|
|
100
|
+
.middleware((argv) => {
|
|
101
|
+
// 中间件:记录日志
|
|
102
|
+
console.log('[中间件] 接收到参数:', argv);
|
|
103
|
+
// 中间件:转换参数
|
|
104
|
+
argv.name = argv.name.toUpperCase();
|
|
105
|
+
console.log('[中间件] 转换后的名字:', argv.name);
|
|
106
|
+
})
|
|
107
|
+
.command('$0', '默认命令', () => { }, (argv) => {
|
|
108
|
+
console.log(`处理名字: ${argv.name}`);
|
|
109
|
+
})
|
|
110
|
+
.argv;
|
|
111
|
+
}
|
|
112
|
+
// #endregion
|
|
113
|
+
// #region 示例5: 选项分组
|
|
114
|
+
function demoOptionGrouping() {
|
|
115
|
+
console.log('\n=== 示例5: 选项分组 ===');
|
|
116
|
+
// 使用 .group() 将相关选项分组显示
|
|
117
|
+
// 运行: ts-node 03-高级特性.ts 5 --help
|
|
118
|
+
(0, yargs_1.default)((0, helpers_1.hideBin)(process.argv.slice(1)))
|
|
119
|
+
.option('host', {
|
|
120
|
+
type: 'string',
|
|
121
|
+
description: '服务器地址',
|
|
122
|
+
default: 'localhost'
|
|
123
|
+
})
|
|
124
|
+
.option('port', {
|
|
125
|
+
type: 'number',
|
|
126
|
+
description: '端口号',
|
|
127
|
+
default: 3000
|
|
128
|
+
})
|
|
129
|
+
.option('verbose', {
|
|
130
|
+
type: 'boolean',
|
|
131
|
+
description: '详细输出'
|
|
132
|
+
})
|
|
133
|
+
.option('debug', {
|
|
134
|
+
type: 'boolean',
|
|
135
|
+
description: '调试模式'
|
|
136
|
+
})
|
|
137
|
+
.group(['host', 'port'], '服务器配置:')
|
|
138
|
+
.group(['verbose', 'debug'], '日志选项:')
|
|
139
|
+
.help()
|
|
140
|
+
.argv;
|
|
141
|
+
console.log('查看 --help 可以看到分组效果');
|
|
142
|
+
}
|
|
143
|
+
// #endregion
|
|
144
|
+
// #region 示例6: 环境变量
|
|
145
|
+
function demoEnvironmentVariables() {
|
|
146
|
+
console.log('\n=== 示例6: 环境变量 ===');
|
|
147
|
+
// 使用 .env() 从环境变量读取配置
|
|
148
|
+
// 运行: MY_APP_PORT=8080 MY_APP_HOST=0.0.0.0 ts-node 03-高级特性.ts 6
|
|
149
|
+
(0, yargs_1.default)((0, helpers_1.hideBin)(process.argv.slice(1)))
|
|
150
|
+
.env('MY_APP') // 读取 MY_APP_ 前缀的环境变量
|
|
151
|
+
.option('port', {
|
|
152
|
+
type: 'number',
|
|
153
|
+
description: '端口号',
|
|
154
|
+
default: 3000
|
|
155
|
+
})
|
|
156
|
+
.option('host', {
|
|
157
|
+
type: 'string',
|
|
158
|
+
description: '主机地址',
|
|
159
|
+
default: 'localhost'
|
|
160
|
+
})
|
|
161
|
+
.argv;
|
|
162
|
+
const argv = (0, yargs_1.default)((0, helpers_1.hideBin)(process.argv.slice(1)))
|
|
163
|
+
.env('MY_APP')
|
|
164
|
+
.option('port', { type: 'number', default: 3000 })
|
|
165
|
+
.option('host', { type: 'string', default: 'localhost' })
|
|
166
|
+
.argv;
|
|
167
|
+
console.log('配置信息:');
|
|
168
|
+
console.log(' Host:', argv.host);
|
|
169
|
+
console.log(' Port:', argv.port);
|
|
170
|
+
console.log('\n提示: 可以设置环境变量 MY_APP_PORT 和 MY_APP_HOST');
|
|
171
|
+
}
|
|
172
|
+
// #endregion
|
|
173
|
+
if (require.main === module) {
|
|
174
|
+
const exampleNum = process.argv[2];
|
|
175
|
+
switch (exampleNum) {
|
|
176
|
+
case '1':
|
|
177
|
+
demoArgumentValidation();
|
|
178
|
+
break;
|
|
179
|
+
case '2':
|
|
180
|
+
demoArgumentConflicts();
|
|
181
|
+
break;
|
|
182
|
+
case '3':
|
|
183
|
+
demoArgumentDependencies();
|
|
184
|
+
break;
|
|
185
|
+
case '4':
|
|
186
|
+
demoMiddleware();
|
|
187
|
+
break;
|
|
188
|
+
case '5':
|
|
189
|
+
demoOptionGrouping();
|
|
190
|
+
break;
|
|
191
|
+
case '6':
|
|
192
|
+
demoEnvironmentVariables();
|
|
193
|
+
break;
|
|
194
|
+
default:
|
|
195
|
+
console.log('请指定示例编号 (1-6)');
|
|
196
|
+
console.log('用法: ts-node 03-高级特性.ts <示例编号> [参数...]');
|
|
197
|
+
console.log('\n可用示例:');
|
|
198
|
+
console.log(' 1 - 参数验证');
|
|
199
|
+
console.log(' 2 - 参数冲突');
|
|
200
|
+
console.log(' 3 - 参数依赖');
|
|
201
|
+
console.log(' 4 - 中间件');
|
|
202
|
+
console.log(' 5 - 选项分组');
|
|
203
|
+
console.log(' 6 - 环境变量');
|
|
204
|
+
}
|
|
205
|
+
}
|