How to pass command line arguments to Deno?
Asked Answered
G

3

18

I have a Deno app, that I wish to pass some command line args to. I searched the manual, but found nothing.

I tried to use the same commands used in Node.js, assuming they might be sharing some for the std libraries, but it didn't work as well.

var args = process.argv.slice(2); 
// Uncaught ReferenceError: process is not defined

Any suggestions?

Galleywest answered 16/5, 2020 at 6:11 Comment(1)
Node and Deno have hardly any APIs in common. This is intentional, Deno is meant to be a fresh start. The only APIs that Deno attempts to port directly are the standards-based browser APIs that you'd find in places like developer.mozilla.org/en-US/docs/Web/APIReaction
B
15

You can use Deno.args to access the command line arguments in Deno.

To try it create a file test.ts :

console.log(Deno.args);

And run it with deno run test.ts firstArgument secondArgument

It will return you with an array of the passed args:

$ deno run test.ts firstArgument secondArgument
[ "firstArgument", "secondArgument" ]
Bersagliere answered 16/5, 2020 at 6:22 Comment(0)
T
24

You can access arguments by using Deno.args, it will contain an array of the arguments passed to that script.

// deno run args.js one two three

console.log(Deno.args); // ['one, 'two', 'three']

If you want to parse those arguments you can use std/flags, which will parse the arguments similar to minimist

import { parse } from "https://deno.land/std/flags/mod.ts";

console.log(parse(Deno.args))

If you call it with:

deno run args.js -h 1 -w on

You'll get

{ _: [], h: 1, w: "on" }
Tagmemic answered 16/5, 2020 at 7:28 Comment(1)
Why does the parser presume that -h needs a value after it, instead of just being a binary flag, like ls -l?Wharton
B
15

You can use Deno.args to access the command line arguments in Deno.

To try it create a file test.ts :

console.log(Deno.args);

And run it with deno run test.ts firstArgument secondArgument

It will return you with an array of the passed args:

$ deno run test.ts firstArgument secondArgument
[ "firstArgument", "secondArgument" ]
Bersagliere answered 16/5, 2020 at 6:22 Comment(0)
I
2

If you take a stroll through the standard library, you will find a library named flags, which sounds like it could be library for command line parsing. In the README, you will find your answer in the very first line:

const { args } = Deno;

Also, if you look at the Deno Manual, specifically the Examples section, you will find numerous examples of command line example programs that perform argument parsing, for example, a clone of the Unix cat command (which is also included in the First Steps section of the Deno Manual), where you will also find your answer in the first line:

for (let i = 0; i < Deno.args.length; i++)

So, in short: the command line arguments are a property of the global Deno object, which is documented here:

const Deno.args: string[]

Returns the script arguments to the program. If for example we run a program:

deno run --allow-read https://deno.land/std/examples/cat.ts /etc/passwd

Then Deno.args will contain:

[ "/etc/passwd" ]

Note: According to the Manual, all non-web APIs are under the global Deno namespace.

Instrumental answered 16/5, 2020 at 6:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.