{"id":13903041,"url":"https://github.com/nanovazquez/yargs-interactive","last_synced_at":"2025-07-18T00:33:05.593Z","repository":{"id":27025871,"uuid":"112198247","full_name":"nanovazquez/yargs-interactive","owner":"nanovazquez","description":"Interactive support for yargs","archived":false,"fork":false,"pushed_at":"2023-02-27T17:34:43.000Z","size":6432,"stargazers_count":46,"open_issues_count":24,"forks_count":9,"subscribers_count":3,"default_branch":"master","last_synced_at":"2024-11-13T12:38:59.449Z","etag":null,"topics":["argument-parsing","cli","inquirer","interactive","olde-yargs","parsed-arguments","prompt","yargs"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/nanovazquez.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null}},"created_at":"2017-11-27T13:14:52.000Z","updated_at":"2024-04-27T14:54:11.000Z","dependencies_parsed_at":"2024-04-08T18:58:08.298Z","dependency_job_id":"db47d7c8-1678-4e5e-b42b-e497695b49d8","html_url":"https://github.com/nanovazquez/yargs-interactive","commit_stats":{"total_commits":44,"total_committers":3,"mean_commits":"14.666666666666666","dds":"0.045454545454545414","last_synced_commit":"0b42c2c866d8eec23fb12c8739ce79eed12e730a"},"previous_names":[],"tags_count":11,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nanovazquez%2Fyargs-interactive","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nanovazquez%2Fyargs-interactive/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nanovazquez%2Fyargs-interactive/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nanovazquez%2Fyargs-interactive/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/nanovazquez","download_url":"https://codeload.github.com/nanovazquez/yargs-interactive/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":226320870,"owners_count":17606370,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["argument-parsing","cli","inquirer","interactive","olde-yargs","parsed-arguments","prompt","yargs"],"created_at":"2024-08-06T22:01:34.983Z","updated_at":"2024-11-25T11:31:14.229Z","avatar_url":"https://github.com/nanovazquez.png","language":"JavaScript","funding_links":[],"categories":["cli"],"sub_categories":[],"readme":"# Yargs Interactive\n\n[![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)\n[![npm](https://img.shields.io/npm/dw/yargs-interactive.svg)](https://www.npmjs.com/package/yargs-interactive)\n\n[_Read the blog post_](https://medium.com/@nanovazquez/yargs-interactive-create-cli-tools-for-humans-and-non-humans-f9419f5cbd9e)\n\nInteractive (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!).\n\n![Yargs Interactive](./assets/yargs-interactive-logo.png)\n\nThis tool helps you to build command line tools without worring to parse arguments, or develop the logic to ask them.\n\n## Installation\n\n```\nnpm install -S yargs-interactive\n```\n\nThen, add this code in your CLI code to get all the arguments parsed:\n\n```js\n#!/usr/bin/env node\n\nconst yargsInteractive = require(\"yargs-interactive\");\nconst options = {\n  name: { type: \"input\", default: \"A robot\", describe: \"Enter your name\" },\n  likesPizza: { type: \"confirm\", default: false, describe: \"Do you like pizza?\" }\n};\n\nyargsInteractive()\n  .usage(\"$0 \u003ccommand\u003e [args]\")\n  .interactive(options)\n  .then(result =\u003e {\n    // Your business logic goes here.\n    // Get the arguments from the result\n    // e.g. myCli(result.name);\n    console.log(`\\nResult is:\\n` + `- Name: ${result.name}\\n` + `- Likes pizza: ${result.likesPizza}\\n`);\n  });\n```\n\nNow, 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:\n\n```\n➜ node my-cli.js --interactive\n```\n\n![Basic usage](./assets/basic-usage.gif)\n\n\u003e **Note:** See other CLI examples [in this folder](./examples).\n\n## Usage\n\nIt supports the following use cases\n\n- [Prompt all questions](#prompt-questions-with-default-values-full-interactive)\n- [Prompt some questions (mixed mode)](#prompt-just-some-questions-mixed-mode)\n- [No prompt at all (ye olde yargs)](#no-prompt-at-all-ye-olde-yargs)\n\n### Prompt questions (full-interactive)\n\n**my-cli.js**\n\n```js\nconst yargsInteractive = require(\"yargs-interactive\");\n\nconst options = {\n  name: {\n    type: \"input\",\n    describe: \"Enter your name\"\n  },\n  likesPizza: {\n    type: \"confirm\",\n    describe: \"Do you like pizza?\"\n  }\n};\n\nyargsInteractive()\n  .usage(\"$0 \u003ccommand\u003e [args]\")\n  .interactive(options)\n  .then(result =\u003e {\n    // The tool will prompt questions and will output your answers.\n    // TODO: Do something with the result (e.g result.name)\n    console.log(result);\n  });\n```\n\n**Usage in terminal**\n\n```\n➜ node my-cli.js --interactive\n```\n\n![](./assets/interactive-with-parameter.gif)\n\nIf you want to **use interactive mode always**, avoiding the need of sending it as an argument, set the `--interactive` parameter to `true` by default:\n\n```js\nconst options = {\n  interactive: { default: true },\n  ...\n};\n\nyargsInteractive()\n  .usage('$0 \u003ccommand\u003e [args]')\n  .interactive(options)\n  .then((result) =\u003e {\n    // The tool will prompt questions and will output your answers.\n    // TODO: Do something with the result (e.g result.name)\n    console.log(result)\n  });\n```\n\nAnd then simply call your CLI with no parameters.\n\n```\n➜ node my-cli.js\n```\n\n### Options\n\n| Property | Type   | Description                                                                                                                                                                                                                                                                                                               |\n| -------- | ------ | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |\n| 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).**                                                                                                                           |\n| describe | string | _(Required)_ The message to display when prompting the option (e.g. `Do you like pizza?`)                                                                                                                                                                                                                                 |\n| default  | any    | The default value of the option.                                                                                                                                                                                                                                                                                          |\n| 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). |\n\n### Prompt some questions (mixed mode)\n\nYou 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_.\n\n**my-cli.js**\n\n```js\nconst yargsInteractive = require(\"yargs-interactive\");\n\nconst options = {\n  name: {\n    // prompt property, if not set, defaults to 'if-empty'\n    // In this case, it means the question will be prompted\n    // if it is not provided by args, as it doesn't have a default value.\n    type: \"input\",\n    describe: \"Enter your name\"\n  },\n  likesPizza: {\n    type: \"confirm\",\n    default: false,\n    describe: \"Do you like pizza?\",\n    prompt: \"never\" // because everyone likes pizza\n  }\n};\n\nyargsInteractive()\n  .usage(\"$0 \u003ccommand\u003e [args]\")\n  .interactive(options)\n  .then(result =\u003e {\n    // The tool will prompt questions output the answers.\n    // You can opt-out options by using `prompt: 'never'`. For these properties, it\n    // will use the value sent by parameter (--likesPizza) or the default value.\n    // TODO: Do something with the result (e.g result.name)\n    console.log(result);\n  });\n```\n\n**Usage in terminal**\n\n```\n➜ node my-cli.js --interactive\n```\n\nNotice that if you enter `node my-cli.js --name='Johh' --interactive` name won't be prompted either (as by default it uses `if-empty`).\n\n### No prompt at all (ye olde yargs)\n\n**my-cli.js**\n\n```js\nconst yargsInteractive = require(\"yargs-interactive\");\n\nconst options = {\n  name: {\n    type: \"input\",\n    default: \"nano\",\n    describe: \"Enter your name\"\n  },\n  likesPizza: {\n    type: \"confirm\",\n    default: false,\n    describe: \"Do you like pizza?\"\n  }\n};\n\nyargsInteractive()\n  .usage(\"$0 \u003ccommand\u003e [args]\")\n  .interactive(options)\n  .then(result =\u003e {\n    // The tool will output the values set via parameters or\n    // the default value (if not provided).\n    // TODO: Do something with the result (e.g result.name)\n    console.log(result);\n  });\n```\n\n**Usage in terminal**\n\n```\n➜ node my-cli.js --name='Johh' --likesPizza\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnanovazquez%2Fyargs-interactive","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fnanovazquez%2Fyargs-interactive","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnanovazquez%2Fyargs-interactive/lists"}