all files / lib/ CommandGroup.js

96.77% Statements 30/31
83.33% Branches 15/18
100% Functions 5/5
96.77% Lines 30/31
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 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81                                                  11× 11× 11× 11× 11×   11× 22×                         42×   42× 11× 11×       11× 22×           11×     41×    
#!/usr/bin/node
var Command = require('./Command'),
    debug = require('debug')('CommandGroup'),
    helpers = require('./Helpers'),
    util = require('util'),
 
    ConfigError = require('./error/config'),
    InvalidInputError = require('./error/invalid-input');
 
/**
 * Command Group
 *
 * A container that holds commands. One command can be chosen by the user at a time, per Command Group.
 *
 * Options:
 *
 *  * name The name of the command
 *  * [description] Description of the command, used in help text and error messages.
 *  * commands An array of commands to use for this group.
 *  * [callback] function(cliInstance, command), A function that is called when this command is used.
 *  * [required] If this Command Group is required or not, default: false
 *
 * @param {{name, [description], commands, [callback] [required]}} options
 *
 * @constructor
 */
function CommandGroup(options) {
    this.name = options.name;
    this.description = options.description;
    this.commands = options.commands || [];
    this.callback = options.callback || function () {};
    this.required = (options.required !== undefined) ? options.required : false;
 
    this.commands.map(function (command) {
        Iif (!command instanceof Command) {
            throw new ConfigError("Command: " + command + " of group " + this.name + " was configured wrong, must be Array or Command object");
        }
    }.bind(this));
}
 
/**
 * Parse Commands
 *
 * Loop through remaining args, one by one, check each command group
 * @param args
 * @param commandGroups
 */
CommandGroup.parse = function (args, commandGroups) {
    var params = {};
 
    for (var i = 0; i < commandGroups.length; i++) {
        debug('Checking on command group: ' + commandGroups[i].name);
        var commandGroup = commandGroups[i],
            found = false,
            command;
 
        commandGroup.commands.forEach(function (command) {
            if (args[0] == command.name) {
                debug("Command found: " + commandGroup.name + " = " + args[0]);
                params[commandGroup.name] = command.name;
                if (command.callback instanceof Function) {
                    command.callback.call(this, this, command);
                }
                Eif (commandGroup.callback instanceof Function) {
                    commandGroup.callback.call(this, this, command);
                }
                found = true;
                args.shift();
                return false;
            }
        }.bind(this));
 
        if (!found && commandGroup.required) {
            debug('Not found!!');
            throw new InvalidInputError("Command Group " + commandGroup.name + " is required and cannot be omitted");
        }
    }
    return params;
};
 
module.exports = CommandGroup;