Completed
Push — master ( 19f1f4...2e8ae7 )
by Thomas
03:06 queued 02:31
created

module.exports   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 47

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
c 1
b 0
f 0
nc 3
dl 0
loc 47
rs 9.0303
nop 0
1
/*
2
 * Copyright (c) 2018 Includable.
3
 * Created by Thomas Schoffelen.
4
 */
5
6
function humanReadableArgName (arg) {
7
  var nameOutput = arg.name + (arg.variadic === true ? '...' : '')
8
9
  return arg.required
10
    ? '<' + nameOutput + '>'
11
    : '[' + nameOutput + ']'
12
}
13
14
function pad (str, width) {
15
  var len = Math.max(0, width - str.length)
16
  return str + Array(len + 1).join(' ')
17
}
18
19
module.exports = function (program, plugins) {
20
  program.commandHelp = function () {
21
    if (!this.commands.length) {
22
      return ''
23
    }
24
25
    var commands = this.commands.filter(function (cmd) {
26
      return !cmd._noHelp
27
    }).map(function (cmd) {
28
      var args = cmd._args.map(function (arg) {
29
        return humanReadableArgName(arg)
30
      }).join(' ')
31
32
      return [
33
        cmd._name +
34
        (cmd.options.length ? ' [options]' : '') +
35
        (args ? ' ' + args : ''),
36
        cmd._description
37
      ]
38
    })
39
40
    var width = commands.reduce(function (max, command) {
41
      return Math.max(max, command[0].length)
42
    }, 0)
43
44
    var output = [
45
      '',
46
      '  Commands:',
47
      '',
48
      commands.map(function (cmd) {
49
        var desc = cmd[1] ? '  ' + cmd[1] : ''
50
        return (desc ? pad(cmd[0], width) : cmd[0]) + desc
51
      }).join('\n').replace(/^/gm, '    '),
52
      ''
53
    ]
54
55
    if (plugins.plugins.length) {
56
      output = output.concat([
57
        '',
58
        '  Installed plugins:',
59
        '',
60
        plugins.plugins.join('\n').replace(/^/gm, '    '),
61
        ''
62
      ])
63
    }
64
65
    return output.concat(['']).join('\n')
66
  }
67
}
68