Completed
Push — master ( 78b7d3...971ae6 )
by Thomas
49s queued 29s
created

CommandHelp.js ➔ humanReadableArgName   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
c 1
b 0
f 0
nc 2
dl 0
loc 7
rs 9.4285
nop 1
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) return ''
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
22
23
    var commands = this.commands.filter(function (cmd) {
24
      return !cmd._noHelp
25
    }).map(function (cmd) {
26
      var args = cmd._args.map(function (arg) {
27
        return humanReadableArgName(arg)
28
      }).join(' ')
29
30
      return [
31
        cmd._name +
32
        (cmd.options.length ? ' [options]' : '') +
33
        (args ? ' ' + args : ''),
34
        cmd._description
35
      ]
36
    })
37
38
    var width = commands.reduce(function (max, command) {
39
      return Math.max(max, command[0].length)
40
    }, 0)
41
42
    var output = [
43
      '',
44
      '  Commands:',
45
      '',
46
      commands.map(function (cmd) {
47
        var desc = cmd[1] ? '  ' + cmd[1] : ''
48
        return (desc ? pad(cmd[0], width) : cmd[0]) + desc
49
      }).join('\n').replace(/^/gm, '    '),
50
      ''
51
    ]
52
53
    if (plugins.plugins.length) {
54
      output = output.concat([
55
        '',
56
        '  Installed plugins:',
57
        '',
58
        plugins.plugins.join('\n').replace(/^/gm, '    '),
59
        ''
60
      ])
61
    }
62
63
    return output.concat(['']).join('\n')
64
  }
65
}
66