All Projects → nanovazquez → yargs-interactive

nanovazquez / yargs-interactive

Licence: MIT License
Interactive support for yargs

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to yargs-interactive

enquirer
Stylish, intuitive and user-friendly prompts, for Node.js. Used by eslint, webpack, yarn, pm2, pnpm, RedwoodJS, FactorJS, salesforce, Cypress, Google Lighthouse, Generate, tencent cloudbase, lint-staged, gluegun, hygen, hardhat, AWS Amplify, GitHub Actions Toolkit, @airbnb/nimbus, and many others! Please follow Enquirer's author: https://github.…
Stars: ✭ 6,523 (+16207.5%)
Mutual labels:  interactive, prompt, inquirer
requestty
An easy-to-use collection of interactive cli prompts inspired by Inquirer.js.
Stars: ✭ 158 (+295%)
Mutual labels:  interactive, prompt, inquirer
Go Prompt
Building powerful interactive prompts in Go, inspired by python-prompt-toolkit.
Stars: ✭ 4,255 (+10537.5%)
Mutual labels:  interactive, prompt
Bit
Bit is a modern Git CLI
Stars: ✭ 5,723 (+14207.5%)
Mutual labels:  interactive, prompt
Windows10debloater
Script to remove Windows 10 bloatware.
Stars: ✭ 11,462 (+28555%)
Mutual labels:  interactive, prompt
argen
Generate argument parsing logic in C from a simple config
Stars: ✭ 14 (-65%)
Mutual labels:  argument-parsing, parsed-arguments
Docker Shell
A simple interactive prompt for docker
Stars: ✭ 299 (+647.5%)
Mutual labels:  interactive, prompt
Clap
Create your command-line parser, with all of the bells and whistles, declaratively or procedurally.
Stars: ✭ 7,174 (+17835%)
Mutual labels:  argument-parsing, parsed-arguments
inquire
A Rust library for building interactive prompts
Stars: ✭ 419 (+947.5%)
Mutual labels:  interactive, prompt
Survey
A golang library for building interactive and accessible prompts with full support for windows and posix terminals.
Stars: ✭ 2,843 (+7007.5%)
Mutual labels:  interactive, prompt
Readline Sync
Synchronous Readline for interactively running to have a conversation with the user via a console(TTY).
Stars: ✭ 601 (+1402.5%)
Mutual labels:  interactive, prompt
inquirer-fuzzy-path
Fuzzy file/directory search and select prompt for Inquirer.js
Stars: ✭ 69 (+72.5%)
Mutual labels:  prompt, inquirer
Sharprompt
Interactive command line interface toolkit for C#
Stars: ✭ 197 (+392.5%)
Mutual labels:  interactive, prompt
whaaaaat
Inquirer.js port to Python (https://www.npmjs.com/package/inquirer). people blame me for staling this project. I do not have time to work on this right now - whaaaaat do you want me to do? take it offline?
Stars: ✭ 73 (+82.5%)
Mutual labels:  prompt, inquirer
PromptCLI
Interactive command line interface library
Stars: ✭ 30 (-25%)
Mutual labels:  prompt, inquirer
powerless
Minimalistic/responsive ZSH prompt inspired by powerline.
Stars: ✭ 63 (+57.5%)
Mutual labels:  prompt
auto-ls
zsh plugin for auto-ls
Stars: ✭ 77 (+92.5%)
Mutual labels:  prompt
HQ
HQ/Trivia solutions from Agora
Stars: ✭ 62 (+55%)
Mutual labels:  interactive
promptconfig
🖥 Craft a custom terminal prompt with JSON.
Stars: ✭ 32 (-20%)
Mutual labels:  prompt
git-commiter
📖✨ Allows you to commit following custom rules or conventions easily
Stars: ✭ 17 (-57.5%)
Mutual labels:  prompt

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
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].