Skip to content
Permalink
Browse files

feat: add missing help command

  • Loading branch information
marionebl committed Feb 11, 2016
1 parent d8c1048 commit 448e5ec2d70187960711574bfbb32f9646d04205
Showing with 57 additions and 0 deletions.
  1. +57 −0 index.js
@@ -16,6 +16,8 @@ var memoize = require('lodash.memoize');
const read = denodeify(fs.readFile);
const rc = denodeify(rcNodeBack);

const pkg = require('./package.json');

const defaults = {
ignore: ['node_modules/**/*'],
validate: null,
@@ -25,6 +27,24 @@ const defaults = {
pretty: false
};

const aliases = {
ignore: 'i',
validate: 's',
indent: 'w',
env: 'e',
quiet: 'q',
pretty: 'p'
};

const descriptions = {
ignore: 'glob pattern to exclude from linting',
validate: 'uri to schema to use for validation',
indent: 'whitespace to use for pretty printing',
env: 'json schema env to use for validation',
quiet: 'surpress all output',
pretty: 'pretty-print the input'
};

function repeat(s, count) {
return new Array(count + 1).join(s);
}
@@ -362,7 +382,44 @@ function execute(settings) {
});
}

function printFlags() {
const flags = Object.keys(defaults)
.map(key => {
return [`--${aliases[key]}, --${key}`, `${descriptions[key]}, defaults to: "${defaults[key]}"`];
});

const lines = [
[`--h, --help`, `show this help`],
[`--v, --version`, `show jsonlint-cli version`]
].concat(flags);

const longestKeyLine = lines.sort((a, b) => b[0].length - a[0].length)[0];
const longest = longestKeyLine[0].length;

return lines
.map(line => `${line[0]}${' '.repeat(4 + longest - line[0].length)}${line[1]}`)
.join('\n');
}

function help() {
console.log(`
${pkg.name} [options] [file] - ${pkg.description}
${printFlags()}
`);
}

function main(options) {
if (options.help) {
help();
return Promise.resolve();
}

if (options.version) {
console.log(pkg.version);
return Promise.resolve();
}

return getSettings(options, process.cwd())
.then(execute);
}

0 comments on commit 448e5ec

Please sign in to comment.
You can’t perform that action at this time.