{"id":13903004,"url":"https://github.com/fardjad/node-interactive-commander","last_synced_at":"2025-07-11T05:31:07.293Z","repository":{"id":193741692,"uuid":"689421108","full_name":"fardjad/node-interactive-commander","owner":"fardjad","description":"Commander.js with integrated interactive prompts","archived":false,"fork":false,"pushed_at":"2024-10-07T00:48:08.000Z","size":1649,"stargazers_count":6,"open_issues_count":4,"forks_count":1,"subscribers_count":1,"default_branch":"main","last_synced_at":"2024-10-07T00:49:06.823Z","etag":null,"topics":["cli","command-line","commander","commanderjs","inquirer","interactive","prompt"],"latest_commit_sha":null,"homepage":"https://www.npmjs.com/package/interactive-commander","language":"TypeScript","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/fardjad.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","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,"publiccode":null,"codemeta":null}},"created_at":"2023-09-09T18:46:32.000Z","updated_at":"2024-10-07T00:46:58.000Z","dependencies_parsed_at":"2023-12-23T01:43:26.002Z","dependency_job_id":"0c3ff67f-65d3-486c-b4a2-31d868c0a429","html_url":"https://github.com/fardjad/node-interactive-commander","commit_stats":null,"previous_names":["fardjad/node-interactive-commander"],"tags_count":210,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fardjad%2Fnode-interactive-commander","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fardjad%2Fnode-interactive-commander/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fardjad%2Fnode-interactive-commander/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fardjad%2Fnode-interactive-commander/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/fardjad","download_url":"https://codeload.github.com/fardjad/node-interactive-commander/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":225693786,"owners_count":17509227,"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":["cli","command-line","commander","commanderjs","inquirer","interactive","prompt"],"created_at":"2024-08-06T22:01:33.342Z","updated_at":"2024-11-21T08:09:09.816Z","avatar_url":"https://github.com/fardjad.png","language":"TypeScript","funding_links":[],"categories":["cli"],"sub_categories":[],"readme":"# Interactive Commander\n\n\u003cdiv class=\"paragraph\"\u003e\n\n\u003cspan class=\"image\"\u003e\u003ca href=\"https://www.npmjs.com/package/interactive-commander\" class=\"image\"\u003e\u003cimg src=\"https://img.shields.io/npm/v/interactive-commander\" alt=\"NPM Version\" /\u003e\u003c/a\u003e\u003c/span\u003e \u003cspan class=\"image\"\u003e\u003ca href=\"https://www.npmjs.com/package/interactive-commander\" class=\"image\"\u003e\u003cimg src=\"https://img.shields.io/npm/dm/interactive-commander\" alt=\"Monthly Downloads\" /\u003e\u003c/a\u003e\u003c/span\u003e \u003cspan class=\"image\"\u003e\u003ca href=\"https://github.com/fardjad/node-interactive-commander/actions\" class=\"image\"\u003e\u003cimg src=\"https://img.shields.io/github/actions/workflow/status/fardjad/node-interactive-commander/test-and-release.yml?branch=main\" alt=\"test-and-release Workflow Status\" /\u003e\u003c/a\u003e\u003c/span\u003e\n\n\u003c/div\u003e\n\nInteractive Commander is an extension of the widely-used [Commander.js][1] library.\nIt seamlessly integrates configurable interactive prompts for missing options\nin your CLI application, enhancing user experience with minimal effort.\n\n![Video Demo](/media/demo.gif)\n\n## Features\n\n- Full compatibility with Commander.js (utilizes Commander.js under the hood\n  and serves as a drop-in replacement for it)\n- Customizable flags for interactive mode\n- Automatic prompting for missing options in interactive mode\n- Built-in [Inquirer.js][2] prompts for boolean, multiple-choice, and string\n  options, along with the ability to create custom prompts\n- [Support for plugins](/examples/plugin.ts)\n\n## Installation\n\n```bash\nnpm install --save interactive-commander\n```\n\nYou can also scaffold a new project with Interactive Commander and TypeScript\nwith [create-interactive-commander][4]. To do so, run the following command and\nfollow the prompts:\n\n```bash\nnpm create interactive-commander@latest\n```\n\n## Usage\n\n```typescript\nimport { InteractiveCommand, InteractiveOption } from \"interactive-commander\";\n\nconst program = new InteractiveCommand();\n\nprogram\n  .command(\"pizza\")\n  // Detached options are interactive by default\n  .option(\"-d, --drink\", \"drink\")\n\n  // Missing mandatory options won't throw an error in interactive mode\n  .requiredOption(\"-o, --olive-oil\", \"olive oil\")\n\n  // Boolean InteractiveOptions will show a confirmation prompt by default\n  .option(\"-c, --cheese\", \"cheese\")\n  .option(\"-C, --no-cheese\", \"no cheese\")\n\n  .addOption(\n    new InteractiveOption(\"-n, --count \u003cnumber\u003e\", \"number of pizzas\")\n      // InteractiveOption supports all the methods of Commander.js' Option\n      .argParser(Number)\n      .default(1),\n  )\n\n  .addOption(\n    new InteractiveOption(\n      \"--non-interactive-option \u003cvalue\u003e\",\n      \"non-interactive option\",\n    )\n      // Passing in undefined to prompt will disable interactive mode for this option\n      .prompt(undefined)\n      .default(\"default value\"),\n  )\n\n  // InteractiveOptions with choices will show a select prompt by default\n  .addOption(\n    new InteractiveOption(\"-s, --size \u003csize\u003e\", \"size\")\n      .choices([\"small\", \"medium\", \"large\"])\n      .default(\"medium\")\n      .makeOptionMandatory(),\n  )\n\n  .addOption(\n    new InteractiveOption(\"-m, --name \u003cstring\u003e\", \"your name\")\n      // You can use the prompt method to implement your own prompt logic\n      .prompt(async (currentValue, option, command) =\u003e {\n        // TODO: Implement your own prompt logic here\n        const answer = \"world\";\n\n        // Return the answer\n        return answer;\n      })\n      .makeOptionMandatory(),\n  )\n\n  .addOption(\n    new InteractiveOption(\"-r, --voucher-code \u003cstring\u003e\", \"voucher code\")\n      // The prompt input gets validated by the argParser function\n      .argParser((value) =\u003e {\n        if (typeof value !== \"string\" || !/^\\d{4}$/.test(value)) {\n          throw new TypeError(\"Invalid voucher code\");\n        }\n\n        return value;\n      }),\n  )\n\n  .action((_options, cmd: InteractiveCommand) =\u003e {\n    console.log(\"Options: %o\", cmd.opts());\n  });\n\nawait program\n  // Enables interactive mode (when -i or --interactive is passed in)\n  // This should almost always be called on the root command right before\n  // calling parseAsync\n  .interactive(\"-i, --interactive\", \"interactive mode\")\n  .parseAsync(process.argv);\n\n// Try the following commands:\n// command-name pizza\n// command-name pizza -i\n// command-name pizza -i --count 2\n// command-name pizza -i --count 2 --no-cheese\n// command-name pizza -i --name \"John Doe\"\n// command-name pizza -i --name \"John Doe\" --non-interactive-option abc\n```\n\nMore examples can be found in the [examples](/examples/) directory.\n\n## Recipes\n\n### Interactive options for the root command\n\nInteractive options on main command won't be prompted for in interactive mode\nif no subcommand is invoked. That is because Commander.js doesn't support\npre-parse (similar to `preSubcommand` hooks) hooks for the main command. As a\nworkaround, you can define a subcommand as the default command to achieve a\nsimilar effect.\n\nSee [default-subcommand.ts](examples/default-subcommand.ts) for an example.\n\n### Enable interactive mode by default\n\nTo enable interactive mode by default, you can define the interactive flags as\n[negatable boolean options][3] (e.g. `--no-interactive`).\n\nSee [no-interactive.ts](examples/no-interactive.ts) for an example.\n\n### Setting default values for option prompts based on other options\n\nIn some cases, it may be necessary for the default value of an option prompt to\ndepend on the value of another option. For example, you might want the billing\naddress to be automatically set to the same value as the user's input for the\nshipping address. To achieve that, you can decorate the `readFunction` of the\nof the billing address option to dynamically set the default value of the\nprompt.\n\nSee [dependent-prompts.ts](examples/dependent-prompts.ts) for an example.\n\n[1]: https://github.com/tj/commander.js\n[2]: https://github.com/SBoudrias/Inquirer.js\n[3]: https://github.com/tj/commander.js#other-option-types-negatable-boolean-and-booleanvalue\n[4]: https://github.com/fardjad/node-create-interactive-commander\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffardjad%2Fnode-interactive-commander","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ffardjad%2Fnode-interactive-commander","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffardjad%2Fnode-interactive-commander/lists"}