npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

yargs-interactive

v3.0.1

Published

Build interactive command line tools without worring to parse the arguments (or ask them).

Downloads

37,563

Readme

Yargs Interactive

Build Status Coverage Status semantic-release npm npm

Read the blog post

Interactive (prompt) support for yargs, based on inquirer. Useful for using the same CLI for both for humans and non-humans (like CI tools). Also supports mixed mode (yay!).

Yargs Interactive

This tool helps you to build command line tools without worring to parse arguments, or develop the logic to ask them.

Installation

npm install -S yargs-interactive

Then, add this code in your CLI code to get all the arguments parsed:

#!/usr/bin/env node

const yargsInteractive = require("yargs-interactive");
const options = {
  name: { type: "input", default: "A robot", describe: "Enter your name" },
  likesPizza: { type: "confirm", default: false, describe: "Do you like pizza?" }
};

yargsInteractive()
  .usage("$0 <command> [args]")
  .interactive(options)
  .then(result => {
    // Your business logic goes here.
    // Get the arguments from the result
    // e.g. myCli(result.name);
    console.log(`\nResult is:\n` + `- Name: ${result.name}\n` + `- Likes pizza: ${result.likesPizza}\n`);
  });

Now, by simply wrapping your CLI code with this tool, you'll get all the information you need from the user. For instance, save the previous snipped in a file named my-cli.js and run it in your terminal:

➜ node my-cli.js --interactive

Basic usage

Note: See other CLI examples in this folder.

Usage

It supports the following use cases

Prompt questions (full-interactive)

my-cli.js

const yargsInteractive = require("yargs-interactive");

const options = {
  name: {
    type: "input",
    describe: "Enter your name"
  },
  likesPizza: {
    type: "confirm",
    describe: "Do you like pizza?"
  }
};

yargsInteractive()
  .usage("$0 <command> [args]")
  .interactive(options)
  .then(result => {
    // The tool will prompt questions and will output your answers.
    // TODO: Do something with the result (e.g result.name)
    console.log(result);
  });

Usage in terminal

➜ node my-cli.js --interactive

If you want to use interactive mode always, avoiding the need of sending it as an argument, set the --interactive parameter to true by default:

const options = {
  interactive: { default: true },
  ...
};

yargsInteractive()
  .usage('$0 <command> [args]')
  .interactive(options)
  .then((result) => {
    // The tool will prompt questions and will output your answers.
    // TODO: Do something with the result (e.g result.name)
    console.log(result)
  });

And then simply call your CLI with no parameters.

➜ node my-cli.js

Options

| Property | Type | Description | | -------- | ------ | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | type | string | (Required) The type of the option to prompt (e.g. input, confirm, etc.). We provide all prompt types supported by Inquirer. | | describe | string | (Required) The message to display when prompting the option (e.g. Do you like pizza?) | | default | any | The default value of the option. | | prompt | string | (Default is if-empty) Property to decide whether to prompt the option or not. Possible values: always, never, if-no-arg (prompts if the option was not sent via command line parameters) and if-empty (prompts if the value was not sent via command line parameters and it doesn't have a default property). |

Prompt some questions (mixed mode)

You can opt-out options from interactive mode by setting the prompt property to never. By default, its value is if-empty, prompting the question to the user if the value was not set via command line parameters, or if it doesn't have a default property. Setting it to if-no-arg will prompt the question if no argument is provided. Lastly, you can use always to always prompt the option.

my-cli.js

const yargsInteractive = require("yargs-interactive");

const options = {
  name: {
    // prompt property, if not set, defaults to 'if-empty'
    // In this case, it means the question will be prompted
    // if it is not provided by args, as it doesn't have a default value.
    type: "input",
    describe: "Enter your name"
  },
  likesPizza: {
    type: "confirm",
    default: false,
    describe: "Do you like pizza?",
    prompt: "never" // because everyone likes pizza
  }
};

yargsInteractive()
  .usage("$0 <command> [args]")
  .interactive(options)
  .then(result => {
    // The tool will prompt questions output the answers.
    // You can opt-out options by using `prompt: 'never'`. For these properties, it
    // will use the value sent by parameter (--likesPizza) or the default value.
    // TODO: Do something with the result (e.g result.name)
    console.log(result);
  });

Usage in terminal

➜ node my-cli.js --interactive

Notice that if you enter node my-cli.js --name='Johh' --interactive name won't be prompted either (as by default it uses if-empty).

No prompt at all (ye olde yargs)

my-cli.js

const yargsInteractive = require("yargs-interactive");

const options = {
  name: {
    type: "input",
    default: "nano",
    describe: "Enter your name"
  },
  likesPizza: {
    type: "confirm",
    default: false,
    describe: "Do you like pizza?"
  }
};

yargsInteractive()
  .usage("$0 <command> [args]")
  .interactive(options)
  .then(result => {
    // The tool will output the values set via parameters or
    // the default value (if not provided).
    // TODO: Do something with the result (e.g result.name)
    console.log(result);
  });

Usage in terminal

➜ node my-cli.js --name='Johh' --likesPizza