Command Line Deskera API Client in Node.js | Part 2: Hello, You!

Engineering Jul 09, 2020

This is Part 2 of the series of posts on writing a basic Command Line Deskera API client in Node.js.

Part 1 — Hello, World!

Part 2 — Hello, You! (this post)

Part 3 — Call an API Endpoint

Part 4 — OAuth 2.0 Authentication

Part 5 — Show Me the Data!

(Note: The GitHub links for this part are: Browse, Zip, Diff)

Add Support of Command Line Arguments

Install yargs module as a dependency:

npm install yargs

Hello, You!

Now, update the bin/index.js file with the following code:

#!/usr/bin/env node

const yargs = require("yargs");

const args = yargs
 .usage("Usage: -n <name>")
 .option("n", { alias: "name", describe: "Your name", type: "string", demandOption: true })
 .argv;
 
const greeting = `Hello, ${args.name}!`;

console.log(greeting);

Install the updated script globally:

npm install -g .

Our script now requires an argument. If it is not provided, the script will prompt a usage error.

% hello
Usage: -n <name>
Options:
--help      Show help                            [boolean]
--version   Show version number                  [boolean]
-n, --name  Your name                            [string] [required]

Missing required argument: n

Try the command with an argument

% hello -n Deskera

Hello, Deskera!

and the version

% hello --version

0.2.0

Next up: Part 3 — Call an API Endpoint

Brajesh Sachan

Brajesh, drives direction of Deskera’s future technology and shapes Deskera as the technology leader. With his expertise and over 15 years of experience, he has significantly contributed to Deskera

Great! You've successfully subscribed.
Great! Next, complete checkout for full access.
Welcome back! You've successfully signed in.
Success! Your account is fully activated, you now have access to all content.