coa 1.0.3 → 1.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.npmignore +6 -0
- package/.nyc_output/1f2a0db5a6d6559149db56d397f47cfc.json +1 -0
- package/.nyc_output/75b82d38f2186df930141082076e11c6.json +1 -0
- package/.travis.yml +9 -0
- package/GNUmakefile +34 -0
- package/README.md +1 -19
- package/coverage/base.css +212 -0
- package/coverage/coa/index.html +93 -0
- package/coverage/coa/index.js.html +68 -0
- package/coverage/coa/lib/arg.js.html +239 -0
- package/coverage/coa/lib/cmd.js.html +1556 -0
- package/coverage/coa/lib/coaobject.js.html +365 -0
- package/coverage/coa/lib/coaparam.js.html +440 -0
- package/coverage/coa/lib/color.js.html +131 -0
- package/coverage/coa/lib/completion.js.html +593 -0
- package/coverage/coa/lib/index.html +197 -0
- package/coverage/coa/lib/index.js.html +107 -0
- package/coverage/coa/lib/opt.js.html +524 -0
- package/coverage/coa/lib/shell.js.html +107 -0
- package/coverage/index.html +106 -0
- package/coverage/prettify.css +1 -0
- package/coverage/prettify.js +1 -0
- package/coverage/sort-arrow-sprite.png +0 -0
- package/coverage/sorter.js +158 -0
- package/index.js +1 -1
- package/lib/arg.js +161 -44
- package/lib/cmd.js +547 -434
- package/lib/color.js +22 -19
- package/lib/completion.js +119 -161
- package/lib/index.js +10 -14
- package/lib/opt.js +313 -130
- package/lib/shell.js +13 -13
- package/package.json +14 -19
- package/qq.js +17 -0
- package/src/arg.coffee +130 -0
- package/src/cmd.coffee +456 -0
- package/src/color.coffee +25 -0
- package/src/completion.coffee +156 -0
- package/src/index.coffee +5 -0
- package/src/opt.coffee +243 -0
- package/src/shell.coffee +10 -0
- package/test/coa.js +496 -0
- package/test/mocha.opts +2 -0
- package/test/shell-test.js +60 -0
- package/tests/api-h.js +9 -0
- package/tests/h.js +6 -0
- package/LICENSE +0 -21
- package/lib/coaobject.js +0 -100
- package/lib/coaparam.js +0 -125
package/src/opt.coffee
ADDED
@@ -0,0 +1,243 @@
|
|
1
|
+
fs = require 'fs'
|
2
|
+
Q = require 'q'
|
3
|
+
Color = require('./color').Color
|
4
|
+
Cmd = require('./cmd').Cmd
|
5
|
+
|
6
|
+
###*
|
7
|
+
Option
|
8
|
+
|
9
|
+
Named entity. Options may have short and long keys for use from command line.
|
10
|
+
@namespace
|
11
|
+
@class Presents option
|
12
|
+
###
|
13
|
+
exports.Opt = class Opt
|
14
|
+
|
15
|
+
###*
|
16
|
+
@constructs
|
17
|
+
@param {COA.Cmd} cmd parent command
|
18
|
+
###
|
19
|
+
constructor: (@_cmd) -> @_cmd._opts.push @
|
20
|
+
|
21
|
+
###*
|
22
|
+
Set a canonical option identifier to be used anywhere in the API.
|
23
|
+
@param {String} _name option name
|
24
|
+
@returns {COA.Opt} this instance (for chainability)
|
25
|
+
###
|
26
|
+
name: (@_name) -> @
|
27
|
+
|
28
|
+
###*
|
29
|
+
Set a long description for option to be used anywhere in text messages.
|
30
|
+
@param {String} _title option title
|
31
|
+
@returns {COA.Opt} this instance (for chainability)
|
32
|
+
###
|
33
|
+
title: Cmd::title
|
34
|
+
|
35
|
+
###*
|
36
|
+
Set a short key for option to be used with one hyphen from command line.
|
37
|
+
@param {String} _short
|
38
|
+
@returns {COA.Opt} this instance (for chainability)
|
39
|
+
###
|
40
|
+
short: (@_short) -> @_cmd._optsByKey['-' + _short] = @
|
41
|
+
|
42
|
+
###*
|
43
|
+
Set a short key for option to be used with double hyphens from command line.
|
44
|
+
@param {String} _long
|
45
|
+
@returns {COA.Opt} this instance (for chainability)
|
46
|
+
###
|
47
|
+
long: (@_long) -> @_cmd._optsByKey['--' + _long] = @
|
48
|
+
|
49
|
+
###*
|
50
|
+
Make an option boolean, i.e. option without value.
|
51
|
+
@returns {COA.Opt} this instance (for chainability)
|
52
|
+
###
|
53
|
+
flag: () ->
|
54
|
+
@_flag = true
|
55
|
+
@
|
56
|
+
|
57
|
+
###*
|
58
|
+
Makes an option accepts multiple values.
|
59
|
+
Otherwise, the value will be used by the latter passed.
|
60
|
+
@returns {COA.Opt} this instance (for chainability)
|
61
|
+
###
|
62
|
+
arr: ->
|
63
|
+
@_arr = true
|
64
|
+
@
|
65
|
+
|
66
|
+
###*
|
67
|
+
Makes an option required.
|
68
|
+
@returns {COA.Opt} this instance (for chainability)
|
69
|
+
###
|
70
|
+
req: ->
|
71
|
+
@_req = true
|
72
|
+
@
|
73
|
+
|
74
|
+
###*
|
75
|
+
Makes an option to act as a command,
|
76
|
+
i.e. program will exit just after option action.
|
77
|
+
@returns {COA.Opt} this instance (for chainability)
|
78
|
+
###
|
79
|
+
only: ->
|
80
|
+
@_only = true
|
81
|
+
@
|
82
|
+
|
83
|
+
###*
|
84
|
+
Set a validation (or value) function for option.
|
85
|
+
Value from command line passes through before becoming available from API.
|
86
|
+
Using for validation and convertion simple types to any values.
|
87
|
+
@param {Function} _val validating function,
|
88
|
+
invoked in the context of option instance
|
89
|
+
and has one parameter with value from command line
|
90
|
+
@returns {COA.Opt} this instance (for chainability)
|
91
|
+
###
|
92
|
+
val: (@_val) -> @
|
93
|
+
|
94
|
+
###*
|
95
|
+
Set a default value for option.
|
96
|
+
Default value passed through validation function as ordinary value.
|
97
|
+
@param {Object} _def
|
98
|
+
@returns {COA.Opt} this instance (for chainability)
|
99
|
+
###
|
100
|
+
def: (@_def) -> @
|
101
|
+
|
102
|
+
###*
|
103
|
+
Make option value inputting stream.
|
104
|
+
It's add useful validation and shortcut for STDIN.
|
105
|
+
@returns {COA.Opt} this instance (for chainability)
|
106
|
+
###
|
107
|
+
input: ->
|
108
|
+
# XXX: hack to workaround a bug in node 0.6.x,
|
109
|
+
# see https://github.com/joyent/node/issues/2130
|
110
|
+
process.stdin.pause();
|
111
|
+
|
112
|
+
@
|
113
|
+
.def(process.stdin)
|
114
|
+
.val (v) ->
|
115
|
+
if typeof v is 'string'
|
116
|
+
if v is '-'
|
117
|
+
process.stdin
|
118
|
+
else
|
119
|
+
s = fs.createReadStream v, { encoding: 'utf8' }
|
120
|
+
s.pause()
|
121
|
+
s
|
122
|
+
else v
|
123
|
+
|
124
|
+
###*
|
125
|
+
Make option value outputing stream.
|
126
|
+
It's add useful validation and shortcut for STDOUT.
|
127
|
+
@returns {COA.Opt} this instance (for chainability)
|
128
|
+
###
|
129
|
+
output: ->
|
130
|
+
@
|
131
|
+
.def(process.stdout)
|
132
|
+
.val (v) ->
|
133
|
+
if typeof v is 'string'
|
134
|
+
if v is '-'
|
135
|
+
process.stdout
|
136
|
+
else
|
137
|
+
fs.createWriteStream v, { encoding: 'utf8' }
|
138
|
+
else v
|
139
|
+
|
140
|
+
###*
|
141
|
+
Add action for current option command.
|
142
|
+
This action is performed if the current option
|
143
|
+
is present in parsed options (with any value).
|
144
|
+
@param {Function} act action function,
|
145
|
+
invoked in the context of command instance
|
146
|
+
and has the parameters:
|
147
|
+
- {Object} opts parsed options
|
148
|
+
- {Array} args parsed arguments
|
149
|
+
- {Object} res actions result accumulator
|
150
|
+
It can return rejected promise by Cmd.reject (in case of error)
|
151
|
+
or any other value treated as result.
|
152
|
+
@returns {COA.Opt} this instance (for chainability)
|
153
|
+
###
|
154
|
+
act: (act) ->
|
155
|
+
opt = @
|
156
|
+
name = @_name
|
157
|
+
@_cmd.act (opts) ->
|
158
|
+
if name of opts
|
159
|
+
res = act.apply @, arguments
|
160
|
+
if opt._only
|
161
|
+
Q.when res, (res) =>
|
162
|
+
@reject {
|
163
|
+
toString: -> res.toString()
|
164
|
+
exitCode: 0
|
165
|
+
}
|
166
|
+
else
|
167
|
+
res
|
168
|
+
@
|
169
|
+
|
170
|
+
###*
|
171
|
+
Set custom additional completion for current option.
|
172
|
+
@param {Function} completion generation function,
|
173
|
+
invoked in the context of option instance.
|
174
|
+
Accepts parameters:
|
175
|
+
- {Object} opts completion options
|
176
|
+
It can return promise or any other value treated as result.
|
177
|
+
@returns {COA.Opt} this instance (for chainability)
|
178
|
+
###
|
179
|
+
comp: Cmd::comp
|
180
|
+
|
181
|
+
_saveVal: (opts, val) ->
|
182
|
+
if @_val then val = @_val val
|
183
|
+
if @_arr
|
184
|
+
(opts[@_name] or= []).push val
|
185
|
+
else
|
186
|
+
opts[@_name] = val
|
187
|
+
val
|
188
|
+
|
189
|
+
_parse: (argv, opts) ->
|
190
|
+
@_saveVal(
|
191
|
+
opts,
|
192
|
+
if @_flag
|
193
|
+
true
|
194
|
+
else
|
195
|
+
argv.shift()
|
196
|
+
)
|
197
|
+
|
198
|
+
_checkParsed: (opts, args) -> not opts.hasOwnProperty @_name
|
199
|
+
|
200
|
+
_usage: ->
|
201
|
+
res = []
|
202
|
+
nameStr = @_name.toUpperCase()
|
203
|
+
|
204
|
+
if @_short
|
205
|
+
res.push '-', Color 'lgreen', @_short
|
206
|
+
unless @_flag then res.push ' ' + nameStr
|
207
|
+
res.push ', '
|
208
|
+
|
209
|
+
if @_long
|
210
|
+
res.push '--', Color 'green', @_long
|
211
|
+
unless @_flag then res.push '=' + nameStr
|
212
|
+
|
213
|
+
res.push ' : ', @_title
|
214
|
+
|
215
|
+
if @_req then res.push ' ', Color('lred', '(required)')
|
216
|
+
|
217
|
+
res.join ''
|
218
|
+
|
219
|
+
_requiredText: -> 'Missing required option:\n ' + @_usage()
|
220
|
+
|
221
|
+
###*
|
222
|
+
Return rejected promise with error code.
|
223
|
+
Use in .val() for return with error.
|
224
|
+
@param {Object} reject reason
|
225
|
+
You can customize toString() method and exitCode property
|
226
|
+
of reason object.
|
227
|
+
@returns {Q.promise} rejected promise
|
228
|
+
###
|
229
|
+
reject: Cmd::reject
|
230
|
+
|
231
|
+
###*
|
232
|
+
Finish chain for current option and return parent command instance.
|
233
|
+
@returns {COA.Cmd} parent command
|
234
|
+
###
|
235
|
+
end: Cmd::end
|
236
|
+
|
237
|
+
###*
|
238
|
+
Apply function with arguments in context of option instance.
|
239
|
+
@param {Function} fn
|
240
|
+
@param {Array} args
|
241
|
+
@returns {COA.Opt} this instance (for chainability)
|
242
|
+
###
|
243
|
+
apply: Cmd::apply
|
package/src/shell.coffee
ADDED
@@ -0,0 +1,10 @@
|
|
1
|
+
exports.unescape = (w) ->
|
2
|
+
w = if w.charAt(0) is '"'
|
3
|
+
w.replace(/^"|([^\\])"$/g, '$1')
|
4
|
+
else
|
5
|
+
w.replace(/\\ /g, ' ')
|
6
|
+
w.replace(/\\("|'|\$|`|\\)/g, '$1')
|
7
|
+
|
8
|
+
exports.escape = (w) ->
|
9
|
+
w = w.replace(/(["'$`\\])/g,'\\$1')
|
10
|
+
if w.match(/\s+/) then '"' + w + '"' else w
|