All files / src/toOpts pairArgvWithArgs.js

100% Statements 39/39
100% Branches 20/20
100% Functions 2/2
100% Lines 37/37

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 615x 5x   5x 89x   89x 89x 89x   89x 89x   89x 160x   160x   160x 79x 79x   79x 79x 79x   23x 23x 23x 42x 42x 42x 42x 42x   56x 18x   38x     79x   79x     81x 81x     160x 160x     89x       5x    
const {combine} = require('./combine')
const {option}  = require('./option')
 
const pairArgvWithArgs = (opts = []) => {
  const {args, errs: ERRS} = combine(...opts.map(option))
 
  return ({errs = [], argv: ARGV = []} = {}) => {
    const errs2 = errs.concat(ERRS)
    const opts2 = []
 
    let at  = 0
    let ARG = ARGV[at]
 
    while (ARG) {
      const options = args[ARG]
 
      let at2 = at + 1
 
      if (Array.isArray(options)) {
        for (let j = 0; j < options.length; j++) {
          const option = options[j]
 
          let {types} = option
          let values  = []
          if (typeof types === 'undefined') {
 
            let i = at + 1
            let arg = ARGV[i] || '--'
            while (arg !== '--') {
              if (typeof types === 'undefined') types = []
              types.push('string')
              values.push(arg)
              i++
              arg = ARGV[i] || '--'
            }
          } else if (Array.isArray(types) && types.length === 0) {
            values = [1]
          } else {
            values = ARGV.slice(at + 1, at + types.length + 1)
          }
 
          opts2.push({...option, values})
 
          at2 = at + (typeof types === 'undefined' ? 0 : types.length) + 1
        }
      } else {
        const values = ARGV.slice(at, at + 1)
        opts2.push({values})
      }
 
      at  = at2
      ARG = ARGV[at]
    }
 
    return {errs: errs2, opts: opts2}
  }
}
 
module.exports = {
  pairArgvWithArgs
}