Skip to content

Commit 5a5ebaf

Browse files
committed
Unkink option parsing options
1 parent 254e331 commit 5a5ebaf

File tree

6 files changed

+104
-102
lines changed

6 files changed

+104
-102
lines changed

bin/documentation.js

Lines changed: 9 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -13,57 +13,15 @@ var parsedArgs = parseArgs();
1313
commands[parsedArgs.command](documentation, parsedArgs);
1414

1515
function parseArgs() {
16-
// reset() needs to be called at parse time because the yargs module uses an
17-
// internal global variable to hold option state
18-
var argv = addCommands(yargs, true)
19-
.usage('Usage: $0 <command> [options]')
16+
17+
var commandArgv = addCommands(yargs)
2018
.version(function () {
2119
return require('../package').version;
2220
})
23-
.option('shallow', {
24-
describe: 'shallow mode turns off dependency resolution, ' +
25-
'only processing the specified files (or the main script specified in package.json)',
26-
default: false,
27-
type: 'boolean'
28-
})
29-
.option('config', {
30-
describe: 'configuration file. an array defining explicit sort order',
31-
alias: 'c'
32-
})
33-
.option('external', {
34-
describe: 'a string / glob match pattern that defines which external ' +
35-
'modules will be whitelisted and included in the generated documentation.',
36-
default: null
37-
})
38-
.option('extension', {
39-
describe: 'only input source files matching this extension will be parsed, ' +
40-
'this option can be used multiple times.',
41-
alias: 'e'
42-
})
43-
.option('polyglot', {
44-
type: 'boolean',
45-
describe: 'polyglot mode turns off dependency resolution and ' +
46-
'enables multi-language support. use this to document c++'
47-
})
48-
.option('private', {
49-
describe: 'generate documentation tagged as private',
50-
type: 'boolean',
51-
default: false,
52-
alias: 'p'
53-
})
54-
.option('access', {
55-
describe: 'Include only comments with a given access level, out of private, ' +
56-
'protected, public, undefined. By default, public, protected, and undefined access ' +
57-
'levels are included',
58-
choices: ['public', 'private', 'protected', 'undefined'],
59-
alias: 'a'
60-
})
61-
.option('github', {
62-
type: 'boolean',
63-
describe: 'infer links to github in documentation',
64-
alias: 'g'
65-
})
66-
.argv;
21+
.argv;
22+
23+
var argv = commands[commandArgv._[0]]
24+
.parseArgs(yargs.reset()).argv;
6725

6826
var options = {};
6927
if (argv.config) {
@@ -109,14 +67,8 @@ function parseArgs() {
10967
}
11068

11169
function addCommands(parser, descriptionOnly) {
112-
parser = parser.demand(1);
113-
for (var cmd in commands) {
114-
if (descriptionOnly) {
115-
parser = parser.command(cmd, commands[cmd].description);
116-
} else {
117-
parser = parser.command(cmd, commands[cmd].description, commands[cmd].parseArgs);
118-
}
119-
}
120-
return parser.help('help');
70+
return Object.keys(commands).reduce(function(parser, cmd) {
71+
return parser.command(cmd, commands[cmd].description);
72+
}, parser.demand(1)).help('help');
12173
}
12274

lib/commands/build.js

Lines changed: 16 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
'use strict';
22

33
var streamArray = require('stream-array'),
4+
sharedOptions = require('./shared_options'),
45
fs = require('fs'),
56
vfs = require('vinyl-fs'),
67
chokidar = require('chokidar'),
@@ -10,26 +11,21 @@ module.exports = build;
1011
module.exports.description = 'build documentation';
1112

1213
module.exports.parseArgs = function (yargs) {
13-
var argv = addOutputArgs(yargs)
14-
.option('format', {
15-
alias: 'f',
16-
default: 'json',
17-
choices: ['json', 'md', 'html']
18-
})
19-
.option('output', {
20-
describe: 'output location. omit for stdout, otherwise is a filename ' +
21-
'for single-file outputs and a directory name for multi-file outputs like html',
22-
default: 'stdout',
23-
alias: 'o'
24-
})
25-
.example('$0 build foo.js -f md > API.md', 'parse documentation in a ' +
26-
'file and generate API documentation as Markdown')
27-
.argv;
28-
29-
if (argv.f === 'html' && argv.o === 'stdout') {
30-
yargs.showHelp();
31-
throw new Error('The HTML output mode requires a destination directory set with -o');
32-
}
14+
return sharedOptions.sharedOutputOptions(
15+
sharedOptions.sharedInputOptions(yargs))
16+
.option('format', {
17+
alias: 'f',
18+
default: 'json',
19+
choices: ['json', 'md', 'html']
20+
})
21+
.option('output', {
22+
describe: 'output location. omit for stdout, otherwise is a filename ' +
23+
'for single-file outputs and a directory name for multi-file outputs like html',
24+
default: 'stdout',
25+
alias: 'o'
26+
})
27+
.example('$0 build foo.js -f md > API.md', 'parse documentation in a ' +
28+
'file and generate API documentation as Markdown');
3329
};
3430

3531
/*
@@ -96,24 +92,3 @@ function build(documentation, parsedArgs, callback) {
9692
}));
9793
}
9894
}
99-
100-
// export this so `serve` can use it also
101-
module.exports.addOutputArgs = addOutputArgs;
102-
function addOutputArgs(yargs) {
103-
return yargs.option('theme', {
104-
describe: 'specify a theme: this must be a valid theme module',
105-
alias: 't'
106-
})
107-
.option('name', {
108-
describe: 'project name. by default, inferred from package.json'
109-
})
110-
.option('watch', {
111-
describe: 'watch input files and rebuild documentation when they change',
112-
alias: 'w',
113-
type: 'boolean'
114-
})
115-
.option('project-version', {
116-
describe: 'project version. by default, inferred from package.json'
117-
})
118-
.help('help');
119-
}

lib/commands/lint.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@
55
module.exports = lint;
66
module.exports.description = 'check for common style and uniformity mistakes';
77
module.exports.parseArgs = function (yargs) {
8-
yargs.example('$0 lint project.js', 'check documentation style')
9-
.help('help');
8+
return yargs
9+
.example('$0 lint project.js', 'check documentation style')
10+
.help('help');
1011
};
1112

1213
function lint(documentation, parsedArgs) {

lib/commands/readme.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ var build = require('./build');
1010
module.exports = readme;
1111
module.exports.description = 'inject documentation into your README.md';
1212
module.exports.parseArgs = function (yargs) {
13-
yargs.usage('Usage: $0 readme [--readme-file=README.md] --section "API"' +
13+
return yargs.usage('Usage: $0 readme [--readme-file=README.md] --section "API"' +
1414
' [--compare-only] [other documentationjs options]')
1515
.option('readme-file', {
1616
describe: 'The markdown file into which to inject documentation',

lib/commands/serve.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
'use strict';
22

33
var errorPage = require('../../lib/error_page'),
4+
sharedOptions = require('./shared_options'),
45
Server = require('../../lib/server');
56

67
var build = require('./build');
78

89
module.exports = serve;
910
module.exports.description = 'generate, update, and display HTML documentation';
10-
module.exports.parseArgs = build.addOutputArgs;
11+
module.exports.parseArgs = function (yargs) {
12+
return sharedOptions.sharedOutputOptions(
13+
sharedOptions.sharedInputOptions(yargs));
14+
};
1115

1216
var server = new Server();
1317
server.on('listening', function () {

lib/commands/shared_options.js

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/**
2+
* Adds shared options to any command that runs documentation
3+
*/
4+
function sharedInputOptions(parser) {
5+
return parser.option('shallow', {
6+
describe: 'shallow mode turns off dependency resolution, ' +
7+
'only processing the specified files (or the main script specified in package.json)',
8+
default: false,
9+
type: 'boolean'
10+
})
11+
.option('config', {
12+
describe: 'configuration file. an array defining explicit sort order',
13+
alias: 'c'
14+
})
15+
.option('external', {
16+
describe: 'a string / glob match pattern that defines which external ' +
17+
'modules will be whitelisted and included in the generated documentation.',
18+
default: null
19+
})
20+
.option('extension', {
21+
describe: 'only input source files matching this extension will be parsed, ' +
22+
'this option can be used multiple times.',
23+
alias: 'e'
24+
})
25+
.option('polyglot', {
26+
type: 'boolean',
27+
describe: 'polyglot mode turns off dependency resolution and ' +
28+
'enables multi-language support. use this to document c++'
29+
})
30+
.option('private', {
31+
describe: 'generate documentation tagged as private',
32+
type: 'boolean',
33+
default: false,
34+
alias: 'p'
35+
})
36+
.option('access', {
37+
describe: 'Include only comments with a given access level, out of private, ' +
38+
'protected, public, undefined. By default, public, protected, and undefined access ' +
39+
'levels are included',
40+
choices: ['public', 'private', 'protected', 'undefined'],
41+
alias: 'a'
42+
})
43+
.option('github', {
44+
type: 'boolean',
45+
describe: 'infer links to github in documentation',
46+
alias: 'g'
47+
});
48+
};
49+
50+
function sharedOutputOptions(yargs) {
51+
return yargs.option('theme', {
52+
describe: 'specify a theme: this must be a valid theme module',
53+
alias: 't'
54+
})
55+
.option('name', {
56+
describe: 'project name. by default, inferred from package.json'
57+
})
58+
.option('watch', {
59+
describe: 'watch input files and rebuild documentation when they change',
60+
alias: 'w',
61+
type: 'boolean'
62+
})
63+
.option('project-version', {
64+
describe: 'project version. by default, inferred from package.json'
65+
})
66+
.help('help');
67+
}
68+
69+
module.exports.sharedOutputOptions = sharedOutputOptions;
70+
module.exports.sharedInputOptions = sharedInputOptions;

0 commit comments

Comments
 (0)