Our CLI app works, but it sure would be awesome if it had some additional features. In this post, we’ll add support for command-line options, JSON output, and automatic copy-to-the-clipboard. Let’s dive right in!

If you’re just jumping on, you can check out the previous parts here: Part 1, Part 2, Part 3, and Part 4.

Where We Are and Where We’re Going

Let’s go ahead and install our CLI app so that we can call it from anywhere. All we need to do is run the following command in the same directory as our package.json file:

npm i -g

After that, we can then call our new generate-address command from anywhere:

> generate-address.cmd
284 Byron Rd Sw
Byron Center MI 49315-9516
(42.8099289, -85.79006690000001)

Pretty cool! But not super-useful if we want to access this data programmatically. JSON would be so much better.

What I’d like to do is pass a parameter that tells the app which format to give me:

> generate-address.cmd --text
284 Byron Rd Sw
Byron Center MI 49315-9516
(42.8099289, -85.79006690000001)

> generate-address.cmd --json
{"street":"1127 Autumn Leaf Ct","city":"Carson","state":"CA","zip":"90746-7454","latitude":33.8703186,"longitude":-118.2528222}

Another nice-to-have: to automatically place the result onto my clipboard. That will save me a few seconds of having to select the JSON and copy it manually.

Building in these capabilities is easy! Why? Because we can leverage some of the great packages that are out there and waiting for us on NPM already!

Let’s get started!

Parsing Command Line Options Like a Pirate

Our first task will be to parse arguments that are passed to our app. And for that, we’ll be using YARGS. I mean, yargs. o

Yargs be a node.js library fer hearties tryin’ ter parse optstrings.

A pirate-themed command-line parsing app. How cool is that??

We’ll need to install the new package…

npm i yargs --save

There’s a lot that we could do with yargs. I highly recommend you check out the docs if you are doing anything non-trivial, because it can do a lot of the heavy-lifting for you!

But this app is simple. We really don’t need much. Parsing our arguments is as simple as this in our index.js:

#!/usr/bin/env node
const AddressCreator = require('./AddressCreator');
const argv = require('yargs').argv

AddressCreator.getRandomAddress().then(x => {
    console.log(x.street);
    console.log(`${x.city} ${x.state} ${x.zip}`);
    console.log(`(${x.latitude}, ${x.longitude})`);
});

Pretty simple!

Optionally Outputting JSON Data

Now that we’re parsing arguments, we can change our app to optionally output JSON data if the --json option is specified:

#!/usr/bin/env node
const AddressCreator = require('./AddressCreator');
const argv = require('yargs').argv

AddressCreator.getRandomAddress().then(x => {

    if (argv.json) {
        console.log(JSON.stringify(x));
    } else {
        console.log(x.street);
        console.log(`${x.city} ${x.state} ${x.zip}`);
        console.log(`(${x.latitude}, ${x.longitude})`);
    }
});

So, if we reinstall and run our app with the --json flag now…

> generate-address.cmd --json
{"street":"1127 Autumn Leaf Ct","city":"Carson","state":"CA","zip":"90746-7454","latitude":33.8703186,"longitude":-118.2528222}

Easy!

Copying to the Clipboard with node-copy-paste

So what about copying data to the clipboard? Once again, there’s a package for that! Actually there are several, but we’ll use copy-paste.

The package has a very simple API:

var ncp = require("copy-paste");

ncp.copy('some text', function () {
  // complete...
})

You probably know what we need to do now.

Install the package:

npm i copy-paste --save

Import the package:

const ncp = require("copy-paste");

And if our app is called with the copy flag, copy the output to our clipboard!

    if (argv.copy) {
        ncp.copy(output);
    }

Putting it all together:

#!/usr/bin/env node
const AddressCreator = require('./AddressCreator');
const argv = require('yargs').argv
const ncp = require("copy-paste");

AddressCreator.getRandomAddress().then(x => {

    const output = argv.json ? 
        JSON.stringify(x) : 
        `${x.street}\r\n${x.city} ${x.state} ${x.zip}\r\n(${x.latitude}, ${x.longitude})`;

    if (argv.copy) {
        ncp.copy(output);
    }

    console.log(output);

});

Now we can reinstall our app (*using npm i -g in our project’s folder!), we can do cool things like this:

> generate-address.cmd
3532 Mcculloch Blvd N
Lk Havasu Cty AZ 86406-4124
(34.4968078, -114.2867361)

> generate-address.cmd --json
{"street":"2021 Pine Needle Trl","city":"Kissimmee","state":"FL","zip":"34746-3092","latitude":28.2154591,"longitude":-81.4240137}

> generate-address.cmd --json --copy
{"street":"330 Hearst Ave","city":"San Francisco","state":"CA","zip":"94112-1349","latitude":37.7309134,"longitude":-122.4450761}
# The JSON blob is now on the keyboard!

Final Thoughts

So that’s really it! I’ve posted the final version of this app on Github, so feel free to grab the code if you think it’s useful.

Node CLI apps are easy to build, and thanks to the wealth of npm packages available, it’s quite easy to build useful apps. Be sure to keep that in mind. You’d be surprised how much time a simple CLI app might save you.

Happy coding!