-
Notifications
You must be signed in to change notification settings - Fork 43
/
js-tutorial.dox
430 lines (419 loc) · 12 KB
/
js-tutorial.dox
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
// -*- mode: c++; coding: utf-8; -*- vim:ts=2:sw=2:expandtab:autoindent:filetype=cpp:enc=utf-8:
/*
The MIT License
Copyright (c) 2008, 2009 Flusspferd contributors (see "CONTRIBUTORS" or
http://flusspferd.org/contributors.txt)
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
/**
* @page js-tutorial Using Flusspferd from Javascript
*
* It is possible to use Flusspferd without ever touching C++ - and even if
* you do, you will probably use some of our Javascript-side features.
*
* @section Contents
*
* - @subpage js-tutorial-invoke
* - @subpage js-tutorial-io
* - @subpage js-tutorial-xml
* - @subpage js-tutorial-getopt
*
* @see
* - @ref cpp-tutorial-modules
*/
/**
* @page js-tutorial-invoke Using the Flusspferd shell / interpreter
*
* Once Flusspferd is @ref install "installed", you can run %flusspferd like
* this:
*
* @verbatim
$ flusspferd
@endverbatim
*
* It will open up a shell. You can use it to evaluate arbitrary Javascript
* expressions.
*
* @verbatim
> 1 + 1
2
> world = "Shell"; print("Hello", world);
Hello Shell
@endverbatim
*
* You can also invoke %flusspferd to run a Javascript file:
*
* @verbatim
$ flusspferd source.js
@endverbatim
*
* For more options, run <code>%flusspferd -h</code>.
*
* @section cmdarg Command line arguments
*
* Arguments given on the command line after the script name are available as
* a normal JavaScript array on the system module called @c args. For
* example:
*
* @verbatim
#!/usr/bin/env flusspferd
function print_args(args) {
// args.length is equivalent to argc in c
print("There are", args.length, "argument(s)");
for (var i=0; i<args.length; i++) {
print(args[i]);
}
}
var system = require('system');
print_args(system.args);
@endverbatim
*
* @section js-tutorial-emacs Using inferior-flusspferd-mode
* Flusspferd comes with an
* <a href="http://www.gnu.org/software/emacs/">Emacs</a> mode.
* The Flusspferd mode can be used to interactively develop
* javascript code by sending it to the Flusspferd shell.
*
* See \ref emacs-install "Emacs Mode install instructions"
* on how to use it.
*
* Next: \ref js-tutorial-io
*
*/
/**
* @page js-tutorial-io Using io
*
* Previous: \ref js-tutorial-invoke
*
* There is an I/O function that you have probably already encountered:
* @c print.
*
* @verbatim
> print("OK: ", [1,2,3,4])
OK: 1 2 3 4
> require("system").stdout.print("Same.")
Same.
@endverbatim
*
* In fact, @c print is a short form for the latter syntax. However, the print
* abbreviation is not available in modules. To spare you the hassle of typing
* require("system").stdout.print everytime, you can assign the module to a
* variable.
*
* @verbatim
const sys = require("system");
sys.stdout.print("Works!");
@endverbatim
*
* The other global function that is exposed as part of @c prelude.js is
* @c readLine - which reads a line from standard input.
*
* @verbatim
> readLine().substr(1)
abcd
bcd
@endverbatim
*
* (Where @c abcd is input.)
*
* Again, @c readLine is just an alias for @c require("system").stdin.readLine.
*
* The @c system module exposes three streams by default:
*
* - @c stdin
* - @c stdout
* - @c stderr
*
* There are many more things that you can do with this and the 'io' module,
* see <a href="js/IO">the documentation for <code>io</code></a>.
*
* Next: \ref js-tutorial-xml
*
*/
/**
* @page js-tutorial-xml Using XML
*
* Previous: \ref js-tutorial-io
*
* Let us begin by loading an XML document from Jeni Tennison's website:
*
* @verbatim
> XML = require("XML")
> doc = XML.parse("http://www.jenitennison.com/index.xml")
<?xml version="1.0" encoding="UTF-16"?>
<?xml-stylesheet type="text/xsl" href="resources/style/page.xsl"?>
<my:doc xmlns:my="http://www.jenitennison.com/" xmlns="http://www.w3.org/1999/xhtml" ...>
...
</my:doc>
@endverbatim
*
* @note You may notice the encoding of UTF-16 above, because @c doc.toString()
* (which is implicitly called here) converts to a Javascript string, and
* in Javascript, strings are encoded in UTF-16, internally.
*
* Now let's look at that document in a few basic ways.
*
* @verbatim
> doc.rootElement
ELEMENT:doc
> doc.rootElement.firstChild = null // => delete document contents
null
> doc
<?xml version="1.0" encoding="UTF-16"?>
<?xml-stylesheet type="text/xsl" href="resources/style/page.xsl"?>
<my:doc xmlns:my="http://www.jenitennison.com/" xmlns="http://www.w3.org/1999/xhtml" ... />
> doc.firstChild
PI:xml-stylesheet
> doc.type
DOCUMENT
@endverbatim
*
* Or, leveraging XPath:
*
* @verbatim
> doc = XML.parse("http://www.jenitennison.com/index.xml")
...
> xpath = new XML.XPath(doc, {my: "http://www.jenitennison.com/"})
[object NativeObject]
> xpath.ns.html = "http://www.w3.org/1999/xhtml" // now, namespaces html and my can be accessed
http://www.w3.org/1999/xhtml
> links = xpath("//my:links/my:link")
ELEMENT:link,ELEMENT:link,ELEMENT:link,ELEMENT:link
> links.forEach(function(x) { print(x.getAttribute("href"), x.content); })
www.w3.org/XML W3C XML Page
www.xml.com XML.com
www.xml.org XML.org from OASIS
www.bayes.co.uk/xml Chris Bayes' XML Portal
@endverbatim
*
* @note Reference documentation has not been written yet.
*
* Next: \ref js-tutorial-getopt
*
*/
/**
* @page js-tutorial-getopt Using Getopt
*
* Previous: \ref js-tutorial-xml
*
* Flusspferd provides with getopt a module to parse command line parameters.
* The module has a C++ and a Javascript API and is also used by the
* Flusspferd Shell. See flusspferd::getopt for the reference.
*
* Getopt is similar to alternatives known from other languages, like
* <a href="http://man.cx/getopt(3)">getopt(3)</a>. The getopt function
* takes a specification which options are supported and returns a list of
* parsed options. If no second parameter containing an Array of command line
* input is passed it is read from the environment.
*
* The parameter specification is an object consisting of parameter names as properties.
* Those properties contain objects defining different aspects of the parameter. So the
* specification is basically a JSON structure. A very simple specification thus might look
* like this:
*
* @verbatim
var spec = {
"help" : { },
"version" : { },
"foo" : { },
"bar" : { }
};
@endverbatim
*
* getopt uses the following syntax for command line parameters:
*
* Short option (one character long): @p -S
*
* Long option: @p --longoption
*
* If an option is short or long is determined by the length of the name. In our example all options
* are long options. It is recommended to always provide a descriptive long option and provide a short
* alias for common options. This brings us to the next point: Providing aliases. You can define several
* aliases for each option by adding an @p "alias" option to the subobjects:
*
* @verbatim
var spec = {
"help" : {
"alias" : "h"
},
"version" : {
"alias" : "v"
},
"foo" : {
"alias" : ["f", "F"]
},
"bar" : {
"alias" : ["baz", "b" ]
}
};
@endverbatim
*
* getopt also supports arguments to those options. The following syntaxes are supported:
*
* Short option with argument: @p -Sargument or @p -S @p argument
*
* Long option with argument: @p --longoption=argument or @p --longoption @p argument
*
* You can specify that an option requires an argument with an @p "argument"
* parameter which should be either @p "none", @p "optional" or @p "required"
* and you can define an argument type with @p "argument_type".
* The @p "argument_type" is currently useful to improve the help output and
* for a bash completion script. We get back to this later.
*
* @verbatim
var spec = {
"help" : {
"alias" : "h"
},
"version" : {
"alias" : "v"
},
"foo" : {
"alias" : ["f", "F"],
"argument" : "required",
"argument_type" : "file"
},
"bar" : {
"alias" : ["baz", "b" ],
"argument" : "optional"
// an argument_type does not need to be set
}
};
@endverbatim
*
* Now we can parse the command line parameters with getopt:
*
* @verbatim
var getopt = require('getopt');
var result = getopt.getopt(spec);
@endverbatim
*
* @p result now is an object containing the parsed options as property names
* which contain a list of arguments:
*
* @verbatim
{
"option0" : [ "arg0", "arg1" ],
"option1" : [ "arg0" ],
"_" : [ "rest" ]
};
@endverbatim
*
* The property @p "_" is a special case containing the unparsed arguments.
*
* Now you could process result. But getopt provides an even easier way by using
* callbacks. You can define callbacks inside the specifications:
*
* @verbatim
var spec = {
"help" : {
"alias" : "h",
"callback" : function() {
print("help");
quit();
}
},
"version" : {
"alias" : "v",
"callback" : function() {
print("Version 1.0");
quit();
}
},
"foo" : {
"alias" : ["f", "F"],
"argument" : "required",
"argument_type" : "file",
"callback" : function(name, argument) {
print("option '" + name + "' argument '" + argument + "'");
}
},
"bar" : {
"alias" : ["baz", "b" ],
"argument" : "optional",
"callback" : function(name, argument) {
if(argument) {
print("option '" + name + "' called with argument '" + argument + "'");
}
else {
print("option '" + name + "' called without argument");
}
}
}
};
@endverbatim
*
* @p "help" is a common option and every program using command line options
* should provide it to list the supported options. getopt can even help
* with this option since getopt already know the supported options. There is
* a getopt_help function taking the parameter specification and generating a
* corresponding output. To improve the output you should add a @p "doc"
* property to all options.
*
* @verbatim
var spec = {
"help" : {
"alias" : "h",
"doc" : "Displays this message.",
"callback" : function() {
print("usage: getopt-example.js [OPTIONS] ...");
print(getopt.getopt_help(spec));
quit();
}
},
"version" : {
"alias" : "v",
"doc" : "Show version number.",
"callback" : function() {
print("Version 1.0");
quit();
}
},
"foo" : {
"alias" : ["f", "F"],
"argument" : "required",
"argument_type" : "file",
"doc" : "Foo!",
"callback" : function(name, argument) {
print("option '" + name + "' argument '" + argument + "'");
}
},
"bar" : {
"alias" : ["baz", "b" ],
"argument" : "optional",
"doc" : "Bar is the way to go.",
"callback" : function(name, argument) {
if(argument) {
print("option '" + name + "' called with argument '" + argument + "'");
}
else {
print("option '" + name + "' called without argument");
}
}
}
};
@endverbatim
*
* getopt even provides functions to auto generate parts of a manpage
* (flusspferd::getopt_man) and bash completion script
* (flusspferd::getopt_bash).
*
* See @p help/getopt-example.js or @p misc/replace-var.js for examples on
* the usage of getopt with Javascript or take a look at
* @p src/programs/flusspferd.cpp for the getopt usage with C++.
*
*/