Yargs Interactive
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!).
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');
yargsInteractive
.usage('$0 <command> [args]')
.interactive()
.then((result) => {
});
});
Finally, call your CLI from the terminal. You can see some example CLIs using this library here.
Usage
It supports the following use cases
Full interactive (prompt questions with default values)
my-cli.js
const yargsInteractive = require('../bin/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) => {
console.log(result)
});
Usage in terminal
➜ node my-cli.js --interactive
Note: See more usage examples here.
What type of prompts are supported
It provides all prompt types supported by Inquirer.
Prompt just some options (mixed mode)
You can opt-out options from interactive mode by setting the prompt
property to false
.
my-cli.js
const yargsInteractive = require('../bin/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
},
};
yargsInteractive()
.usage('$0 <command> [args]')
.interactive(options)
.then((result) => {
console.log(result);
});
Usage in terminal
➜ node my-cli.js --name='Johh' --interactive
Note: See more usage examples here.
No prompt at all (ye olde yargs)
my-cli.js
const yargsInteractive = require('../bin/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) => {
console.log(result);
});
Usage in terminal
➜ node my-cli.js --name='Johh' --likesPizza
Note: See more usage examples here.