Back to home

InstallationExample code
Yargs Parser main image

Yargs Parser

Yargs parser is an incredibly basic option parser for the command line.

This is a basic library I tend to use a lot for any of my CLI tools that I am building out.

Installation

1 2 3 4 5 # NPM npm install yargs-parser --save # or yarn yarn add yargs-parser

Example code

The main use case we will use is for options and arguments passed from the command line.

First, create an index.js file and require the following:

1 2 var argv = require("yargs-parser")(process.argv.slice(2)); console.log(argv);

Now we can already run the script from the command line, pass options and arguments and see what happens:

1 2 node index.js hello --foo=33 --bar world # prints { _: ['hello'], foo: 33, bar: 'world' }

If we disect the above command, we note that anything passed after node index.js becomes an accessible argument from our argv object under argv._. All the other options passed as the object key as a property of argv with the corresponding value passed.

Taking all this into account, the above is now accesible as follows:

1 2 3 4 var argv = require("yargs-parser")(process.argv.slice(2)); console.log(argv._[0]); // hello console.log(argv.foo); // 33 console.log(argv.bar); // world

Great! Now we can start building our Node.js commandline tools.

For now, let's just set up a basic example of how you could run a command line tool that just logs out some basic information based on the first argument passed and options.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 var argv = require("yargs-parser")(process.argv.slice(2)); const help = ` Usage $ node index.js <action> <option> Action --- hello [name] Echoes hello. Defaults to "world". friends Echoes what they are Examples $ node index.js hello $ node index.js hello --name clark $ node index.js friends `; const hello = () => { const name = argv.name ? argv.name : "clark"; console.log(`hello ${name}`); }; const friends = () => console.log("are like flowers"); /** * Run a function based on the argument provided. * */ const main = async () => { try { switch (argv._[0]) { case "hello": hello(); break; case "friends": friends(); break; default: console.log(help); break; } } catch (err) { console.error(err); } }; // Run the program main();

Now we can run our tool above!

1 2 3 4 5 6 7 8 node index.js hello # prints hello clark node index.js hello --name tim # prints hello tim node index.js hello --name=bob # prints hello bob node index.js friends # prints are like flowers

Get creative and start building some useful Node.js CLI tools for your workflow.

Code can be found on my Github.

Hello is a series that is about short, sharp examples. Read more on this series to find small gems to add your toolset.

Dennis O'Keeffe

@dennisokeeffe92
  • Melbourne, Australia

Hi, I am a professional Software Engineer. Formerly of Culture Amp, UsabilityHub, Present Company and NightGuru.
I am currently working on workingoutloud.dev, Den Dribbles and LandPad .

Related articles


1,200+ PEOPLE ALREADY JOINED ❤️️

Get fresh posts + news direct to your inbox.

No spam. We only send you relevant content.