How to create CLI scripts in Deno Part I


Deno is asynchronous JavaScript/TypeScript runtime. If you are not sure how and what’s of Deno, here is the links

CLI / command line script

With Deno you can make beautiful scripts with command line arguments and install them locally in your system, even an executable is plausible.

catching the command line

So how do we catch command line arguments, for example

sayHi manoj

The above CLI supposed to say hi to first argument(manoj) , hi manoj. Now you under stand what an argument in command line is .

Let’s build a simple command line program with Deno

 //app.ts
const {args:[name]}=Deno
console.log(`hi ${name}`);

and you can run it as

deno run app.ts Joe
//result
hi jol

How to catch multiple argument separated by white space ? Here is a quick inundation to our script.

const {args:[hus,wife]}=Deno
console.log(`hi ${hus} ,${wife}`);

Install the script locally

We can install our script locally using the deno install command so that can run without a deno command along shell.

deno install --unstable -f -n sayHi app.ts

Here -f is used to overwrite the existing file which is optional , -n is used to specify the name of the command which also optional (it will take the name of the .ts file instead, if no name is specified). The install command will place the file in your deno home directory.

Run the command

Running the command is as simple as running dir in shell.

sayHi Geethu

We can build much more advanced CLI with options and commands using some of the interesting deno CLI framework. I leave it to the second part.

Published by

Manoj

A developer,teacher and a blogger obsessed with Python,Dart and open source world from India

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.