all files / lib/output-parsers/ tty.js

100% Statements 61/61
100% Branches 20/20
100% Functions 11/11
100% Lines 61/61
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 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106                                                                                         
#!/usr/bin/node
var AbstractOutputParser = require('./abstract-output-parser');
var columnify = require('columnify');
 
function Tty(cli) {
	AbstractOutputParser.call(this, cli);
}
 
Tty.prototype = Object.create(AbstractOutputParser.prototype);
 
Tty.prototype.output = function () {
	this.usageStatement();
	this.commands();
	this.flags();
	this.options();
	process.stdout.write("\n");
};
 
Tty.prototype.usageStatement = function () {
	process.stdout.write("Usage: " + this.cli.scriptName);
	//Commands
	if (this.cli.commandGroups.length !== 0) {
		this.cli.commandGroups.forEach(function (commandGroup) {
			process.stdout.write(" [" + commandGroup.name + "]");
		});
	}
 
	//Flags
	if (this.cli.flags.length !== 0) {
		process.stdout.write(" [flags]");
	}
 
	//Options
	if (this.cli.options.length !== 0) {
		process.stdout.write(" [options]");
	}
 
	process.stdout.write("\n\n");
};
 
Tty.prototype.commands = function() {
	if (this.cli.commandGroups.length !== 0) {
		var commandGroupOptions = [];
		process.stdout.write("Commands:\n");
		this.cli.commandGroups.forEach(function(commandGroup) {
			commandGroupOptions.push(["\t", commandGroup.name, "-", commandGroup.description]);
			commandGroup.commands.forEach(function(command) {
				commandGroupOptions.push(["\t", "\t", command.name, command.description]);
			});
			commandGroupOptions.push([" "]);
		});
		commandGroupOptions.pop();
 
		var columns = columnify(commandGroupOptions, { showHeaders: false });
		process.stdout.write(columns);
		process.stdout.write("\n");
	}
};
 
Tty.prototype.flags = function () {
	if (this.cli.flags.length !== 0) {
		var flagOptions = [];
		process.stdout.write("Flags:\n");
		this.cli.flags.forEach(function (flag) {
			if(flag.shortFlag != null) {
				if(flag.longFlag != null) {
					flagOptions.push(["\t", flag.shortFlag + ", " + flag.longFlag, " ", flag.description]);
				}
				else {
					flagOptions.push(["\t", flag.shortFlag, " ", flag.description]);
				}
			}
			else {
				flagOptions.push(["\t", flag.longFlag, " ", flag.description]);
			}
		});
		var columns = columnify(flagOptions, { showHeaders: false });
		process.stdout.write(columns);
		process.stdout.write("\n");
	}
};
 
Tty.prototype.options = function () {
	if (this.cli.options.length !== 0) {
		var optionDetails = [];
		process.stdout.write("Options:\n");
		this.cli.options.forEach(function (option) {
			if(option.shortFlag != null) {
				if(option.longFlag != null) {
					optionDetails.push(["\t", option.shortFlag + ", " + option.longFlag, " ", option.description]);
				}
				else {
					optionDetails.push(["\t", option.shortFlag, " ", option.description]);
				}
			}
			else {
				optionDetails.push(["\t", option.longFlag, " ", option.description]);
			}
		});
		var columns = columnify(optionDetails, { showHeaders: false });
		process.stdout.write(columns);
		process.stdout.write("\n");
	}
};
 
module.exports = Tty;