Skip to content

Commit 92b7b4b

Browse files
committed
Improve binary by providing command-specific help strings
1 parent d47a224 commit 92b7b4b

File tree

2 files changed

+79
-62
lines changed

2 files changed

+79
-62
lines changed

lib/args.js

+72-62
Original file line numberDiff line numberDiff line change
@@ -2,36 +2,27 @@ var path = require('path'),
22
yargs = require('yargs'),
33
loadConfig = require('../lib/load_config.js');
44

5-
function parse(args) {
6-
// reset() needs to be called at parse time because the yargs module uses an
7-
// internal global variable to hold option state
8-
return yargs.reset()
9-
.usage('Usage: $0 <command> [options]')
10-
11-
.demand(1)
12-
13-
.command('build', 'build documentation')
14-
.command('lint', 'check for common style and uniformity mistakes')
15-
.command('serve', 'generate, update, and display HTML documentation')
16-
17-
.version(function () {
18-
return require('../package').version;
5+
function commonOptions(parser) {
6+
return parser.option('shallow', {
7+
describe: 'shallow mode turns off dependency resolution, ' +
8+
'only processing the specified files (or the main script specified in package.json)',
9+
default: false,
10+
type: 'boolean'
1911
})
20-
21-
.help('help')
22-
.alias('help', 'h')
23-
24-
.option('format', {
25-
alias: 'f',
26-
default: 'json',
27-
choices: ['json', 'md', 'html']
12+
.option('config', {
13+
describe: 'configuration file. an array defining explicit sort order',
14+
alias: 'c'
2815
})
29-
.option('watch', {
30-
describe: 'watch input files and rebuild documentation when they change',
31-
alias: 'w',
32-
type: 'boolean'
16+
.option('polyglot', {
17+
type: 'boolean',
18+
describe: 'polyglot mode turns off dependency resolution and ' +
19+
'enables multi-language support. use this to document c++'
3320
})
34-
.option('theme', {
21+
.help('help');
22+
}
23+
24+
function outputOptions(parser) {
25+
return parser.option('theme', {
3526
describe: 'specify a theme: this must be a valid theme module',
3627
alias: 't'
3728
})
@@ -44,42 +35,65 @@ function parse(args) {
4435
.option('name', {
4536
describe: 'project name. by default, inferred from package.json'
4637
})
47-
.option('project-version', {
48-
describe: 'project version. by default, inferred from package.json'
49-
})
50-
.option('shallow', {
51-
describe: 'shallow mode turns off dependency resolution, ' +
52-
'only processing the specified files (or the main script specified in package.json)',
53-
default: false,
54-
type: 'boolean'
55-
})
56-
.option('polyglot', {
57-
type: 'boolean',
58-
describe: 'polyglot mode turns off dependency resolution and ' +
59-
'enables multi-language support. use this to document c++'
60-
})
6138
.option('github', {
6239
type: 'boolean',
6340
describe: 'infer links to github in documentation',
6441
alias: 'g'
6542
})
66-
.option('config', {
67-
describe: 'configuration file. an array defining explicit sort order',
68-
alias: 'c'
69-
})
70-
.option('output', {
71-
describe: 'output location. omit for stdout, otherwise is a filename ' +
72-
'for single-file outputs and a directory name for multi-file outputs like html',
73-
default: 'stdout',
74-
alias: 'o'
43+
.option('watch', {
44+
describe: 'watch input files and rebuild documentation when they change',
45+
alias: 'w',
46+
type: 'boolean'
7547
})
48+
.option('project-version', {
49+
describe: 'project version. by default, inferred from package.json'
50+
});
51+
}
7652

77-
.example('$0 build foo.js -f md > API.md', 'parse documentation in a ' +
78-
'file and generate API documentation as Markdown')
53+
function parse(args) {
54+
// reset() needs to be called at parse time because the yargs module uses an
55+
// internal global variable to hold option state
56+
var command = yargs.reset()
57+
.usage('Usage: $0 <command> [options]')
58+
.demand(1)
59+
.command('build', 'build documentation')
60+
.command('lint', 'check for common style and uniformity mistakes')
61+
.command('serve', 'generate, update, and display HTML documentation')
62+
.version(function () {
63+
return require('../package').version;
64+
})
65+
.parse(args)._[0];
66+
67+
if (command === 'build') {
68+
return outputOptions(commonOptions(yargs.reset()))
69+
.option('format', {
70+
alias: 'f',
71+
default: 'json',
72+
choices: ['json', 'md', 'html']
73+
})
74+
.option('output', {
75+
describe: 'output location. omit for stdout, otherwise is a filename ' +
76+
'for single-file outputs and a directory name for multi-file outputs like html',
77+
default: 'stdout',
78+
alias: 'o'
79+
})
80+
.example('$0 build foo.js -f md > API.md', 'parse documentation in a ' +
81+
'file and generate API documentation as Markdown')
82+
.parse(args);
83+
}
7984

80-
.example('$0 lint project.js', 'check documentation style')
85+
if (command === 'serve') {
86+
return outputOptions(commonOptions(yargs.reset())).parse(args);
87+
}
88+
89+
if (command === 'lint') {
90+
return commonOptions(yargs.reset())
91+
.example('$0 lint project.js', 'check documentation style')
92+
.parse(args);
93+
}
8194

82-
.parse(args)
95+
yargs.showHelp();
96+
process.exit(1);
8397
}
8498

8599
/**
@@ -89,12 +103,10 @@ function parse(args) {
89103
* @private
90104
*/
91105
module.exports = function (args) {
92-
var argv = parse(args);
93-
94-
var command = argv._[0],
95-
inputs = argv._.slice(1);
96-
97-
var name = argv.name,
106+
var argv = parse(args),
107+
command = argv._[0],
108+
inputs = argv._.slice(1),
109+
name = argv.name,
98110
version = argv['project-version'],
99111
transform;
100112

@@ -149,5 +161,3 @@ module.exports = function (args) {
149161
output: argv.o
150162
}
151163
}
152-
153-

test/bin.js

+7
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,13 @@ test('given no files', function (t) {
156156
});
157157
}, options);
158158

159+
test('with an invalid command', function (t) {
160+
documentation(['invalid'], function (err) {
161+
t.ok(err, 'returns error');
162+
t.end();
163+
});
164+
}, options);
165+
159166
test('write to file', function (t) {
160167

161168
var dst = path.join(os.tmpdir(), (Date.now() + Math.random()).toString());

0 commit comments

Comments
 (0)