{"id":13526811,"url":"https://github.com/moxystudio/node-promptly","last_synced_at":"2025-12-24T22:52:51.716Z","repository":{"id":6598530,"uuid":"7841535","full_name":"moxystudio/node-promptly","owner":"moxystudio","description":"Simple command line prompting utility for nodejs","archived":false,"fork":false,"pushed_at":"2021-12-14T18:06:42.000Z","size":504,"stargazers_count":149,"open_issues_count":0,"forks_count":13,"subscribers_count":12,"default_branch":"master","last_synced_at":"2024-04-14T12:10:32.905Z","etag":null,"topics":["choose","cli","command-line","nodejs","prompt"],"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/moxystudio.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2013-01-26T18:12:40.000Z","updated_at":"2024-03-09T16:07:13.000Z","dependencies_parsed_at":"2022-07-31T02:38:21.122Z","dependency_job_id":null,"html_url":"https://github.com/moxystudio/node-promptly","commit_stats":null,"previous_names":[],"tags_count":15,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/moxystudio%2Fnode-promptly","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/moxystudio%2Fnode-promptly/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/moxystudio%2Fnode-promptly/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/moxystudio%2Fnode-promptly/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/moxystudio","download_url":"https://codeload.github.com/moxystudio/node-promptly/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246607041,"owners_count":20804509,"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":["choose","cli","command-line","nodejs","prompt"],"created_at":"2024-08-01T06:01:35.348Z","updated_at":"2025-04-01T08:30:21.774Z","avatar_url":"https://github.com/moxystudio.png","language":"JavaScript","readme":"# promptly\n\n[![NPM version][npm-image]][npm-url] [![Downloads][downloads-image]][npm-url] [![Build Status][travis-image]][travis-url] [![Coverage Status][codecov-image]][codecov-url] [![Dependency status][david-dm-image]][david-dm-url] [![Dev Dependency status][david-dm-dev-image]][david-dm-dev-url]\n\n[npm-url]:https://npmjs.org/package/promptly\n[downloads-image]:https://img.shields.io/npm/dm/promptly.svg\n[npm-image]:https://img.shields.io/npm/v/promptly.svg\n[travis-url]:https://travis-ci.org/moxystudio/node-promptly\n[travis-image]:https://img.shields.io/travis/moxystudio/node-promptly/master.svg\n[codecov-url]:https://codecov.io/gh/moxystudio/node-promptly\n[codecov-image]:https://img.shields.io/codecov/c/github/moxystudio/node-promptly/master.svg\n[david-dm-url]:https://david-dm.org/moxystudio/node-promptly\n[david-dm-image]:https://img.shields.io/david/moxystudio/node-promptly.svg\n[david-dm-dev-url]:https://david-dm.org/moxystudio/node-promptly?type=dev\n[david-dm-dev-image]:https://img.shields.io/david/dev/moxystudio/node-promptly.svg\n\n\u003e Simple command line prompting utility.\n\n\n## Installation\n\n`$ npm install promptly`\n\n\n## API\n\n### .prompt(message, [options])\n\nPrompts for a value, printing the `message` and waiting for the input.   \nReturns a promise that resolves with the input.\n\nAvailable options:\n\n| Name   | Description   | Type     | Default |\n| ------ | ------------- | -------- | ------- |\n| default | The default value to use if the user provided an empty input | string | undefined |\n| trim | Trims the user input | boolean | true |\n| validator | A validator or an array of validators | function/array | undefined |\n| retry | Retry if any of the validators fail | boolean | true |\n| silent | Do not print what the user types | boolean | false |\n| replace | Replace each character with the specified string when `silent` is true | string | '' |\n| input | Input stream to read from | [Stream](https://nodejs.org/api/process.html#process_process_stdin) | process.stdin |\n| output | Output stream to write to | [Stream](https://nodejs.org/api/process.html#process_process_stdout) | process.stdout |\n| timeout | Timeout in ms | number | 0 |\n| useDefaultOnTimeout | Return default value if timed out | boolean | false |\n\nThe same **options** are available to **all functions** but with different default values.\n\n#### Examples\n\n- Ask for a name:\n\n    ```js\n    const promptly = require('promptly');\n\n    (async () =\u003e {\n        const name = await promptly.prompt('Name: ');\n        console.log(name);\n    })();\n    ```\n\n- Ask for a name with a constraint (non-empty value and length \u003e 2):\n\n    ```js\n    const promptly = require('promptly');\n\n    const validator = function (value) {\n        if (value.length \u003c 2) {\n            throw new Error('Min length of 2');\n        }\n\n        return value;\n    };\n\n    (async () =\u003e {\n        const name = await promptly.prompt('Name: ', { validator });\n        // Since retry is true by default, promptly will keep asking for a name until it is valid\n        // Between each prompt, the error message from the validator will be printed\n        console.log('Name is:', name);\n    })();\n    \n    ```\n\n- Same as above but do not retry automatically:\n\n    ```js\n    const promptly = require('promptly');\n\n    const validator = function (value) {\n        if (value.length \u003c 2) {\n            throw new Error('Min length of 2');\n        }\n\n        return value;\n    };\n\n    (async () =\u003e {\n        try {\n            const name = await promptly.prompt('Name: ', { validator, retry: false });\n            console.log('Name is:', name);\n        } catch (err) {\n            console.error('Invalid name:')\n            console.error(`- ${err.message}`);\n        }\n    })();\n    ```\n\n- Ask for a name with timeout:\n\n    ```js\n    const promptly = require('promptly');\n\n    (async () =\u003e {\n        const name = await promptly.prompt('Name: ', { timeout: 3000 });\n        console.log(name);\n    })();\n    ```\n\n    It throws an `Error(\"timed out\")` if timeout is reached and no default value is provided\n\n#### Validators\n\nThe validators have two purposes: to check and transform input. They can be asynchronous or synchronous\n\n```js\nconst validator = (value) =\u003e {\n    // Validation example, throwing an error when invalid\n    if (value.length !== 2) {\n        throw new Error('Length must be 2');\n    }\n\n    // Parse the value, modifying it\n    return value.replace('aa', 'bb');\n}\n\nconst asyncValidator = async (value) =\u003e {\n    await myfunc();\n    return value;\n}\n```\n\n### .confirm(message, [options])\n\nAsk the user for confirmation, printing the `message` and waiting for the input.   \nReturns a promise that resolves with the answer.\n\nTruthy values are: `y`, `yes` and `1`. Falsy values are `n`, `no`, and `0`.   \nComparison is made in a case insensitive way.\n\nThe options are the same as [prompt](#promptmessage-options), except that `trim` defaults to `false`.\n\n#### Examples\n\n- Ask to confirm something important:\n\n    ```js\n    const promptly = require('promptly');\n\n    (async () =\u003e {\n        const answer = await promptly.confirm('Are you really sure? ');\n\n        console.log('Answer:', answer);\n    })();\n    ```\n\n### .choose(message, choices, [options])\n\nAsk the user to choose between multiple `choices` (array of choices), printing the `message` and waiting for the input.   \nReturns a promise that resolves with the choice.\n\nThe options are the same as [prompt](#promptmessage-options), except that `trim` defaults to `false`.\n\n#### Examples\n\n- Ask to choose between:\n\n    ```js\n    const promptly = require('promptly');\n\n    (async () =\u003e {\n        const choice = await promptly.choose('Do you want an apple or an orange? ', ['apple', 'orange']);\n\n        console.log('Choice:', choice);\n    })();\n    ```\n\n### .password(message, [options])\n\nPrompts for a password, printing the `message` and waiting for the input.   \nReturns a promise that resolves with the password.\n\nThe options are the same as [prompt](#promptmessage-options), except that `trim` and `silent` default to `false` and `default` is an empty string (to allow empty passwords).\n\n#### Examples\n\n- Ask for a password:\n\n    ```js\n    const promptly = require('promptly');\n\n    (async () =\u003e {\n        const password = await promptly.password('Type a password: ');\n\n        console.log('Password:', password);\n    })();\n    ```\n\n- Ask for a password but mask the input with `*`:\n\n    ```js\n    const promptly = require('promptly');\n\n    (async () =\u003e {\n        const password = await promptly.password('Type a password: ', { replace: '*' });\n\n        console.log('Password:', password);\n    })();\n    ```\n\n## Tests\n\n`$ npm test`   \n`$ npm test -- --watch` during development\n\n\n## License\n\nReleased under the [MIT License](https://www.opensource.org/licenses/mit-license.php).\n","funding_links":[],"categories":["Repository"],"sub_categories":["Command-line Utilities"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmoxystudio%2Fnode-promptly","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmoxystudio%2Fnode-promptly","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmoxystudio%2Fnode-promptly/lists"}