cli/options/FlagOption.js

/**
 * @module flitter-cli/options/FlagOption
 */

const Option = require('./Option')

/**
 * Non-positional, flag-based CLI option.
 * @extends module:flitter-cli/options/Option~Option
 */
class FlagOption extends Option {
    /**
     * Instantiate the option.
     * @param {string|boolean} long_flag - the name of the long flag (e.g. '--long'), false to disable
     * @param {string|boolean} short_flag - the name of the short flag (e.g. '-f'), false to disable
     * @param {string|boolean} message - help message, false to disable
     * @param {string|boolean} argument_description - argument description, false to disable
     */
    constructor(
        long_flag = false,
        short_flag = false,
        message = false,
        argument_description = false
    ) {
        super()

        /**
         * The long flag for this option.
         * e.g. '--long'
         * @type {string|boolean}
         */
        this.long_flag = long_flag

        /**
         * The short flag for this option.
         * e.g. '-l'
         * @type {string|boolean}
         */
        this.short_flag = short_flag

        /**
         * The help message.
         * @type {string|boolean}
         */
        this.message = message

        /**
         * The argument description.
         * @type {string|boolean}
         */
        this.argument_description = argument_description
    }

    /**
     * Get the referential name for this option.
     * Defaults to the long flag (without the '--'). If this cannot
     * be found, the short flag (without the '-') is used.
     * @returns {string}
     */
    get argument_name() {
        if ( this.long_flag ) {
            return this.long_flag.replace('--', '')
        } else {
            return this.short_flag.replace('-', '')
        }
    }
}

module.exports = exports = FlagOption