@8pattern/jcommand 中文文档教程

发布于 5年前 浏览 25 项目主页 更新于 3年前

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
  1. execPath:执行器的路径。 它是普通的节点根路径。

     node cmd.js
     # jCommand.execPath === 'D:\\Node\\node.exe'
    
  2. filePath:代码文件的路径。

     node cmd.js
     # jCommand.filePath === 'D:\\cmd.js'
    
  3. args:命令行中的原始参数。

     node cmd.js -a -b=1 c:2
     # jCommand.args === ['-a', '-b=1', 'c:2']
    
  4. 命令 <命令[]> 参数包装列表。 Command 是参数的包装器。 它有五个属性:rawcommandvalueprenextraw 表示参数的原始字符串。 commandvalue会自动检测argument得到相应的结果(根据"="现在)。 如果无法从 raw 中检测到它们,则 command 将等于 raw 而值将被分配为 null。 prenext 链接上一个或下一个参数包装实例。 例如:

    Argument-a-b=1c:2
    raw-a-b=1c:2
    command-a-bc:2
    valuenull1null
     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
  1. option (command: string | string[], callback: Function): jCommand
  • exactly match the received commands
     jCommand.option('-P', callback)
     jCommand.option(['-P', '--port'], callback)
  1. match (command: RegExp, callback: Function): jCommands
  • 提供一个正则表达式来匹配所有有效参数

     jCommand.option(/--?P/i, callback)
    
  1. 模糊(命令: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)
    
  2. 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)YesNoNoYesNo
    option(['-p', '--port'], cb)YesNoNoYesYes
    match(/-p/, cb)YesNoYesYesYes
    match(/--P/i, cb)NoNoYesNoYes
    match(/^--?p$/i, cb)YesYesYesYesNo
    fuzzy('p', cb)YesYesYesYesNo
    fuzzy('p', '@', cb)NoNoNoNoNo

    注意

  3. 动作可以被链调用。

  const t = jCommand
      .option('-p',() => {})
      .match(/w/, () => {})
      .fuzzy('m', () => {})
      .valud(() => true, () => {})

  console.log(t === jCommand) // true
  1. 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
  1. 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
  1. execPath <string>: the path of the executor. It's Node root path in ordinary.

     node cmd.js
     # jCommand.execPath === 'D:\\Node\\node.exe'
    
  2. filePath <string>: the path of the code file.

     node cmd.js
     # jCommand.filePath === 'D:\\cmd.js'
    
  3. args <string[]>: the original arguments in command-line.

     node cmd.js -a -b=1 c:2
     # jCommand.args === ['-a', '-b=1', 'c:2']
    
  4. commands <Command[]> the list of argument wraps. Command is a wrapper for argument. It has five attributes: rawcommand, 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=1c:2
    raw-a-b=1c:2
    command-a-bc:2
    valuenull1null
     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
  1. option (command: string | string[], callback: Function): jCommand
  • exactly match the received commands
     jCommand.option('-P', callback)
     jCommand.option(['-P', '--port'], callback)
  1. match (command: RegExp, callback: Function): jCommands
  • provide a regular expression to match all valid argument

     jCommand.option(/--?P/i, callback)
    
  1. 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)
    
  2. 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)YesNoNoYesNo
    option(['-p', '--port'], cb)YesNoNoYesYes
    match(/-p/, cb)YesNoYesYesYes
    match(/--P/i, cb)NoNoYesNoYes
    match(/^--?p$/i, cb)YesYesYesYesNo
    fuzzy('p', cb)YesYesYesYesNo
    fuzzy('p', '@', cb)NoNoNoNoNo

    NOTICE

  3. the actions can be called by chains.

  const t = jCommand
      .option('-p',() => {})
      .match(/w/, () => {})
      .fuzzy('m', () => {})
      .valud(() => true, () => {})

  console.log(t === jCommand) // true
  1. 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
  1. 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
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文