@8pattern/jcommand 中文文档教程
jCommand
的命令行助手
Install
npm install -S @8pattern/jcommand
Usage
Node.js Demo
// cmd.js
const jCommand = require('@8pattern/jcommand')
jCommand.option(['-P', '--port'], (cmd) => console.log(cmd.value))
node cmd.js -P=3000 or node cmd.js --port=3000
# the console will print 3000
- Attribute
execPath
:执行器的路径。 它是普通的节点根路径。 node cmd.js # jCommand.execPath === 'D:\\Node\\node.exe'
filePath
:代码文件的路径。 node cmd.js # jCommand.filePath === 'D:\\cmd.js'
args
:命令行中的原始参数。 node cmd.js -a -b=1 c:2 # jCommand.args === ['-a', '-b=1', 'c:2']
命令 <命令[]> 参数包装列表。 Command 是参数的包装器。 它有五个属性:raw,command,value,pre和next。 raw 表示参数的原始字符串。 command和value会自动检测argument得到相应的结果(根据"="现在)。 如果无法从 raw 中检测到它们,则 command 将等于 raw 而值将被分配为 null。 pre 和next 链接上一个或下一个参数包装实例。 例如:
Argument -a -b=1 c:2 raw -a -b=1 c:2 command -a -b c:2 value null 1 null node cmd.js -a -b=1 c:2 # jCommand.commands[1].raw === '-b=1' # jCommand.commands[1].pre.raw === '-a' # jCommand.commands[1].next.raw === 'c:2'
- Action
- option (command: string | string[], callback: Function): jCommand
- exactly match the received commands
jCommand.option('-P', callback)
jCommand.option(['-P', '--port'], callback)
- match (command: RegExp, callback: Function): jCommands
提供一个正则表达式来匹配所有有效参数
jCommand.option(/--?P/i, callback)
模糊(命令:string | string[],回调:Function):jCommand 或(命令:string | string[ ], prefix: string | string[], callback: Function): jCommand
- match the arguments in a case insensitive mode
- the prefix argument represents the prefix chars before the argument, ['-', '--'] by default
jCommand.fuzzy('p', callback) // sames as jCommand.fuzzy('p', ['-', '--'], callback) jCommand.fuzzy(['p', 'port'], '-', callback)
valid (validator: Function, callback: Function): jCommand
- can provide a custom validator to match the expected arguments.
- validator can receive the only argument —— a Command instance. If return true, the command will be regarded as selected. And false otherwise.
- In fact, option, match and fuzzy are three particular cases of valid.
下面介绍参数是否会触发相应的规则。
-p -P --p -p=3000 --port=3000 option('-p', cb) Yes No No Yes No option(['-p', '--port'], cb) Yes No No Yes Yes match(/-p/, cb) Yes No Yes Yes Yes match(/--P/i, cb) No No Yes No Yes match(/^--?p$/i, cb) Yes Yes Yes Yes No fuzzy('p', cb) Yes Yes Yes Yes No fuzzy('p', '@', cb) No No No No No 注意
动作可以被链调用。
const t = jCommand
.option('-p',() => {})
.match(/w/, () => {})
.fuzzy('m', () => {})
.valud(() => true, () => {})
console.log(t === jCommand) // true
- callback has the only argument of the Command instance. If need the previous or rest command-line arguments, they will can be found by pre and next attributes.
jCommand.option('-P', (cmd) => {
console.log(cmd.next.command)
})
// node cmd.js -P 3000 -> print 3000
- only if the rule satisfied, the callback will be triggered, even if it was triggered before. In other words, the callback may be triggered more than once.
jCommand
.option('-P', (cmd) => console.log('option:', cmd.command))
.match(/p/i, (cmd) => console.log('match: ', cmd.command))
.fuzzy('p', (cmd) => console.log('fuzzy: ', cmd.command))
.valid((cmd) => /p/i.test(cmd.command), (cmd) => console.log('valid: ', cmd.command))
// node cms.js -P
// print:
// option: -P
// match: -P
// fuzzy: -P
// valid: -P
jCommand
A command-line helper for Node.js
Install
npm install -S @8pattern/jcommand
Usage
Demo
// cmd.js
const jCommand = require('@8pattern/jcommand')
jCommand.option(['-P', '--port'], (cmd) => console.log(cmd.value))
node cmd.js -P=3000 or node cmd.js --port=3000
# the console will print 3000
- Attribute
execPath <string>: the path of the executor. It's Node root path in ordinary.
node cmd.js # jCommand.execPath === 'D:\\Node\\node.exe'
filePath <string>: the path of the code file.
node cmd.js # jCommand.filePath === 'D:\\cmd.js'
args <string[]>: the original arguments in command-line.
node cmd.js -a -b=1 c:2 # jCommand.args === ['-a', '-b=1', 'c:2']
commands <Command[]> the list of argument wraps. Command is a wrapper for argument. It has five attributes: raw,command, value, pre and next. raw represents the original string of the argument. command and value will be automatically detect the argument to obtain the corresponding results (according to "=" now). If they can't be detected from the raw, command will equal the raw while the value will be assigned to be null. pre and next link the previous or next argument wrap instance. For example:
Argument -a -b=1 c:2 raw -a -b=1 c:2 command -a -b c:2 value null 1 null node cmd.js -a -b=1 c:2 # jCommand.commands[1].raw === '-b=1' # jCommand.commands[1].pre.raw === '-a' # jCommand.commands[1].next.raw === 'c:2'
- Action
- option (command: string | string[], callback: Function): jCommand
- exactly match the received commands
jCommand.option('-P', callback)
jCommand.option(['-P', '--port'], callback)
- match (command: RegExp, callback: Function): jCommands
provide a regular expression to match all valid argument
jCommand.option(/--?P/i, callback)
fuzzy (command: string | string[], callback: Function): jCommand or (command: string | string[], prefix: string | string[], callback: Function): jCommand
- match the arguments in a case insensitive mode
- the prefix argument represents the prefix chars before the argument, ['-', '--'] by default
jCommand.fuzzy('p', callback) // sames as jCommand.fuzzy('p', ['-', '--'], callback) jCommand.fuzzy(['p', 'port'], '-', callback)
valid (validator: Function, callback: Function): jCommand
- can provide a custom validator to match the expected arguments.
- validator can receive the only argument —— a Command instance. If return true, the command will be regarded as selected. And false otherwise.
- In fact, option, match and fuzzy are three particular cases of valid.
The following presents whether the arguments will trigger the corresponding rules.
-p -P --p -p=3000 --port=3000 option('-p', cb) Yes No No Yes No option(['-p', '--port'], cb) Yes No No Yes Yes match(/-p/, cb) Yes No Yes Yes Yes match(/--P/i, cb) No No Yes No Yes match(/^--?p$/i, cb) Yes Yes Yes Yes No fuzzy('p', cb) Yes Yes Yes Yes No fuzzy('p', '@', cb) No No No No No NOTICE
the actions can be called by chains.
const t = jCommand
.option('-p',() => {})
.match(/w/, () => {})
.fuzzy('m', () => {})
.valud(() => true, () => {})
console.log(t === jCommand) // true
- callback has the only argument of the Command instance. If need the previous or rest command-line arguments, they will can be found by pre and next attributes.
jCommand.option('-P', (cmd) => {
console.log(cmd.next.command)
})
// node cmd.js -P 3000 -> print 3000
- only if the rule satisfied, the callback will be triggered, even if it was triggered before. In other words, the callback may be triggered more than once.
jCommand
.option('-P', (cmd) => console.log('option:', cmd.command))
.match(/p/i, (cmd) => console.log('match: ', cmd.command))
.fuzzy('p', (cmd) => console.log('fuzzy: ', cmd.command))
.valid((cmd) => /p/i.test(cmd.command), (cmd) => console.log('valid: ', cmd.command))
// node cms.js -P
// print:
// option: -P
// match: -P
// fuzzy: -P
// valid: -P