How to create CLI scripts in Deno Part II:cliffy


If you miss the first part here is the links

cliffy

This is a third party CLI framework for deno which will help you to build command line application using,

  • options
  • commands
  • event handlers
  • sub commands

For more info and example visit the official repository. To explore the frame work go through the example. The repo lack documentation, I know but the developer provide a good set of examples.

Greet CLI

Here is a quick example.

 import { Command } from "https://deno.land/x/cliffy@v0.17.0/command/mod.ts";

new Command()
    .name('Greet')
    .version('0.0.1')
    .description('Greet')
    .example('Greeting people',"greet -g good-night")  
    .option("-g --msg <msg:string>","Greeting with no space",{
        default:'Good morning' ,               
        action:async({msg})=>{
            console.log(msg);
        }
    })
    .parse(Deno.args)

The above is simple greet program with Cliffy framework, which use option for -g for message, also have default value for the option

Run the script in deno

//run the script
deno run app.ts -g good-evening
//output 
good-evening
//bring help
deno run app.ts -h

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 greet 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.

greet -g Good-evening
//try bring some help
greet -h
greet -anything

A complete CLI project for Deno users is hosted at GitHub, have a look.

You may love to read these Deno posts

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.