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 - npm Package Compare versions

Comparing version 2.1.0 to 2.2.0

64

package.json
{
"name": "yargs-interactive",
"version": "2.1.0",
"version": "2.2.0",
"description": "Build interactive command line tools without worring to parse the arguments (or ask them).",

@@ -10,6 +10,5 @@ "main": "src/index.js",

"scripts": {
"lint": "eslint src test",
"test": "nyc --reporter=text --reporter=html mocha",
"coveralls": "nyc report --reporter=text-lcov | coveralls",
"commitmsg": "validate-commit-msg",
"lint": "eslint src test --fix",
"test": "jest --collectCoverage",
"coveralls": "jest --collectCoverage && cat ./coverage/lcov.info | coveralls",
"semantic-release": "semantic-release"

@@ -37,17 +36,50 @@ },

"dependencies": {
"inquirer": "^4.0.0",
"inquirer": "^7.0.0",
"yargs": "^10.0.3"
},
"devDependencies": {
"coveralls": "^3.0.0",
"eslint": "^4.11.0",
"eslint-config-google": "^0.9.1",
"husky": "^0.14.3",
"mocha": "^4.0.1",
"nyc": "^11.3.0",
"proxyquire": "^1.8.0",
"semantic-release": "^15.13.3",
"sinon": "^4.1.2",
"validate-commit-msg": "^2.14.0"
"@commitlint/cli": "^8.1.0",
"@commitlint/config-conventional": "^8.1.0",
"@types/inquirer": "^6.5.0",
"@types/jest": "^24.0.18",
"@types/node": "12.7.2",
"@types/yargs": "^13.0.2",
"babel-eslint": "^10.0.2",
"coveralls": "^3.0.6",
"cz-conventional-changelog": "^3.0.2",
"eslint": "^6.2.1",
"eslint-config-google": "^0.13.0",
"husky": "^3.0.4",
"jest": "^24.9.0",
"jest-matcher-one-of": "^1.0.2",
"lint-staged": "^9.2.3",
"proxyquire": "^2.1.3",
"semantic-release": "^15.13.24"
},
"engines": {
"node": ">=8",
"npm": ">=6"
},
"config": {
"commitizen": {
"path": "./node_modules/cz-conventional-changelog"
}
},
"husky": {
"hooks": {
"commit-msg": "commitlint -E HUSKY_GIT_PARAMS",
"pre-commit": "npx lint-staged"
}
},
"commitlint": {
"extends": [
"@commitlint/config-conventional"
]
},
"lint-staged": {
"*.{js}": [
"npm run lint",
"git add"
]
}
}
# Yargs Interactive
[![Build Status](https://travis-ci.org/nanovazquez/yargs-interactive.svg?branch=master)](https://travis-ci.org/nanovazquez/yargs-interactive) [![Coverage Status](https://coveralls.io/repos/github/nanovazquez/yargs-interactive/badge.svg)](https://coveralls.io/github/nanovazquez/yargs-interactive) [![semantic-release](https://img.shields.io/badge/%20%20%F0%9F%93%A6%F0%9F%9A%80-semantic--release-e10079.svg)](https://github.com/semantic-release/semantic-release) [![npm](https://img.shields.io/npm/v/yargs-interactive.svg?style=flat)](https://www.npmjs.com/package/yargs-interactive)

@@ -24,24 +25,20 @@ [![npm](https://img.shields.io/npm/dw/yargs-interactive.svg)](https://www.npmjs.com/package/yargs-interactive)

const yargsInteractive = require('yargs-interactive');
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?' },
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]')
.usage("$0 <command> [args]")
.interactive(options)
.then((result) => {
.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`
);
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:
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:

@@ -59,32 +56,32 @@ ```

It supports the following use cases
* [Prompt questions with default values (full-interactive)](#prompt-questions-with-default-values-full-interactive)
* [Prompt just some questions (mixed mode)](#prompt-just-some-questions-mixed-mode)
* [No prompt at all (ye olde yargs)](#no-prompt-at-all-ye-olde-yargs)
### Prompt questions with default values (full-interactive)
- [Prompt all questions](#prompt-questions-with-default-values-full-interactive)
- [Prompt some questions (mixed mode)](#prompt-just-some-questions-mixed-mode)
- [No prompt at all (ye olde yargs)](#no-prompt-at-all-ye-olde-yargs)
### Prompt questions (full-interactive)
**my-cli.js**
```js
const yargsInteractive = require('yargs-interactive');
const yargsInteractive = require("yargs-interactive");
const options = {
name: {
type: 'input',
default: 'nano',
describe: 'Enter your name'
type: "input",
describe: "Enter your name"
},
likesPizza: {
type: 'confirm',
default: false,
describe: 'Do you like pizza?'
},
type: "confirm",
describe: "Do you like pizza?"
}
};
yargsInteractive()
.usage('$0 <command> [args]')
.usage("$0 <command> [args]")
.interactive(options)
.then((result) => {
.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)
console.log(result);
});

@@ -127,35 +124,38 @@ ```

| 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](https://github.com/SBoudrias/Inquirer.js/#prompt-types).**|
| 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). |
| 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](https://github.com/SBoudrias/Inquirer.js/#prompt-types).** |
| 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 just some questions (mixed mode)
### 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 it doesn't have a default property. Setting to `if-no-arg` will only prompt the question if no argument is provided. Lastly, you can use `always` to always prompt the option.
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**
```js
const yargsInteractive = require('yargs-interactive');
const yargsInteractive = require("yargs-interactive");
const options = {
name: {
// prompt property if not set defaults to 'if-empty'
type: 'input',
describe: 'Enter your 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',
type: "confirm",
default: false,
describe: 'Do you like pizza?',
prompt: 'never' // because everyone likes pizza
},
describe: "Do you like pizza?",
prompt: "never" // because everyone likes pizza
}
};
yargsInteractive()
.usage('$0 <command> [args]')
.usage("$0 <command> [args]")
.interactive(options)
.then((result) => {
.then(result => {
// The tool will prompt questions output the answers.

@@ -170,2 +170,3 @@ // You can opt-out options by using `prompt: 'never'`. For these properties, it

**Usage in terminal**
```

@@ -180,22 +181,23 @@ ➜ node my-cli.js --interactive

**my-cli.js**
```js
const yargsInteractive = require('yargs-interactive');
const yargsInteractive = require("yargs-interactive");
const options = {
name: {
type: 'input',
default: 'nano',
describe: 'Enter your name'
type: "input",
default: "nano",
describe: "Enter your name"
},
likesPizza: {
type: 'confirm',
type: "confirm",
default: false,
describe: 'Do you like pizza?'
},
describe: "Do you like pizza?"
}
};
yargsInteractive()
.usage('$0 <command> [args]')
.usage("$0 <command> [args]")
.interactive(options)
.then((result) => {
.then(result => {
// The tool will output the values set via parameters or

@@ -209,4 +211,5 @@ // the default value (if not provided).

**Usage in terminal**
```
➜ node my-cli.js --name='Johh' --likesPizza
```
/**
* Filter keys in an object based on a condition.
* @param {Object} element - The element whose keys will be filtered.
* @param {any} condition - (value, key) => boolean
* @param {object} element The element whose keys will be filtered.
* @param {any} condition (value, key) => boolean
* The condition to execute in each key value (i.e. if condition(value, key) returns true, the key is kept).
* @return {object} - A new object with only the keys that pass the condition.
* @return {object} A new object with only the keys that pass the condition.
*/

@@ -8,0 +8,0 @@ module.exports = (element = {}, condition = () => true) => {

const inquirer = require('inquirer');
/**
* Initiate an interactive prompt to get values from the user.
* @param {object} values The values to configure the prompt
* @return {object} A promise that, when fullfilled, will contain answer of the questions prompted to the user
*/
module.exports = (values = {}) => {

@@ -11,3 +16,3 @@ const prompt = inquirer.createPromptModule();

message: value.describe,
default: value.default,
default: value.default
});

@@ -14,0 +19,0 @@ });

/**
* Checks if the argument received is provided in the argument collection
* @param {Object} arg - The arg to check (e.g. `likesPizza`)
* @param {Object} processArgs - The collection of process arguments (e.g. `["--interactive", "--likesPizza=true"]`)
* @return {boolean} - Returns true if the argument is present in the collection of process arguments, false otherwise.
* @param {Object} arg The arg to check (e.g. `likesPizza`)
* @param {Object} processArgs The collection of process arguments (e.g. `["--interactive", "--likesPizza=true"]`)
* @return {boolean} True if the argument is present in the collection of process arguments, false otherwise.
*/

@@ -7,0 +7,0 @@ module.exports = (arg, processArgs) => {

@@ -8,3 +8,3 @@ const yargs = require('yargs');

// Set up yargs options
let yargsInteractive = (processArgs = process.argv.slice(2), cwd) => {
const yargsInteractive = (processArgs = process.argv.slice(2), cwd) => {
const yargsConfig = yargs(processArgs, cwd);

@@ -49,5 +49,3 @@

// Check if we should get the values from the interactive mode
return argv.interactive
? interactiveMode(interactiveOptions).then((result) => Object.assign({}, argv, result))
: Promise.resolve(argv);
return argv.interactive ? interactiveMode(interactiveOptions).then((result) => Object.assign({}, argv, result)) : Promise.resolve(argv);
};

@@ -54,0 +52,0 @@

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