-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcli.html
196 lines (124 loc) · 7.58 KB
/
cli.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
<!DOCTYPE html>
<html>
<head>
<title>cli.js</title>
<meta charset="UTF-8">
<link rel="stylesheet" media="all" href="public/stylesheets/normalize.css" />
<link rel="stylesheet" media="all" href="docs/docco.css" />
</head>
<body>
<div class="container">
<div class="page">
<div class="header">
<h1>cli.js</h1>
<div class="toc">
<h3>Table of Contents</h3>
<ol>
<li>
<a class="source" href="cli.html">
lib/cli.js
</a>
</li>
<li>
<a class="source" href="configure.html">
lib/configure.js
</a>
</li>
<li>
<a class="source" href="defaults.html">
lib/defaults.js
</a>
</li>
<li>
<a class="source" href="docco.html">
lib/docco.js
</a>
</li>
<li>
<a class="source" href="document.html">
lib/document.js
</a>
</li>
<li>
<a class="source" href="format.html">
lib/format.js
</a>
</li>
<li>
<a class="source" href="index.html">
lib/index.js
</a>
</li>
<li>
<a class="source" href="languages.html">
lib/languages.js
</a>
</li>
<li>
<a class="source" href="parse.html">
lib/parse.js
</a>
</li>
<li>
<a class="source" href="write.html">
lib/write.js
</a>
</li>
</ol>
</div>
</div>
<h2 id="command-line-interface">Command Line Interface</h2>
<p>The core part of the CLI is here. Mostly it provides a useful, simplifed
interface around the component parts. Running it locally is as simple as
<code>cli(opts)</code>, in fact this is how the binary works internally.</p>
<p>Import our external dependancies.</p>
<div class='highlight'><pre><span class="hljs-keyword">import</span> commander <span class="hljs-keyword">from</span> <span class="hljs-string">"commander"</span>;
<span class="hljs-keyword">import</span> fs <span class="hljs-keyword">from</span> <span class="hljs-string">"fs-extra"</span>;
<span class="hljs-keyword">import</span> path <span class="hljs-keyword">from</span> <span class="hljs-string">"path"</span>;</pre></div>
<p>Import our internal dependancies</p>
<div class='highlight'><pre><span class="hljs-keyword">import</span> defaults <span class="hljs-keyword">from</span> <span class="hljs-string">"./defaults"</span>;
<span class="hljs-keyword">import</span> <span class="hljs-built_in">document</span> <span class="hljs-keyword">from</span> <span class="hljs-string">"./document"</span>;</pre></div>
<p>Extract the docco version from <code>package.json</code>
We’re exporting this so it is available elsewhere.</p>
<div class='highlight'><pre><span class="hljs-keyword">export</span> <span class="hljs-keyword">const</span> version = <span class="hljs-built_in">JSON</span>.parse(fs.readFileSync(path.join(__dirname, <span class="hljs-string">"../package.json"</span>))).version;</pre></div>
<p>Finally, let’s define the interface to run Docco from the command line.
Parse options using <a href="https://github.com/visionmedia/commander.js">Commander</a>.
Programatically using the CLI function is possible by simply importing it and
then running <code>cli({...yourArguments})</code></p>
<div class='highlight'><pre><span class="hljs-keyword">export</span> <span class="hljs-keyword">const</span> cli = <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">args = process.argv, generate = document</span>) </span>{</pre></div>
<p>First, log the logo/description.</p>
<div class='highlight'><pre> global.console.log(<span class="hljs-string">`
__
____/ /___ ______________ ___ _____
/ __ / __ \\/ ___/ ___/ __ \\______/ _ \\/ ___/
/ /_/ / /_/ / /__/ /__/ /_/ /_____/ __(__ )
\\__,_/\\____/\\___/\\___/\\____/ \\___/____/
The Quick & Simple Literate Doccumentation Generator
`</span>);</pre></div>
<p>Now we use Commander to both describe the options and then parse the arguments.</p>
<div class='highlight'><pre> commander.version(version)
.usage(<span class="hljs-string">"[options] files"</span>)
.option(<span class="hljs-string">"-L, --languages [file]"</span>, <span class="hljs-string">"use a custom languages.json"</span>, (...args) => <span class="hljs-built_in">JSON</span>.parse(fs.readFileSync(...args)))
.option(<span class="hljs-string">"-l, --layout [name]"</span>, <span class="hljs-string">"choose a layout (parallel, linear or classic)"</span>, defaults.layout)
.option(<span class="hljs-string">"-o, --output [path]"</span>, <span class="hljs-string">"output to a given folder"</span>, defaults.output)
.option(<span class="hljs-string">"-c, --css [file]"</span>, <span class="hljs-string">"use a custom css file"</span>, defaults.css)
.option(<span class="hljs-string">"-t, --template [file]"</span>, <span class="hljs-string">"use a custom .jst template"</span>, defaults.template)
.option(<span class="hljs-string">"-e, --extension [ext]"</span>, <span class="hljs-string">"assume a file extension for all inputs"</span>, defaults.extension)
.option(<span class="hljs-string">"-m, --marked [file]"</span>, <span class="hljs-string">"use custom marked options"</span>, defaults.marked)
.option(<span class="hljs-string">"-T, --throw"</span>, <span class="hljs-string">"throw errors if code syntax highlighting fails"</span>, defaults.throw)
.option(<span class="hljs-string">"-v, --verbose"</span>, <span class="hljs-string">"shows all files as they are proccessed rather than a summary"</span>, defaults.verbose)
.parse(args).name = <span class="hljs-string">"docco"</span>;</pre></div>
<p>If we have parse arguments then we were called with files.
Lets try to build the documentation.</p>
<div class='highlight'><pre> <span class="hljs-keyword">if</span> (commander.args.length) {
<span class="hljs-keyword">return</span> generate(commander);
}</pre></div>
<p>If we weren’t, then just log the help text (a description of the options above).</p>
<div class='highlight'><pre> <span class="hljs-keyword">return</span> global.console.log(commander.helpInformation());
};
<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> cli;</pre></div>
<div class="fleur">h</div>
</div>
</div>
</body>
</html>