Enhance Your Command Line Experience with Yargs Comprehensive API Guide and Examples

Introduction to Yargs

Yargs is a powerful Node.js library that helps developers build interactive command-line tools by parsing arguments and generating an elegant user interface. It’s packed with numerous APIs that simplify the process of handling command-line arguments. Let’s dive into some of the most useful APIs provided by Yargs.

API Examples

1. Basic Usage

 const yargs = require('yargs'); const argv = yargs.argv;
console.log('Arguments:', argv); 

2. Customizing Help Messages

 const yargs = require('yargs');
yargs
  .usage('Usage: $0 <command> [options]')
  .help('h')
  .alias('h', 'help')
  .argv;

3. Adding Commands

 const yargs = require('yargs');
yargs.command({
  command: 'greet',
  describe: 'Greet a user',
  builder: {
    name: {
      describe: 'Name of the user',
      demandOption: true,
      type: 'string'
    }
  },
  handler(argv) {
    console.log(`Hello, ${argv.name}!`);
  }
}).argv; 

4. Working with Options

 const yargs = require('yargs');
yargs.options({
  name: {
    describe: 'Your name',
    demandOption: true,
    type: 'string'
  },
  age: {
    describe: 'Your age',
    demandOption: false,
    type: 'number'
  }
}).argv; 

5. Positional Arguments

 const yargs = require('yargs');
yargs.command({
  command: 'order <item>',
  describe: 'Place an order',
  builder: (yargs) => {
    return yargs.positional('item', {
      describe: 'Item to order',
      type: 'string'
    });
  },
  handler(argv) {
    console.log(`Ordering a ${argv.item}`);
  }
}).argv; 

6. Middleware

 const yargs = require('yargs');
yargs.middleware((argv) => {
  argv.timestamp = new Date();
}).argv;
console.log('Arguments with Middleware:', yargs.argv); 

7. Configuration from Files

 const yargs = require('yargs');
yargs.config('config', 'Path to configuration file', (configPath) => {
  return require(configPath);
}).argv; 

8. Example Application

Here’s a simple example application using several Yargs APIs:

 const yargs = require('yargs');
yargs
  .usage('$0 command')
  .command({
    command: 'greet',
    describe: 'Greet a user',
    builder: {
      name: {
        describe: 'Name of the user',
        demandOption: true,
        type: 'string'
      }
    },
    handler(argv) {
      console.log(`Hello, ${argv.name}!`);
    }
  })
  .command({
    command: 'order <item>',
    describe: 'Place an order',
    builder: (yargs) => {
      return yargs.positional('item', {
        describe: 'Item to order',
        type: 'string'
      });
    },
    handler(argv) {
      console.log(`Ordering a ${argv.item}`);
    }
  })
  .help()
  .argv;

This simple CLI application allows users to greet someone by name and place orders. It combines command handling, argument parsing, and help message generation.

With Yargs, you can quickly create complex command-line tools with rich interfaces, making your Node.js applications more interactive and user-friendly.

Hash: ecd460d0b4d02d8834937d9b6431adc581806abb36f5d2d368fe8155c96c4bf9

Leave a Reply

Your email address will not be published. Required fields are marked *