{"_id":"@8pattern/jcommand","_rev":"2-510670997a4a2ae25a49ac651f91f31f","name":"@8pattern/jcommand","dist-tags":{"latest":"0.1.0"},"versions":{"0.1.0":{"name":"@8pattern/jcommand","version":"0.1.0","description":"a command-line helper for Node.js","scripts":{"dev":"rollup -c -w --environment NODE_ENV:development","build":"rollup -c --environment NODE_ENV:production","test":"jest"},"keywords":["commond-line"],"author":{"name":"8th-pattern"},"license":"MIT","main":"./dist/jcommand.js","types":"./src","devDependencies":{"@babel/plugin-proposal-object-rest-spread":"^7.5.5","@babel/preset-typescript":"^7.6.0","@types/jest":"^24.0.18","@types/node":"^12.12.5","babel-loader":"^8.0.6","jest":"^24.9.0","jest-cli":"^24.9.0","rollup":"^1.26.3","rollup-plugin-babel":"^4.3.3","rollup-plugin-terser":"^5.1.2","rollup-plugin-typescript2":"^0.24.3","ts-jest":"^24.1.0","typescript":"^3.6.3"},"repository":{"type":"git","url":"git+https://github.com/8th-pattern/jCommand.git"},"bugs":{"url":"https://github.com/8th-pattern/jCommand/issues"},"homepage":"https://github.com/8th-pattern/jCommand#readme","gitHead":"1a02a7192bb4e01b93615bca78f1bcef04383149","_id":"@8pattern/jcommand@0.1.0","_nodeVersion":"12.13.0","_npmVersion":"6.12.1","dist":{"integrity":"sha512-GKexo4pJhj1eMQjh3NhXjWvCocXo0sCOEVmXs1jMrI7alLMgXX9ftLKLT2MLmNeXlO7bWXu8cAvxVqKFOEQNQQ==","shasum":"b3069297e182c7b2aa1ba50cd35d2c31f9c09807","tarball":"https://registry.npmjs.org/@8pattern/jcommand/-/jcommand-0.1.0.tgz","fileCount":14,"unpackedSize":20399,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.4\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJdwoB4CRA9TVsSAnZWagAAWUcP/joO6Du2fmV03ET8KFFj\n2/KRy9gQD3uOcC/tP65B+7K3cj9LzQ/plm9K/0ue/bry9YL3/3jirkxen6jx\n2DD5TwUATR692NjgzH7rZypG6UxwV/rFoOrdJPzaSCj4HITZDCIRUYTBy44+\nm/NXY59zeaDfYJ/kAqjWmiW1Fhb5PjiHTVHduT2OUxgWi+YFabEfATasx4ax\n6yD07PKDqaS9o/DghOcecPOgQUhVte8vsquuwfrOpbq6XxICpFR8J54ZZ2iJ\nYM7VtxpUJO2G8LHN+PDR9sfhZQ9boY1+cLsdm0cehLRKvPCqFWh4NV9hTPXs\n6sMt8a1G6Y6GS7aALqA4GsA8MF2i2m8KelQG7xkjiDh3WJognjrGvXJIxawf\n1m5Wn4eshvKiva9lXHIYbf4sckYTa83rHZBMkFJjmxv+7e+Sr/scL9at2BFY\nrG+bp8ftOIrq8ceqIHVRXiZKUdhOZcqKCJqenzfKXfgkkgA9P9YIDfo9ePVo\n8QAind1UImJm4AQGdc2UKfCKKB7XZsA/wQzQDwgtTkiCZPbrrQPYp0WLkeBe\nX4pGfSIYbp5+EXSSKxO9+zAINiTeygFe2GUlf+ucOYPHdxBeqA+0MGJdSWQH\noJUjHg7pm6eA5oXmAmgKQVcb9b808R+6M74+6Cv8OsJiN/v8tEZfDFviAXhn\njSZK\r\n=DHsf\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIQCwlwQk6paq4J24x6etZqHnGPQSCRfxZSO9gCWoaBc4sgIgVHYwVua4WYrbLZw5Wp7d0CSmpom0UxhGpA14U4T3IYk="}]},"maintainers":[{"name":"8pattern","email":"eighth.principle@gmail.com"}],"_npmUser":{"name":"8pattern","email":"eighth.principle@gmail.com"},"directories":{},"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/jcommand_0.1.0_1573027960055_0.005495888411257477"},"_hasShrinkwrap":false,"deprecated":"Package no longer supported. Contact Support at https://www.npmjs.com/support for more info."}},"time":{"created":"2019-11-06T08:12:39.815Z","0.1.0":"2019-11-06T08:12:40.221Z","modified":"2022-08-06T14:44:34.587Z"},"maintainers":[{"name":"8pattern","email":"eighth.principle@gmail.com"}],"description":"a command-line helper for Node.js","homepage":"https://github.com/8th-pattern/jCommand#readme","keywords":["commond-line"],"repository":{"type":"git","url":"git+https://github.com/8th-pattern/jCommand.git"},"author":{"name":"8th-pattern"},"bugs":{"url":"https://github.com/8th-pattern/jCommand/issues"},"license":"MIT","readme":"# jCommand\r\n A command-line helper for Node.js\r\n\r\n\r\n\r\n## Install\r\n\r\n```shell\r\nnpm install -S @8pattern/jcommand\r\n```\r\n\r\n\r\n\r\n## Usage\r\n\r\nDemo\r\n\r\n```javascript\r\n// cmd.js\r\nconst jCommand = require('@8pattern/jcommand')\r\njCommand.option(['-P', '--port'], (cmd) => console.log(cmd.value))\r\n```\r\n\r\n```shell\r\nnode cmd.js -P=3000 or node cmd.js --port=3000\r\n# the console will print 3000 \r\n```\r\n\r\n\r\n\r\n+ Attribute\r\n  \r\n  1. **execPath** &lt;string&gt;: the path of the executor. It's Node root path in ordinary.\r\n     \r\n     ```shell\r\n     node cmd.js\r\n     # jCommand.execPath === 'D:\\\\Node\\\\node.exe'\r\n     ```\r\n     \r\n  2. **filePath** &lt;string&gt;: the path of the code file.\r\n     \r\n     ```shell\r\n     node cmd.js\r\n     # jCommand.filePath === 'D:\\\\cmd.js'\r\n     ```\r\n     \r\n  3. args &lt;string[]&gt;: the original arguments in command-line.\r\n  \r\n     ```shell\r\n     node cmd.js -a -b=1 c:2\r\n     # jCommand.args === ['-a', '-b=1', 'c:2']\r\n     ```\r\n  \r\n  5. **commands** &lt;Command[]&gt; the list of argument wraps. *Command* is a wrapper for argument. It has five attributes: **raw**，**command**, **value**, **pre** and **next**.  **raw** represents the original string of the argument. **command** and **value** will be automatically detect the argument to obtain the corresponding results (according to **\"=\"** now). If they can't be detected from the raw, command will equal the raw while the value will be assigned to be null. **pre** and **next** link the previous or next argument wrap instance. For example:\r\n  \r\n     | Argument |   -a   | -b=1 |  c:2   |\r\n     | :------: | :----: | :--: | :----: |\r\n     |   raw    |   -a   | -b=1 |  c:2   |\r\n     | command  |   -a   |  -b  |  c:2   |\r\n     |  value   | *null* |  1   | *null* |\r\n     \r\n     ```shell\r\n     node cmd.js -a -b=1 c:2\r\n     # jCommand.commands[1].raw === '-b=1'\r\n     # jCommand.commands[1].pre.raw === '-a'\r\n     # jCommand.commands[1].next.raw === 'c:2'\r\n     ```\r\n\r\n+ Action\r\n\r\n  1. **option** (command: string | string[], callback: Function): jCommand\r\n  + exactly match the received commands\r\n  \r\n   ```javascript\r\n     jCommand.option('-P', callback)\r\n     jCommand.option(['-P', '--port'], callback)\r\n   ```\r\n  \r\n  2. **match** (command: RegExp, callback: Function): jCommands\r\n  \r\n   + provide a regular expression to match all valid argument\r\n  \r\n     ```javascript\r\n     jCommand.option(/--?P/i, callback)\r\n     ```\r\n  \r\n  3. **fuzzy** (command: string | string[], callback: Function): jCommand  *or* (command: string | string[], prefix: string | string[], callback: Function): jCommand\r\n  \r\n     + match the arguments in a case insensitive mode\r\n     + the prefix argument represents the prefix chars before the argument, **['-', '--'] by default**\r\n  \r\n     ```javascript\r\n     jCommand.fuzzy('p', callback) // sames as jCommand.fuzzy('p', ['-', '--'], callback)\r\n     jCommand.fuzzy(['p', 'port'], '-', callback)\r\n     ```\r\n  \r\n  4. **valid** (validator: Function, callback: Function): jCommand\r\n  \r\n     + can provide a custom validator to match the expected arguments.\r\n     + validator can receive the only argument —— a **Command** instance. If return **true**, the command will be regarded as selected. And **false** otherwise.\r\n     + In fact, **option**, **match** and **fuzzy** are three particular cases of **valid**.\r\n  \r\n     \r\n  \r\n  The following presents whether the arguments will trigger the corresponding rules.\r\n  \r\n  |                              |  -p  |  -P  | --p  | -p=3000 | --port=3000 |\r\n  | :--------------------------: | :--: | :--: | :--: | :-----: | :---------: |\r\n  |       option('-p', cb)       | Yes  |  No  |  No  |   Yes   |     No      |\r\n  | option(['-p', '--port'], cb) | Yes  |  No  |  No  |   Yes   |     Yes     |\r\n  |       match(/-p/, cb)        | Yes  |  No  | Yes  |   Yes   |     Yes     |\r\n  |      match(/--P/i, cb)       |  No  |  No  | Yes  |   No    |     Yes     |\r\n  |     match(/^--?p$/i, cb)     | Yes  | Yes  | Yes  |   Yes   |     No      |\r\n  |        fuzzy('p', cb)        | Yes  | Yes  | Yes  |   Yes   |     No      |\r\n  |     fuzzy('p', '@', cb)      |  No  |  No  |  No  |   No    |     No      |\r\n  \r\n  **NOTICE**\r\n  \r\n  1. the actions can be called by chains.\r\n  \r\n  ```javascript\r\n  const t = jCommand\r\n      .option('-p',() => {})\r\n      .match(/w/, () => {})\r\n  \t.fuzzy('m', () => {})\r\n  \t.valud(() => true, () => {})\r\n  \r\n  console.log(t === jCommand) // true\r\n  ```\r\n  \r\n  \r\n  \r\n  2. callback has the only argument of the **Command** instance. If need the previous or rest command-line arguments, they will can be found by **pre** and **next** attributes.\r\n  \r\n  ```javascript\r\n  jCommand.option('-P', (cmd) => {\r\n  \tconsole.log(cmd.next.command)\r\n  })\r\n  // node cmd.js -P 3000  -> print 3000\r\n  ```\r\n  \r\n  3. only if the rule satisfied, the callback will be triggered, even if it was triggered before. In other words, the callback may be triggered **more than once**.\r\n  \r\n  ```javascript\r\n  jCommand\r\n  \t.option('-P', (cmd) => console.log('option:', cmd.command))\r\n  \t.match(/p/i, (cmd) => console.log('match: ', cmd.command))\r\n  \t.fuzzy('p', (cmd) => console.log('fuzzy: ', cmd.command))\r\n  \t.valid((cmd) => /p/i.test(cmd.command), (cmd) => console.log('valid: ', cmd.command))\r\n  \r\n  // node cms.js -P\r\n  // print:\r\n  //\toption: -P\r\n  //  match: -P\r\n  //  fuzzy: -P\r\n  //  valid: -P\r\n  ```","readmeFilename":"README.md"}