Skip to content

Commit

Permalink
feat(help): Added custom help feature (#47)
Browse files Browse the repository at this point in the history
Allow user to define a custom help for both global and commands (fixes
#30)
Also fixes #14.
  • Loading branch information
mattallty authored Apr 13, 2017
1 parent d1e429a commit 3324eec
Show file tree
Hide file tree
Showing 7 changed files with 433 additions and 14 deletions.
43 changes: 43 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,49 @@ Help can be displayed using `-h` or `--help` options, or with the default `help`
<img src="https://github.com/mattallty/Caporal.js/raw/master/assets/colors.png" wdith="600">
</p>

## Custom help

You can add some custom help to the whole program or to specific commands using `.help()`.


### Custom help for the whole program

```javascript
#!/usr/bin/env node
const prog = require('caporal');
prog
.version('1.0.0')
.help('my global help') // here our custom help for the whole program
.command('order pizza')
.action(function(args, options) {

});

prog.parse(process.argv);
```

### Custom help for specific commands

```javascript
#!/usr/bin/env node
const prog = require('caporal');
prog
.version('1.0.0')
// first command
.command('order')
.help('my help for the order command') // here our custom help for the `order` command
.action(function(args, options) {

})
// second command
.command('cancel')
.help('my help for the cancel command') // here our custom help for the `cancel` command
.action(function(args, options) {

})

prog.parse(process.argv);
```

## Typo suggestions

Expand Down
2 changes: 2 additions & 0 deletions examples/pizza/pizza.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ const prog = require('../..');
prog
.version('1.0.0')
// the "order" command
.help(`My Custom help !!`)
.command('order', 'Order a pizza')
.help(`My Custom help about the order command !!`)
.alias('give-it-to-me')
// <kind> will be auto-magicaly autocompleted by providing the user with 3 choices
.argument('<kind>', 'Kind of pizza', ["margherita", "hawaiian", "fredo"])
Expand Down
10 changes: 10 additions & 0 deletions lib/command.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,16 @@ class Command extends GetterSetter {
}


/**
* Add help for the current command
*
* @param {String} help - Help string
* @returns {Command}
*/
help(help) {
this._program._helper._addCustomHelp(help, this);
return this;
}

/**
* @private
Expand Down
29 changes: 26 additions & 3 deletions lib/help.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,28 @@ const Table = require('cli-table2');
const chalk = require('chalk');
const colorize = require('./colorful').colorize;

/**
* @private
*/
class Help {

constructor(program) {
this._program = program;
this._customHelp = {};
}

display(command) {
/**
* Add a custom help for the whole program or a specific command
*
* @param {String} help - Help text
* @param {Command|null} command - Command concerned or null for program help
* @private
*/
_addCustomHelp(help, command) {
this._customHelp[command ? command._name : '_program'] = help;
}

get(command) {

if (!command && this._program._commands.length === 1) {
command = this._program._commands[0];
Expand All @@ -20,15 +35,19 @@ class Help {
let help = `
${chalk.cyan(this._program.name() || this._program.bin())} ${chalk.dim(this._program.version())} ${description}
${this._getUsage(command)} `;
${this._getUsage(command)}`;

if (!command && this._program._commands.length > 1) {
help += "\n\n " + this._getCommands();
}

help += "\n\n " + this._getGlobalOptions();

return console.log(help + "\n");
if (this._customHelp['_program']) {
help += "\n\n " + chalk.bold('MORE INFO') + "\n\n " + this._customHelp['_program'];
}

return help + "\n";
}

_getCommands() {
Expand All @@ -53,6 +72,10 @@ class Help {
const options = cmd.options();
let help = (cmd.name() ? cmd.name() + ' ' : '') + args.map(a => a.synopsis()).join(' ');

if (this._customHelp[cmd._name]) {
help += `\n\n ${this._customHelp[cmd._name]}`;
}

if (args.length) {
help += `\n\n ${chalk.bold('ARGUMENTS')}\n\n`;
const argsTable = this._getSimpleTable();
Expand Down
23 changes: 18 additions & 5 deletions lib/program.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class Program extends GetterSetter {
constructor() {
super();
this._commands = [];
this._help = new Help(this);
this._helper = new Help(this);
this.version = this.makeGetterSetter('version');
this.name = this.makeGetterSetter('name');
this.description = this.makeGetterSetter('description');
Expand All @@ -31,9 +31,22 @@ class Program extends GetterSetter {
/**
* @private
*/
help(cmdStr) {
_help(cmdStr) {
const cmd = this._commands.filter(c => (c.name() === cmdStr || c.getAlias() === cmdStr))[0];
this._help.display(cmd);
const help = this._helper.get(cmd);
console.log(help);
return help;
}

/**
* Add a global help for your program
*
* @param {String} help - Help string
* @returns {Program}
*/
help(help) {
this._helper._addCustomHelp(help);
return this;
}

/**
Expand Down Expand Up @@ -87,7 +100,7 @@ class Program extends GetterSetter {
_run(args, options) {

if (args[0] === 'help') {
this.help(args.slice(1).join(' '));
this._help(args.slice(1).join(' '));
return process.exit(0);
}

Expand Down Expand Up @@ -115,7 +128,7 @@ class Program extends GetterSetter {
}

if (!cmd || options.help || options.h) {
this.help();
this._help(args.join(' '));
return process.exit(0);
}

Expand Down
Loading

0 comments on commit 3324eec

Please sign in to comment.