A lightweight Node.js command line argv parser and command executor
npm install hemslhemsl is a lightweight Node.js command line argv parser and command executor







> White flowers Komatsu, scientific name: Villadia batesii (Hemsl.) Baehni & Macbride, Sedum, Tulian perennial multi-meat plants, flowering generally 4 to 5 months. White flowers Komatsu leaves more beautiful, there is a certain ornamental value; potted plants can be placed on the TV, the computer can absorb radiation, can also be planted in the room to absorb formaldehyde and other substances, purify the air.
``bash`
$ npm install hemsl --save
`bash`
$ yarn add hemsl
`js`
var Args = require('hemsl');
var args = new Args();
`js`
args
.version('1.1.0')
.bin('example');
`js`
args
.option(, {
option: 'debug',
default: true,
describe: 'Print debug log (global option)',
alias: 'd'
})
.option({
option: 'grep
default: true,
describe: 'Debug log filter (global option)',
alias: 'g'
});
`jsstart
args.command({
command: 'start
describe: 'Start a local server',
usage: 'example start
/**
* The command handle
* Start a local server
*/
fn: function(port, ip){
console.log('Server started at', 'http://' + ip + ':' + port);
var http = require('http');
var server = http.createServer(function(req, res){
console.log(req.method.bold.gray, req.url);
res.end(req.url);
})
server.listen(port, ip);
}
})
`
There are two ways to add an option to a command:
- Method 1: Call the args.command() method and set the configuration field optionsargs.command().option()
- Method 2: Call the method
#### Method 1
`js`
args.command({
command: 'start
describe: '...',
usage: '...',
fn: function(port, ip){
//...
},
options: {
'p': {
default: '',
describe: 'service port',
alias: 'port',
usage: ''
},
'hot-reload': {
alias: 'H',
describe: 'enable hot reload'
}
}
});
#### Method 2
`js`
args.command({
command: 'start
// ...
})
.option({
option: 'date-format',
default: 'yyyy-MM-dd',
alias: 'R',
describe: 'date format string'
})
.option({
option: 'time-format',
alias: 'm',
default: 'HH:mm:ss',
describe: 'time format string'
})
* Args
* new Args(config)
* [.parse([argv], [execute])](#Args+parse) ⇒ Object
* .execute() ⇒ Args
* .option(key, config) ⇒ Args
* .command(cmd, config) ⇒ Command
* [.help([cmdName])](#Args+help) ⇒ Args
* .version(ver) ⇒ Args
* .bin(binName) ⇒ Args
| Param | Type | Default | Description |
| --- | --- | --- | --- |
| config | Object | | 配置对象 |
| [config.__] | Boolean | false | 是否停止解析--后面的内容 |
| [config.colors] | Object | | 文本颜色配置 |
| [config.colors.title] | String | 'white' | 标题文本颜色 |
| [config.colors.command] | String | 'white' | 命令名称文本颜色 |
| [config.colors.option] | String | 'white' | Option文本颜色 |
| [config.colors.paragraph] | String | 'gray' | 段落文本颜色 |
| [config.colors.parameter] | String | 'gray' | 参数文本颜色 |
为true`,自动执行argv中的命令Kind: instance method of Args
Returns: Object - 解析后的对象
Access: public
| Param | Type | Default | Description |
| --- | --- | --- | --- |
| [argv] | Array | process.argv.slice(2) | 要解析的参数数组 |
| [execute] | Boolean | false | 是否自动执行参数中的命令 |
Kind: instance method of Args
Access: public
Kind: instance method of Args
Access: public
| Param | Type | Description |
| --- | --- | --- |
| key | String | 选项名称 |
| config | Object | 选项配置 |
Kind: instance method of Args
Access: public
| Param | Type | Description |
| --- | --- | --- |
| cmd | String | 命令名称 |
| config | Object | 命令配置 |
Kind: instance method of Args
Access: public
| Param | Type | Description |
| --- | --- | --- |
| [cmdName] | String | 命令名称 |
Kind: instance method of Args
Access: public
| Param | Type | Description |
| --- | --- | --- |
| ver | String | 版本号 |
Kind: instance method of Args
Access: public
| Param | Type | Description |
| --- | --- | --- |
| binName | String | 名称 |
* Command
* new Command(cmd, config)
* .option(key, opt) ⇒ Command
| Param | Type | Description |
| --- | --- | --- |
| cmd | String | 命令名称 |
| config | Object | 配置参数 |
| config.usage | String | 命令使用帮助 |
| config.describe | String | 命令描述信息 |
| config.fn | function | 执行命令时调用的函数 |
| config.options | Object | 命令支持的选项(option) |
Kind: instance method of Command
Access: public
| Param | Type | Description |
| --- | --- | --- |
| key | String | 选项名称 |
| opt | Object | 选项配置 |