CLI using Deno
How to create deno CLI
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 CLI 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
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.
Comments