/*
 * Optics / subcommand.js
 * copyright (c) 2016 Susisu
 */

/**
 * @module subcommand
 */

"use strict";

function endModule() {
    module.exports = Object.freeze({
        Subcommand,
        CommandGroup
    });
}

const command = require("./command.js");

/**
 * The `Subcommand` associates a name with a command.
 * @static
 */
class Subcommand {
    /**
     * Creates a new `Subcommand` instance.
     * @param {string} name The name of the subcommand.
     * @param {module:command.CommandBase} cmd The command associated to the name.
     * @throws {TypeError} `name` is not a string.
     * @throws {TypeError} `cmd` is not an instance of {@link module:command.CommandBase}.
     */
    constructor(name, cmd) {
        if (typeof name !== "string") {
            throw new TypeError("name must be a string");
        }
        if (!(cmd instanceof command.CommandBase)) {
            throw new TypeError("cmd must be an instance of CommandBase");
        }
        this.name = name;
        this.cmd  = cmd;
    }
}

/**
 * A `CommandGroup` instance represents a group of subcommands.
 * @static
 * @extends {module:command.CommandBase}
 */
class CommandGroup extends command.CommandBase {
    /**
     * Creates a new `CommandGroup` instance.
     * @param {string} desc The description of the command group.
     * @param {module:subcommand.Subcommand[]} subcmds Subcommands the group contains.
     * @param {(module:command.CommandBase | null)} defaultCmd Default command, invoked when no subcommand is specified.
     */
    constructor(desc, subcmds, defaultCmd) {
        super();
        this.desc       = desc;
        this.subcmds    = subcmds;
        this.defaultCmd = defaultCmd;
    }
}

endModule();

@@ -1535,54 +1535,771 @@
    *
-/%0A    constructor(desc, subcmds, defaultCmd) %7B
+ @throws %7BTypeError%7D %60desc%60 is not a string.%0A     * @throws %7BTypeError%7D %60subcmds%60 is not an array of %7B@link module:subcommand.Subcommand%7D.%0A     * @throws %7BTypeError%7D %60defaultCmd%60 is not an instance of %7B@link module:command.CommandBase%7D nor null.%0A     */%0A    constructor(desc, subcmds, defaultCmd) %7B%0A        if (typeof desc !== %22string%22) %7B%0A            throw new TypeError(%22desc must be a string%22);%0A        %7D%0A        if (!Array.isArray(subcmds) %7C%7C !subcmds.every(subcmd =%3E subcmd instanceof Subcommand)) %7B%0A            throw new TypeError(%22args must be an array of Subcommand%22);%0A        %7D%0A        if (!(defaultCmd instanceof command.CommandBase) && defaultCmd !== null) %7B%0A            throw new TypeError(%22defaultCmd must be an instance of Command or null%22)%0A        %7D
 %0A   

Fix CommandGroup constructor does not throw TypeErrors