
Security News
Attackers Are Hunting High-Impact Node.js Maintainers in a Coordinated Social Engineering Campaign
Multiple high-impact npm maintainers confirm they have been targeted in the same social engineering campaign that compromised Axios.
**CLI-Param**是一个轻量的用于解析命令行参数以方便js程序读取的包。传入一个模板对象,用于定义解析参数的方式,然后**CLI-Param**会将命令行参数解析成一个js对象。如果用户输入的参数不符合模板标准,会自动打印错误信息,也可以自定义错误信息。
CLI-Param是一个轻量的用于解析命令行参数以方便js程序读取的包。传入一个模板对象,用于定义解析参数的方式,然后CLI-Param会将命令行参数解析成一个js对象。如果用户输入的参数不符合模板标准,会自动打印错误信息,也可以自定义错误信息。
const cliParam = require('cli-param')
const template = { //模板对象
handle(param) { //处理方法
console.log(param); //打印解析完成的参数对象
},
bsd: [
{
name: 'age1', //参数名
describe: '参数描述1'
},
{
name: 'age2', //参数名
describe: '参数描述2'
}
]
}
cliParam(template)
$> cli p1 p2
{ age1: 'p1', age2: 'p2' }
如果缺少一个bsd参数则会打印错误信息:
$> cli p1
缺少参数"age2"作为第"2"个参数
参数描述2
这些没有选项名的参数我们称为无名参数。
仅仅有这些还远远不够,很多时候需要能解析带选项的参数。
使用双减号--加选项名
const cliParam = require('cli-param')
const template = {
handle(m) {
console.log(m);
},
bsd: [
{
name: 'age1', //参数名
describe: '参数描述1'
}
],
option: [
{
name: 'op1', //选项名
ab: 'o' //选项简写
},
{
name: 'op2', //选项名
ab: 'a' //选项简写
},
{
name: 'op3', //选项名
ab: 'b' //选项简写
}
]
}
cliParam(template)
$> cli p1 --op1 OP1
{ age1: 'p1', op1: 'OP1' }
也可以用选项的简写,将使用单个-,解析结果仍会使用其全称作为键名。
$> cli p1 -o OP1
{ age1: 'p1', op1: 'OP1' }
如果缺省参数的值,将会被赋予true,方便作为模式选项使用。
以下写法的解析结果是一致的
但是第一种写法后面可能会跟值,而第二种只能作为模式选项。
$> cli p1 -a -b
$> cli p1 -ab
{ age1: 'p1', op3: true, op2: true }
有时候需要让一个命令有多个子命令,就像$> npm install cli-param中的install就是一个子命令。在CLI-Param中,使用subcmd来创建子命令。
解析成功后会调用不同的handle函数来处理参数。
const cliParam = require('cli-param')
const template = {
subcmd: {
install: {
describe: '下载',
handle(m) {
console.log('下载', m.package);
},
bsd: [
{
name: 'package',
describe: '要下载的包',
more: true //允许接受多个值
}
]
},
uninstall: {
describe: '卸载',
handle(m) {
console.log('卸载', m.package);
},
bsd: [
{
name: 'package',
describe: '要卸载的包',
more: true
}
]
}
}
}
cliParam(template)
$> cli install p1 p2
下载 [ 'p1', 'p2' ]
$> cli uninstall p1 p2
卸载 [ 'p1', 'p2' ]
FAQs
**CLI-Param**是一个轻量的用于解析命令行参数以方便js程序读取的包。传入一个模板对象,用于定义解析参数的方式,然后**CLI-Param**会将命令行参数解析成一个js对象。如果用户输入的参数不符合模板标准,会自动打印错误信息,也可以自定义错误信息。
The npm package cli-param receives a total of 0 weekly downloads. As such, cli-param popularity was classified as not popular.
We found that cli-param demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
Multiple high-impact npm maintainers confirm they have been targeted in the same social engineering campaign that compromised Axios.

Security News
Axios compromise traced to social engineering, showing how attacks on maintainers can bypass controls and expose the broader software supply chain.

Security News
Node.js has paused its bug bounty program after funding ended, removing payouts for vulnerability reports but keeping its security process unchanged.