{"name":"command-line-commands","author":{"name":"Lloyd Brookes","email":"75pound@gmail.com"},"version":"4.0.1","description":"Add a git-like command interface to your app.","repository":{"type":"git","url":"git+https://github.com/75lb/command-line-commands.git"},"license":"MIT","type":"module","exports":{"import":"./index.js","require":"./dist/index.cjs"},"keywords":["argv","parse","argument","args","option","parser","parsing","cli","command","commands","line"],"engines":{"node":">=12.17"},"scripts":{"test":"npm run dist && npm run test:ci","test:ci":"75lb-nature test-runner test.js","docs":"75lb-nature jsdoc2md -t README.hbs index.js > README.md","dist":"75lb-nature cjs-build index.js"},"peerDependencies":{"@75lb/nature":"latest"},"peerDependenciesMeta":{"@75lb/nature":{"optional":true}},"dependencies":{"array-back":"^6.2.2"},"devDependencies":{"command-line-args":"^6.0.0","command-line-usage":"^7.0.3","minimist":"^1.2.8"},"_id":"command-line-commands@4.0.1","gitHead":"800b3b9111e7f6da6e1b647a5ef46ce77376fbc6","bugs":{"url":"https://github.com/75lb/command-line-commands/issues"},"homepage":"https://github.com/75lb/command-line-commands#readme","_nodeVersion":"22.8.0","_npmVersion":"10.8.2","dist":{"integrity":"sha512-EKgwIf5iea3FkVF5aFPb92cqqQ0J4N+GwqLb8SCMy3BPh4w7nYc7FTmOkUg9MrBqfh9r5f/CPhlFph3zd5T/eA==","shasum":"354e07bd0cadfc6e36dbb26a21dfa4e21ac32d6e","tarball":"http://registry-npm.ywwl.com/command-line-commands/download/command-line-commands-4.0.1.tgz","fileCount":7,"unpackedSize":16479,"signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIAXVhr/9/JkePHT3rEZeOLtVu+zYv1XeunW7AYroZDGsAiEAhIqJsurcmZjcb4tmHCa8fdlNT2yMloVdO1++EOajuaE="}],"size":4058,"noattachment":false,"key":"/command-line-commands/-/command-line-commands-4.0.1.tgz"},"_npmUser":{"name":"75lb","email":"75pound@gmail.com"},"directories":{},"maintainers":[{"name":"75lb","email":"75pound@gmail.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/command-line-commands_4.0.1_1726528204193_0.5424484652479973"},"_hasShrinkwrap":false,"readme":"[![view on npm](https://badgen.net/npm/v/command-line-commands)](https://www.npmjs.org/package/command-line-commands)\n[![npm module downloads](https://badgen.net/npm/dt/command-line-commands)](https://www.npmjs.org/package/command-line-commands)\n[![Gihub repo dependents](https://badgen.net/github/dependents-repo/75lb/command-line-commands)](https://github.com/75lb/command-line-commands/network/dependents?dependent_type=REPOSITORY)\n[![Gihub package dependents](https://badgen.net/github/dependents-pkg/75lb/command-line-commands)](https://github.com/75lb/command-line-commands/network/dependents?dependent_type=PACKAGE)\n[![Node.js CI](https://github.com/75lb/command-line-commands/actions/workflows/node.js.yml/badge.svg)](https://github.com/75lb/command-line-commands/actions/workflows/node.js.yml)\n[![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg)](https://github.com/feross/standard)\n\n# command-line-commands\n\nA lightweight module to help build a git-like command interface for your app.\n\nIts job is to extract the command (the first argument, unless it's an option), check it's valid and either return it or throw. From there, you can parse the remaining args using your preferred option parser (e.g. [command-line-args](https://github.com/75lb/command-line-args), [minimist](https://github.com/substack/minimist) etc.).\n\n## Synopsis\n\nCreate a list of valid commands (`null` represents \"no command\"). Supply it to `commandLineCommands()`, receiving back an object with two properties: `command` (the supplied command) and `argv` (the remainder of the command line args):\n```js\nconst commandLineCommands = require('command-line-commands')\n\nconst validCommands = [ null, 'clean', 'update', 'install' ]\nconst { command, argv } = commandLineCommands(validCommands)\n\n/* print the command and remaining command-line args */\nconsole.log('command: %s', command)\nconsole.log('argv:    %s', JSON.stringify(argv))\n```\n\nWe'll assume the above script is installed as `example`. Since the `validCommands` list includes `null`, running it without a command is valid:\n```\n$ example\ncommand: null\nargv:    []\n```\n\nRunning `example` with no command and one option:\n```\n$ example --verbose\ncommand: null\nargv:    [\"--verbose\"]\n```\n\nRunning `example` with both a command and an option:\n```\n$ example install --save something\ncommand: install\nargv:    [\"--save\",\"something\"]\n```\n\nRunning `example` without a valid command will cause `commandLineCommands()` to throw.\n\nFrom here, you can make a decision how to proceed based on the `command` and `argv` received. For example, if no command (`null`) was passed, you could parse the remaining `argv` for general options (in this case using [command-line-args](https://github.com/75lb/command-line-args)):\n\n```js\nif (command === null) {\n  const commandLineArgs = require('command-line-args')\n  const optionDefinitions = [\n    { name: 'version', type: Boolean }\n  ]\n\n  // pass in the `argv` returned by `commandLineCommands()`\n  const options = commandLineArgs(optionDefinitions, { argv })\n\n  if (options.version) {\n    console.log('version 1.0.1')\n  }\n}\n```\n\nThe same example, using [minimist](https://github.com/substack/minimist):\n\n```js\nif (command === null) {\n  const minimist = require('minimist')\n\n  // pass in the `argv` returned by `commandLineCommands()``\n  const options = minimist(argv)\n\n  if (options.version) {\n    console.log('version 1.0.1')\n  }\n}\n```\n\n## More examples\n\nBoth examples use [command-line-args](https://github.com/75lb/command-line-args) for option-parsing.\n\n- [Simple](https://github.com/75lb/command-line-commands/blob/master/example/simple.js): A basic app with a couple of commands.\n- [Advanced](https://github.com/75lb/command-line-commands/blob/master/example/advanced/git.js): A more complete example, implementing part of the git command interface.\n\n## Usage guides\n\nUsage guides can be generated by [command-line-usage](https://github.com/75lb/command-line-usage). Here is a simple example ([code](https://github.com/75lb/command-line-commands/blob/master/example/usage.js)):\n\n![usage](https://raw.githubusercontent.com/75lb/command-line-commands/master/example/screens/command-list.png)\n\n# API Reference\n**Example**  \n```js\nconst commandLineCommands = require('command-line-commands')\n```\n<a name=\"exp_module_command-line-commands--commandLineCommands\"></a>\n\n### commandLineCommands(commands, [argv]) ⇒ <code>Object</code> ⏏\nParses the `argv` value supplied (or `process.argv` by default), extracting and returning the `command` and remainder of `argv`. The command will be the first value in the `argv` array unless it is an option (e.g. `--help`).\n\n**Kind**: Exported function  \n**Throws**:\n\n- `INVALID_COMMAND` - user supplied a command not specified in `commands`.\n\n\n| Param | Type | Description |\n| --- | --- | --- |\n| commands | <code>string</code> \\| <code>Array.&lt;string&gt;</code> | One or more command strings, one of which the user must supply. Include `null` to represent \"no command\" (effectively making a command optional). |\n| [argv] | <code>Array.&lt;string&gt;</code> | An argv array, defaults to the global `process.argv` if not supplied. |\n\n\n* * *\n\n&copy; 2015-25 Lloyd Brookes \\<75pound@gmail.com\\>. Documented by [jsdoc-to-markdown](https://github.com/jsdoc2md/jsdoc-to-markdown).\n","publish_time":1726528204404,"_cnpm_publish_time":1726528204404,"dist-tags":{"latest":"4.0.1"}}