@@ -2,36 +2,27 @@ var path = require('path'),
2
2
yargs = require ( 'yargs' ) ,
3
3
loadConfig = require ( '../lib/load_config.js' ) ;
4
4
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'
19
11
} )
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'
28
15
} )
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++ '
33
20
} )
34
- . option ( 'theme' , {
21
+ . help ( 'help' ) ;
22
+ }
23
+
24
+ function outputOptions ( parser ) {
25
+ return parser . option ( 'theme' , {
35
26
describe : 'specify a theme: this must be a valid theme module' ,
36
27
alias : 't'
37
28
} )
@@ -44,42 +35,65 @@ function parse(args) {
44
35
. option ( 'name' , {
45
36
describe : 'project name. by default, inferred from package.json'
46
37
} )
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
- } )
61
38
. option ( 'github' , {
62
39
type : 'boolean' ,
63
40
describe : 'infer links to github in documentation' ,
64
41
alias : 'g'
65
42
} )
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'
75
47
} )
48
+ . option ( 'project-version' , {
49
+ describe : 'project version. by default, inferred from package.json'
50
+ } ) ;
51
+ }
76
52
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
+ }
79
84
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
+ }
81
94
82
- . parse ( args )
95
+ yargs . showHelp ( ) ;
96
+ process . exit ( 1 ) ;
83
97
}
84
98
85
99
/**
@@ -89,12 +103,10 @@ function parse(args) {
89
103
* @private
90
104
*/
91
105
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 ,
98
110
version = argv [ 'project-version' ] ,
99
111
transform ;
100
112
@@ -149,5 +161,3 @@ module.exports = function (args) {
149
161
output : argv . o
150
162
}
151
163
}
152
-
153
-
0 commit comments