yargs-interactive
Advanced tools
Comparing version 2.0.1 to 2.0.2
@@ -1,1 +0,51 @@ | ||
{"name":"yargs-interactive","version":"2.0.1","description":"Build interactive command line tools without worring to parse the arguments (or ask them).","main":"src/index.js","files":["src"],"scripts":{"lint":"eslint src test","test":"nyc --reporter=text --reporter=html mocha","coveralls":"nyc report --reporter=text-lcov | coveralls","commitmsg":"validate-commit-msg","semantic-release":"semantic-release pre && npm publish && semantic-release post"},"repository":{"type":"git","url":"git+https://github.com/nanovazquez/yargs-interactive.git"},"keywords":["yargs","interactive","cli","arguments","args","prompt","inquirer"],"author":"nanovazquez","license":"MIT","bugs":{"url":"https://github.com/nanovazquez/yargs-interactive/issues"},"homepage":"https://github.com/nanovazquez/yargs-interactive#readme","dependencies":{"inquirer":"^4.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":"^8.2.0","sinon":"^4.1.2","validate-commit-msg":"^2.14.0"}} | ||
{ | ||
"name": "yargs-interactive", | ||
"version": "2.0.2", | ||
"description": "Build interactive command line tools without worring to parse the arguments (or ask them).", | ||
"main": "src/index.js", | ||
"files": [ | ||
"src" | ||
], | ||
"scripts": { | ||
"lint": "eslint src test", | ||
"test": "nyc --reporter=text --reporter=html mocha", | ||
"coveralls": "nyc report --reporter=text-lcov | coveralls", | ||
"commitmsg": "validate-commit-msg", | ||
"semantic-release": "semantic-release" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/nanovazquez/yargs-interactive.git" | ||
}, | ||
"keywords": [ | ||
"yargs", | ||
"interactive", | ||
"cli", | ||
"arguments", | ||
"args", | ||
"prompt", | ||
"inquirer" | ||
], | ||
"author": "nanovazquez", | ||
"license": "MIT", | ||
"bugs": { | ||
"url": "https://github.com/nanovazquez/yargs-interactive/issues" | ||
}, | ||
"homepage": "https://github.com/nanovazquez/yargs-interactive#readme", | ||
"dependencies": { | ||
"inquirer": "^4.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" | ||
} | ||
} |
@@ -36,12 +36,20 @@ # Yargs Interactive | ||
// Get the arguments from the result | ||
// (e.g. result.name) | ||
myCli(result.name); | ||
// e.g. myCli(result.name); | ||
console.log( | ||
`\nResult is:\n` | ||
+ `- Name: ${result.name}\n` | ||
+ `- Likes pizza: ${result.likesPizza}\n` | ||
); | ||
}); | ||
``` | ||
By simply wrapping your CLI code with this tool, you will get all the information you need from the user. | ||
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](./assets/basic-usage.gif) | ||
> **Note:** See more usage examples [in the examples folder](./examples). | ||
> **Note:** See other CLI examples [in this folder](./examples). | ||
@@ -92,3 +100,3 @@ ## Usage | ||
If you want to **use interactive mode always**, just set the `--interactive`` parameter to `true` by default. | ||
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: | ||
@@ -128,3 +136,3 @@ ```js | ||
You can opt-out options from interactive mode by setting the `prompt` property to `never`. | ||
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. Last, you can use `always` to always prompt the option. | ||
@@ -137,4 +145,4 @@ **my-cli.js** | ||
name: { | ||
// prompt property if not set defaults to 'if-empty' | ||
type: 'input', | ||
name: 'nano', | ||
describe: 'Enter your name' | ||
@@ -162,9 +170,9 @@ }, | ||
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 | ||
➜ 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) | ||
@@ -179,3 +187,3 @@ | ||
type: 'input', | ||
name: 'nano', | ||
default: 'nano', | ||
describe: 'Enter your name' | ||
@@ -182,0 +190,0 @@ }, |
@@ -30,9 +30,19 @@ const yargs = require('yargs'); | ||
// Remove options with prompt value set to 'never' | ||
// and options with prompt value set to 'if-empty' but no default value or value set via parameter | ||
const interactiveOptions = filterObject(mergedOptions, (item, key) => ( | ||
item.prompt !== 'never' | ||
&& (item.prompt !== 'if-empty' || isEmpty(item.default) || isEmpty(argv[key])) | ||
)); | ||
// Filter options to prompt based on the "if-empty" property | ||
const interactiveOptions = filterObject(mergedOptions, (item, key) => { | ||
// Do not prompt items with prompt value set as "never" | ||
if (item.prompt === 'never') { | ||
return false; | ||
} | ||
// Prompt items with prompt value set as "always" | ||
if (item.prompt === 'always') { | ||
return true; | ||
} | ||
// Cases: item.prompt === "if-empty" or item.prompt undefined (fallbacks to "if-empty") | ||
// Prompt the items that are empty (i.e. a value was not sent via parameter OR doesn't have a default value) | ||
return isEmpty(argv[key]) && isEmpty(item.default); | ||
}); | ||
// Check if we should get the values from the interactive mode | ||
@@ -39,0 +49,0 @@ return argv.interactive |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
12523
92
207