Skip to content

Commit

Permalink
feat(prompt): Add the 'if-empty' value to the prompt option (closes #8)…
Browse files Browse the repository at this point in the history
… (#9)

BREAKING CHANGE: this feature breaks the contract of the prompt property, changing it from a boolean to a string.
  • Loading branch information
nanovazquez authored May 5, 2018
1 parent 4319a5a commit 2bf8ed6
Show file tree
Hide file tree
Showing 15 changed files with 3,002 additions and 150 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
.npm
.DS_Store
.nyc_output
coverage
npm-debug.log
node_modules
build
Expand Down
22 changes: 15 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@
[![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)
[![npm](https://img.shields.io/npm/dw/yargs-interactive.svg)](https://www.npmjs.com/package/yargs-interactive)


[See the blog post](https://medium.com/@nanovazquez/yargs-interactive-create-cli-tools-for-humans-and-non-humans-f9419f5cbd9e)
[_See the blog post_](https://medium.com/@nanovazquez/yargs-interactive-create-cli-tools-for-humans-and-non-humans-f9419f5cbd9e)

Interactive (prompt) support for [yargs](https://github.com/yargs/yargs), based on [inquirer](https://github.com/SBoudrias/Inquirer.js/). Useful for using the same CLI for both for humans and non-humans (like CI tools). Also supports mixed mode (yay!).

Expand Down Expand Up @@ -113,11 +112,18 @@ And then simply call your CLI with no parameters.
➜ node my-cli.js
```

**What type of prompts are supported?** It provides all prompt types supported by [Inquirer](https://github.com/SBoudrias/Inquirer.js/#prompt-types).
### 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](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` and `if-empty`, which prompts the option if the value wasn't set via command line parameters or using the default property. |

### Prompt just some questions (mixed mode)

You can opt-out options from interactive mode by setting the `prompt` property to `false`.
You can opt-out options from interactive mode by setting the `prompt` property to `never`.

**my-cli.js**
```js
Expand All @@ -133,22 +139,24 @@ const options = {
type: 'confirm',
default: false,
describe: 'Do you like pizza?',
prompt: false // because everyone likes pizza
prompt: 'never' // because everyone likes pizza
},
};

yargsInteractive()
.usage('$0 <command> [args]')
.interactive(options)
.then((result) => {
// The tool will prompt questions for all options and will output your answers.
// You can opt-out options by using `prompt: false`. For these properties, it
// 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
Expand Down
24 changes: 15 additions & 9 deletions examples/basic.js
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,19 +1,25 @@
#!/usr/bin/env node

/*
* Usage:
* Usage 1:
* 1) Open a terminal.
* 2) Enter: node ./examples/basic.js --name='John'
* 3) Output should have John as name and the default value of likesPizza (false).
* Result: Output should have John as name and the default value of likesPizza (false).
*
* Alternatives
* ------------
* 1) Enter: node ./examples/basic.js --name='John' --likesPizza
* Output should have John as name and likesPizza set to true
* Usage 2:
* 1) Open a terminal.
* 2) Enter: node ./examples/basic.js --name='John' --likesPizza
* Result: Output should have John as name and likesPizza set to true
*
* Usage 3:
* 1) Open a terminal.
* 2) Enter: node ./examples/basic.js
* Output should have the default values in all properties.
* 3) Enter: node ./examples/basic.js --interactive
* The tool will prompt questions and will output your answers.
* Result: Output should have the default values in all properties.
*
* Usage 4:
* 1) Open a terminal.
* 2) Enter: node ./examples/basic.js --interactive
* Result: he tool will prompt questions and will output the answers.
*/

const yargsInteractive = require('../src');
Expand Down
2 changes: 1 addition & 1 deletion examples/full-interactive.js
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* Usage:
* 1) Open a terminal.
* 2) Enter: node ./examples/full-interactive.js'
* 3) The tool will prompt questions and will output your answers.
* Result: The tool will prompt questions and will output the answers.
*/
const yargsInteractive = require('../src');

Expand Down
4 changes: 2 additions & 2 deletions examples/mixed-mode.js
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* Usage:
* 1) Open a terminal.
* 2) Enter: node ./examples/mixed-mode.js --interactive
* 3) The tool will prompt a question for the name but not for the likesPizza property (it will use the default value).
* Result: The tool will prompt a question for the name but not for the likesPizza property (it will use the default value instead).
*/

const yargsInteractive = require('../src');
Expand All @@ -19,7 +19,7 @@ const options = {
type: 'confirm',
default: true,
describe: 'Do you like pizza?',
prompt: false // because everyone likes pizza
prompt: 'never' // because everyone likes pizza
},
};

Expand Down
39 changes: 39 additions & 0 deletions examples/prompt.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#!/usr/bin/env node

/*
* Usage 1:
* 1) Open a terminal.
* 2) Enter: node ./examples/prompt.js --interactive
* Result: The tool will prompt question for namem and likesPizza.
*
* Usage 2:
* 1) Open a terminal.
* 2) Enter: node ./examples/prompt.js --name="John"
* Result: The tool will prompt question for likesPizza but not for the name (it was sent via parameter).
*/

const yargsInteractive = require('../src');

const options = {
name: {
type: 'input',
describe: 'Enter your name',
prompt: 'if-empty'
},
likesPizza: {
type: 'confirm',
default: false,
describe: 'Do you like pizza?'
},
};

yargsInteractive()
.usage('$0 <command> [args]')
.interactive(options)
.then((result) => {
console.log(
`\nResult is:\n`
+ `- Name: ${result.name}\n`
+ `- Likes pizza: ${result.likesPizza}\n`
);
});
Loading

0 comments on commit 2bf8ed6

Please sign in to comment.