Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Standardize help output #853

Merged
merged 6 commits into from Aug 31, 2018
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
40 changes: 18 additions & 22 deletions Readme.md
Expand Up @@ -45,7 +45,7 @@ console.log(' - %s cheese', program.cheese);

Short flags may be passed as a single arg, for example `-abc` is equivalent to `-a -b -c`. Multi-word options such as "--template-engine" are camel-cased, becoming `program.templateEngine` etc.

Note that multi-word options starting with `--no` prefix negate the boolean value of the following word. For example, `--no-sauce` sets the value of `program.sauce` to false.
Note that multi-word options starting with `--no` prefix negate the boolean value of the following word. For example, `--no-sauce` sets the value of `program.sauce` to false.

```js
#!/usr/bin/env node
Expand Down Expand Up @@ -153,7 +153,7 @@ program
.option('-s --size <size>', 'Pizza size', /^(large|medium|small)$/i, 'medium')
.option('-d --drink [drink]', 'Drink', /^(coke|pepsi|izze)$/i)
.parse(process.argv);

console.log(' size: %j', program.size);
console.log(' drink: %j', program.drink);
```
Expand Down Expand Up @@ -248,30 +248,28 @@ You can enable `--harmony` option in two ways:
The help information is auto-generated based on the information commander already knows about your program, so the following `--help` info is for free:

```
$ ./examples/pizza --help

Usage: pizza [options]
$ ./examples/pizza --help
Usage: pizza [options]

An application for pizzas ordering
An application for pizzas ordering

Options:

-h, --help output usage information
-V, --version output the version number
-p, --peppers Add peppers
-P, --pineapple Add pineapple
-b, --bbq Add bbq sauce
-c, --cheese <type> Add the specified type of cheese [marble]
-C, --no-cheese You do not want any cheese
Options:

-h, --help output usage information
-V, --version output the version number
-p, --peppers Add peppers
-P, --pineapple Add pineapple
-b, --bbq Add bbq sauce
-c, --cheese <type> Add the specified type of cheese [marble]
-C, --no-cheese You do not want any cheese
```

## Custom help

You can display arbitrary `-h, --help` information
by listening for "--help". Commander will automatically
exit once you are done so that the remainder of your program
does not execute causing undesired behaviours, for example
does not execute causing undesired behaviors, for example
in the following executable "stuff" will not output when
`--help` is used.

Expand All @@ -294,11 +292,11 @@ program
// node's emit() is immediate

program.on('--help', function(){
console.log(' Examples:');
console.log('');
console.log(' $ custom-help --help');
console.log(' $ custom-help -h');
console.log('')
console.log('Examples:');
console.log('');
console.log(' $ custom-help --help');
console.log(' $ custom-help -h');
});

program.parse(process.argv);
Expand All @@ -309,7 +307,6 @@ console.log('stuff');
Yields the following help output when `node script-name.js -h` or `node script-name.js --help` are run:

```

Usage: custom-help [options]

Options:
Expand All @@ -324,7 +321,6 @@ Examples:

$ custom-help --help
$ custom-help -h

```

## .outputHelp(cb)
Expand Down
2 changes: 1 addition & 1 deletion examples/custom-help
Expand Up @@ -16,11 +16,11 @@ program
// node's emit() is immediate

program.on('--help', function(){
console.log('');
console.log(' Examples:');
console.log('');
console.log(' $ custom-help --help');
console.log(' $ custom-help -h');
console.log('');
});

program.parse(process.argv);
Expand Down
20 changes: 9 additions & 11 deletions index.js
Expand Up @@ -580,7 +580,7 @@ Command.prototype.executeSubCommand = function(argv, args, unknown) {
proc.on('close', process.exit.bind(process));
proc.on('error', function(err) {
if (err.code === 'ENOENT') {
console.error('\n %s(1) does not exist, try --help\n', bin);
console.error('%s(1) does not exist, try --help', bin);
} else if (err.code === 'EACCES') {
console.error('\n %s(1) not executable. try chmod or run with root\n', bin);
jaredpetersen marked this conversation as resolved.
Show resolved Hide resolved
}
Expand Down Expand Up @@ -1072,12 +1072,12 @@ Command.prototype.commandHelp = function() {
var width = this.padWidth();

return [
' Commands:',
'Commands:',
'',
commands.map(function(cmd) {
var desc = cmd[1] ? ' ' + cmd[1] : '';
return (desc ? pad(cmd[0], width) : cmd[0]) + desc;
}).join('\n').replace(/^/gm, ' '),
}).join('\n').replace(/^/gm, ' '),
''
].join('\n');
};
Expand All @@ -1093,17 +1093,17 @@ Command.prototype.helpInformation = function() {
var desc = [];
if (this._description) {
desc = [
' ' + this._description,
this._description,
''
];

var argsDescription = this._argsDescription;
if (argsDescription && this._args.length) {
var width = this.padWidth();
desc.push(' Arguments:');
desc.push('Arguments:');
desc.push('');
this._args.forEach(function(arg) {
desc.push(' ' + pad(arg.name, width) + ' ' + argsDescription[arg.name]);
desc.push(' ' + pad(arg.name, width) + ' ' + argsDescription[arg.name]);
});
desc.push('');
}
Expand All @@ -1114,8 +1114,7 @@ Command.prototype.helpInformation = function() {
cmdName = cmdName + '|' + this._alias;
}
var usage = [
'',
' Usage: ' + cmdName + ' ' + this.usage(),
'Usage: ' + cmdName + ' ' + this.usage(),
''
];

Expand All @@ -1124,17 +1123,16 @@ Command.prototype.helpInformation = function() {
if (commandHelp) cmds = [commandHelp];

var options = [
' Options:',
'Options:',
'',
'' + this.optionHelp().replace(/^/gm, ' '),
'' + this.optionHelp().replace(/^/gm, ' '),
''
];

return usage
.concat(desc)
.concat(options)
.concat(cmds)
.concat([''])
.join('\n');
};

Expand Down
4 changes: 2 additions & 2 deletions test/test.command.help.js
Expand Up @@ -5,8 +5,8 @@ var program = require('../')

program.command('bare');

program.commandHelp().should.equal(' Commands:\n\n bare\n');
program.commandHelp().should.equal('Commands:\n\n bare\n');

program.command('mycommand [options]');

program.commandHelp().should.equal(' Commands:\n\n bare\n mycommand [options]\n');
program.commandHelp().should.equal('Commands:\n\n bare\n mycommand [options]\n');
15 changes: 7 additions & 8 deletions test/test.command.helpInformation.js
Expand Up @@ -7,18 +7,17 @@ program.command('somecommand');
program.command('anothercommand [options]');

var expectedHelpInformation = [
'Usage: [options] [command]',
'',
' Usage: [options] [command]',
'Options:',
'',
' Options:',
' -h, --help output usage information',
'',
' -h, --help output usage information',
'Commands:',
'',
' Commands:',
'',
' somecommand',
' anothercommand [options]',
'\n'
' somecommand',
' anothercommand [options]',
''
].join('\n');

program.helpInformation().should.equal(expectedHelpInformation);
2 changes: 1 addition & 1 deletion test/test.command.name.js
Expand Up @@ -18,7 +18,7 @@ program.commands[1].name().should.equal('help');
var output = process.stdout.write.args[0];

output[0].should.containEql([
' mycommand [options] this is my command'
' mycommand [options] this is my command'
].join('\n'));

sinon.restore();
8 changes: 4 additions & 4 deletions test/test.command.nohelp.js
Expand Up @@ -47,10 +47,10 @@ process.stdout.write.args.length.should.equal(1);
var output = process.stdout.write.args[0];

var expect = [
' Commands:',
'Commands:',
'',
' mycommand [options] this is my command',
' anothercommand [options]',
' help [cmd] display help for [cmd]'
' mycommand [options] this is my command',
' anothercommand [options]',
' help [cmd] display help for [cmd]'
].join('\n');
output[0].indexOf(expect).should.not.be.equal(-1);