
Security News
Crates.io Implements Trusted Publishing Support
Crates.io adds Trusted Publishing support, enabling secure GitHub Actions-based crate releases without long-lived API tokens.
yargs-interactive
Advanced tools
Build interactive command line tools without worring to parse the arguments (or ask them).
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!).
This tool helps you to build command line tools without worring to parse arguments, or develop the logic to ask them.
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. result.name)
myCli(result.name);
});
By simply wrapping your CLI code with this tool, you will get all the information you need from the user.
Note: See more usage examples in the examples folder.
It supports the following use cases
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
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 and if-empty , which prompts the option if the value wasn't set via command line parameters or using the default property. |
You can opt-out options from interactive mode by setting the prompt
property to never
.
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: '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);
});
By default, its value is if-empty
, prompting the question to the user if the value was not set via command line parameters or using the default property. Last, you can use always
to always prompt the option.
Usage in terminal
➜ node my-cli.js --name='Johh' --interactive
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
FAQs
Build interactive command line tools without worring to parse the arguments (or ask them).
We found that yargs-interactive demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
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.
Security News
Crates.io adds Trusted Publishing support, enabling secure GitHub Actions-based crate releases without long-lived API tokens.
Research
/Security News
Undocumented protestware found in 28 npm packages disrupts UI for Russian-language users visiting Russian and Belarusian domains.
Research
/Security News
North Korean threat actors deploy 67 malicious npm packages using the newly discovered XORIndex malware loader.