process模块

process.argv

@return <string[]>

启动Node进程时传入的命令行参数,返回一个数组

第一个元素是process.execPath,即启动进程的可执行文件的绝对路径 

第二个元素是正在执行的JavaScript文件的路径
 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
!#/usr/bin/env node

process.argv.forEach((arg,index) => {
console.log('index:',index,';arg:',arg)
})

input:
$ node process.js one {two:true} three

output:
index: 0; arg: /Users/joeng/.nvm/versions/node/v10.12.0/bin/node
index: 1; arg: /Users/joeng/Training/processor.js
index: 2; arg: one
index: 3; arg: {two:true}
index: 4; arg: three

npm package: yargs 可解析参数
https://github.com/yargs/yargs/blob/master/docs/api.md


Comments

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×