yargs vs inquirer
Command Line Argument Parsing Libraries
yargsinquirerSimilar Packages:
Command Line Argument Parsing Libraries

Inquirer and Yargs are both popular npm packages used for building command-line interfaces (CLIs) in Node.js applications. Inquirer is primarily focused on interactive prompts and user input, allowing developers to create engaging command-line experiences with various question types. Yargs, on the other hand, is designed for parsing command-line arguments and options, providing a straightforward way to handle command-line inputs and create robust command-line applications. While both libraries serve the purpose of enhancing CLI functionality, they cater to different aspects of user interaction and input handling.

Npm Package Weekly Downloads Trend
3 Years
Github Stars Ranking
Stat Detail
Package
Downloads
Stars
Size
Issues
Publish
License
yargs125,333,18411,396231 kB3016 months agoMIT
inquirer36,340,44221,23948.3 kB452 days agoMIT
Feature Comparison: yargs vs inquirer

User Interaction

  • yargs:

    Yargs focuses on parsing command-line arguments rather than user interaction. It provides a simple API for defining commands, options, and flags, allowing developers to create command-line tools that can accept various inputs. While it does not offer interactive prompts, it can validate and process user inputs efficiently.

  • inquirer:

    Inquirer excels in creating interactive command-line applications by providing a variety of prompt types, such as input, confirm, list, checkbox, and more. This allows developers to engage users effectively and gather input in a user-friendly manner. It supports asynchronous prompts, enabling a smooth flow of interaction without blocking the event loop.

Argument Parsing

  • yargs:

    Yargs provides powerful argument parsing capabilities, allowing developers to define commands, options, and their expected types. It automatically generates help documentation and supports features like default values, required options, and command validation, making it a robust choice for command-line applications.

  • inquirer:

    Inquirer does not handle argument parsing directly; it is designed for interactive user input. Developers typically use it in conjunction with other libraries for parsing command-line arguments, as its primary focus is on prompting users rather than processing command-line inputs.

Ease of Use

  • yargs:

    Yargs is also user-friendly, providing a clear and concise syntax for defining commands and options. Its built-in help generation and validation features simplify the process of creating command-line tools, allowing developers to focus on functionality rather than argument handling.

  • inquirer:

    Inquirer is straightforward to use, with a simple API that allows developers to create prompts with minimal configuration. It abstracts away the complexities of handling user input, making it easy for developers to implement interactive features without extensive boilerplate code.

Customization

  • yargs:

    Yargs offers customization options for command-line options and commands, including the ability to define custom help messages, aliases, and default values. However, its customization is more focused on command structure rather than user interaction.

  • inquirer:

    Inquirer allows for significant customization of prompts, including the ability to define custom validation functions, modify prompt styles, and create complex question flows. This flexibility makes it suitable for applications that require tailored user experiences.

Community and Ecosystem

  • yargs:

    Yargs also has a robust community and is frequently updated. It is well-documented and widely adopted for building command-line tools, making it a reliable choice for developers looking for a comprehensive argument parsing solution.

  • inquirer:

    Inquirer has a strong community and is widely used in various projects, especially for CLI tools that require user interaction. It has numerous plugins and extensions available, enhancing its capabilities and integration with other libraries.

How to Choose: yargs vs inquirer
  • yargs:

    Choose Yargs if your application needs to parse command-line arguments and options. It is well-suited for building command-line tools that require complex argument handling, such as subcommands, flags, and validation.

  • inquirer:

    Choose Inquirer if your application requires interactive prompts and user input. It is ideal for scenarios where you need to ask users questions and gather their responses in a structured manner, such as in setup scripts or configuration wizards.

README for yargs

Yargs

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


ci NPM version js-standard-style Coverage Conventional Commits

Description

Yargs helps you build interactive command line tools, by parsing arguments and generating an elegant user interface.

It gives you:

  • commands and (grouped) options (my-program.js serve --port=5000).
  • a dynamically generated help menu based on your arguments:
mocha [spec..]

Run tests with Mocha

Commands
  mocha inspect [spec..]  Run tests with Mocha                         [default]
  mocha init <path>       create a client-side Mocha setup at <path>

Rules & Behavior
  --allow-uncaught           Allow uncaught errors to propagate        [boolean]
  --async-only, -A           Require all tests to use a callback (async) or
                             return a Promise                          [boolean]
  • generate completion scripts for Bash and Zsh for your command
  • and tons more.

Installation

Stable version:

npm i yargs

Bleeding edge version with the most recent features:

npm i yargs@next

Usage

Simple Example

#!/usr/bin/env node
import yargs from 'yargs';
import { hideBin } from 'yargs/helpers';
const argv = yargs(hideBin(process.argv)).parse()

if (argv.ships > 3 && argv.distance < 53.5) {
  console.log('Plunder more riffiwobbles!')
} else {
  console.log('Retreat from the xupptumblers!')
}
$ ./plunder.js --ships=4 --distance=22
Plunder more riffiwobbles!

$ ./plunder.js --ships 12 --distance 98.7
Retreat from the xupptumblers!

Note: hideBin is a shorthand for process.argv.slice(2). It has the benefit that it takes into account variations in some environments, e.g., Electron.

Complex Example

#!/usr/bin/env node
import yargs from 'yargs';
import { hideBin } from 'yargs/helpers';

yargs(hideBin(process.argv))
  .command('serve [port]', 'start the server', (yargs) => {
    return yargs
      .positional('port', {
        describe: 'port to bind on',
        default: 5000
      })
  }, (argv) => {
    if (argv.verbose) console.info(`start server on :${argv.port}`)
    serve(argv.port)
  })
  .option('verbose', {
    alias: 'v',
    type: 'boolean',
    description: 'Run with verbose logging'
  })
  .parse()

Run the example above with --help to see the help for the application.

Supported Platforms

TypeScript

yargs has type definitions at @types/yargs.

npm i @types/yargs --save-dev

See usage examples in docs.

Deno

As of v16, yargs supports Deno:

import yargs from 'https://deno.land/x/yargs@v17.7.2-deno/deno.ts'
import { Arguments } from 'https://deno.land/x/yargs@v17.7.2-deno/deno-types.ts'

yargs(Deno.args)
  .command('download <files...>', 'download a list of files', (yargs: any) => {
    return yargs.positional('files', {
      describe: 'a list of files to do something with'
    })
  }, (argv: Arguments) => {
    console.info(argv)
  })
  .strictCommands()
  .demandCommand(1)
  .parse()

Note: If you use version tags in url then you also have to add -deno flag on the end, like @17.7.2-deno

Usage in Browser

See examples of using yargs in the browser in docs.

Documentation

Table of Contents

Supported Node.js Versions

Libraries in this ecosystem make a best effort to track Node.js' release schedule. Here's a post on why we think this is important.