Content deleted Content added
→Programming languages: Remove Bash section because the code doesn't show any parsing and is dangerous on top of that |
documented command-line argument parsing in Node.js |
||
Line 125:
$ racket /tmp/c -nfe 8
8-(
</source>
===Node.js===
[[Javascript]] programs written for [[Node.js]] use the <code>process.argv</code> global variable.<ref>{{cite web|title=process.argv|work=Node.js v10.16.3 Documentation|url=https://nodejs.org/docs/latest-v10.x/api/process.html#process_process_argv|accessdate=3 October 2019}}</ref>
<source lang="javascript">
// argv.js
console.log(process.argv);
</source>
<source lang="bash">
$ node argv.js one two three four five
[ 'node',
'/home/avian/argvdemo/argv.js',
'one',
'two',
'three',
'four',
'five' ]
</source>
[[Node.js]] programs are invoked by running the interpreter node interpreter with a given file, so the first two arguments will be <code>node</code> and the name of the javascript source file. It is often useful to extract the rest of the arguments by slicing a sub-array from <code>process.argv</code>.<ref>{{cite web|title=How to parse command line arguments|work=Node.js Foundation Documentation|accessdate=3 October 2019|url=https://nodejs.org/en/knowledge/command-line/how-to-parse-command-line-arguments/}}</ref>
<source lang="javascript">
// process-args.js
console.log(process.argv.slice(2));
</source>
<source lang="bash">
$ node process-args.js one two=three four
[
'one',
'two=three',
'four' ]
</source>
|