Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

yargs-interactive

Package Overview
Dependencies
Maintainers
1
Versions
11
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

yargs-interactive

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

  • 1.1.1
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
9.6K
decreased by-14.07%
Maintainers
1
Weekly downloads
 
Created
Source

Yargs Interactive npm Build Status Coverage Status semantic-release

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 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: 'nano', 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. result.name)
  });

Basic usage

Note: See more usage examples in the examples folder.

Usage

It supports the following use cases

Prompt questions with default values (full-interactive)

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 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, just 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

What type of prompts are supported? It provides all prompt types supported by Inquirer.

Prompt just some questions (mixed mode)

You can opt-out options from interactive mode by setting the prompt property to false.

my-cli.js

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

const options = {
  name: {
    type: 'input',
    name: 'nano',
    describe: 'Enter your name'
  },
  likesPizza: {
    type: 'confirm',
    default: false,
    describe: 'Do you like pizza?',
    prompt: false // because everyone likes pizza
  },
};

yargsInteractive()
  .usage('$0 <command> [args]')
  .interactive(options)
  .then((result) => {
    // The tool will prompt questions for all options and will output your answers.
    // You can opt-out options by using `prompt: false`. 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 --name='Johh' --interactive

No prompt at all (ye olde yargs)

my-cli.js

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

const options = {
  name: {
    type: 'input',
    name: '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

Keywords

FAQs

Package last updated on 03 Mar 2018

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc