Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the Deno equivalent of process.argv in Node.js?

Tags:

deno

When working with NodeJS, I can pass the arguments to a Node script like this:

$ node node-server.js arg1 arg2=arg2-val arg3

And can get the arguments like so:

// print process.argv
process.argv.forEach(function (val, index, array) {
  console.log(index + ': ' + val);
});
//Output
0: node
1: /Users/umar/work/node/node-server.js
2: arg1 
3: arg2=arg2-val
4: arg3

How to get the command-line arguments in Deno?

Some experts suggested me to solve the problem by answers to the question

like image 407
DevLoverUmar Avatar asked Jun 03 '20 06:06

DevLoverUmar


1 Answers

Deno executable path ~ process.argv[0]:

Deno.execPath()

File URL of executed script ~ process.argv[1]:

Deno.mainModule

You can use path.fromFileUrl for conversions of URL to path string:

import { fromFileUrl } from "https://deno.land/[email protected]/path/mod.ts";
const modPath = fromFileUrl(import.meta.url)

Command-line arguments ~ process.argv.slice(2):

Deno.args

Example

deno run --allow-read test.ts -foo -bar=baz 42

Sample output (Windows):

Deno.execPath(): <scoop path>\apps\deno\current\deno.exe
import.meta.url: file:///C:/path/to/project/test.ts
  as path: C:\path\to\project\test.ts
Deno.args: [ "-foo", "-bar=baz", "42" ]
like image 186
ford04 Avatar answered Sep 28 '22 00:09

ford04