Skip to content

How to Pass Command Line Arguments to Deno

Deno, Command Line, Javascript2 min read

In modern software development, knowing how to efficiently work with command line arguments is crucial. In this article, we will explore how to pass command line arguments to Deno, the secure runtime for JavaScript and TypeScript. By the end of this guide, you will have a comprehensive understanding of how to handle command line arguments within your Deno scripts and applications.

What are Command Line Arguments?

Command line arguments are parameters passed to a program at the time of execution. These arguments provide a way to influence the behavior of a script or application without modifying its source code. They are commonly used to customize the functionality of a program, such as specifying input files, toggling options, or providing configuration settings.

Basic Syntax for Passing Command Line Arguments to Deno

In Deno, passing command line arguments is straightforward. When you run a script using the deno command, any additional arguments provided after the script name are automatically accessible within the script through the global Deno.args array. Here's a basic example demonstrating this concept:

1// hello.js
2console.log("Hello, " + Deno.args[0]);

If you execute the above script using the following command:

1deno run hello.js World

You will see the output:

1Hello, World

In this example, the argument "World" is accessed within the script via Deno.args[0], which represents the first command line argument passed to the script.

Accessing Multiple Command Line Arguments

To access multiple command line arguments passed to a Deno script, you can simply use array indexing to retrieve specific arguments from the Deno.args array. Consider the following example:

1// greet.js
2console.log("Hello, " + Deno.args[0]);
3console.log("Your age is " + Deno.args[1]);

When executing the script with the following command:

1deno run greet.js Alice 25

The output will be:

1Hello, Alice
2Your age is 25

In this case, Deno.args[0] corresponds to the first argument "Alice", and Deno.args[1] corresponds to the second argument "25".

Handling Command Line Options and Flags

Apart from regular arguments, command line options and flags can also be passed to Deno scripts. These are typically preceded by a dash (-) or double-dash (--) and are commonly used to toggle specific settings or provide additional context to a script. To process command line options and flags, you can utilize libraries like std/flags that come bundled with Deno. Here's a simple example using the flags module to handle options:

1// options.js
2import { parse } from "";
4const args = parse(Deno.args);
5if (args.h || {
6 console.log("Usage: deno run options.js --name=your_name --age=your_age");
7} else {
8 console.log("Hello, " +;
9 console.log("Your age is " + args.age);

Executing the script with the following command:

1deno run options.js --name=Alice --age=25

Will produce the output:

1Hello, Alice
2Your age is 25

In this example, the parse function from the flags module is used to extract named options (--name, --age) along with their corresponding values from the command line arguments.

By leveraging the features inherent to Deno and its standard library, you can seamlessly integrate command line argument handling into your JavaScript and TypeScript projects, enhancing their flexibility and usability.