├── .github └── FUNDING.yml ├── .gitignore ├── .travis.yml ├── CONTRIBUTING.md ├── DOCUMENTATION.md ├── LICENSE ├── README.md ├── examples └── index.js ├── lib └── index.js ├── package.json └── test └── index.js /.github/FUNDING.yml: -------------------------------------------------------------------------------- 1 | github: ionicabizau 2 | patreon: ionicabizau 3 | open_collective: ionicabizau 4 | custom: https://www.buymeacoffee.com/h96wwchmy -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *.swp 2 | *.swo 3 | *~ 4 | *.log 5 | node_modules 6 | *.env 7 | .DS_Store 8 | package-lock.json 9 | .bloggify/* 10 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | node_js: 3 | - 4 4 | -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | # 🌟 Contributing 2 | 3 | Want to contribute to this project? Great! Please read these quick steps to streamline the process and avoid unnecessary tasks. ✨ 4 | 5 | ## 💬 Discuss Changes 6 | Start by opening an issue in the repository using the [bug tracker][1]. Describe your proposed contribution or the bug you've found. If relevant, include platform info and screenshots. 🖼️ 7 | 8 | Wait for feedback before proceeding unless the fix is straightforward, like a typo. 📝 9 | 10 | ## 🔧 Fixing Issues 11 | 12 | Fork the project and create a branch for your fix, naming it `some-great-feature` or `some-issue-fix`. Commit changes while following the [code style][2]. If the project has tests, add one. ✅ 13 | 14 | If a `package.json` or `bower.json` exists, add yourself to the `contributors` array; create it if it doesn't. 🙌 15 | 16 | ```json 17 | { 18 | "contributors": [ 19 | "Your Name (http://your.website)" 20 | ] 21 | } 22 | ``` 23 | 24 | ## 📬 Creating a Pull Request 25 | Open a pull request and reference the initial issue (e.g., *fixes #*). Provide a clear title and consider adding visual aids for clarity. 📊 26 | 27 | ## ⏳ Wait for Feedback 28 | Your contributions will be reviewed. If feedback is given, update your branch as needed, and the pull request will auto-update. 🔄 29 | 30 | ## 🎉 Everyone Is Happy! 31 | Your contributions will be merged, and everyone will appreciate your effort! 😄❤️ 32 | 33 | Thanks! 🤩 34 | 35 | [1]: /issues 36 | [2]: https://github.com/IonicaBizau/code-style -------------------------------------------------------------------------------- /DOCUMENTATION.md: -------------------------------------------------------------------------------- 1 | ## Documentation 2 | 3 | You can see below the API reference of this module. 4 | 5 | ### `clp(args)` 6 | Parses the cli arguments. 7 | 8 | #### Params 9 | 10 | - **Array** `args`: The arguments to parse (default: the process arguments). 11 | 12 | #### Return 13 | - **Object** An object containing the parsed arguments. 14 | 15 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015-25 Ionică Bizău (https://ionicabizau.net) 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | # clp 21 | 22 | [![Support me on Patreon][badge_patreon]][patreon] [![Buy me a book][badge_amazon]][amazon] [![PayPal][badge_paypal_donate]][paypal-donations] [![Ask me anything](https://img.shields.io/badge/ask%20me-anything-1abc9c.svg)](https://github.com/IonicaBizau/ama) [![Travis](https://img.shields.io/travis/IonicaBizau/clp.svg)](https://travis-ci.org/IonicaBizau/clp/) [![Version](https://img.shields.io/npm/v/clp.svg)](https://www.npmjs.com/package/clp) [![Downloads](https://img.shields.io/npm/dt/clp.svg)](https://www.npmjs.com/package/clp) [![Get help on Codementor](https://cdn.codementor.io/badges/get_help_github.svg)](https://www.codementor.io/@johnnyb?utm_source=github&utm_medium=button&utm_term=johnnyb&utm_campaign=github) 23 | 24 | Buy Me A Coffee 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | > A tiny and fast command line arguments parser. 33 | 34 | 35 | 36 | 37 | 38 | 39 | ## :rocket: Migration from `3.x.x` to `4.x.x` 40 | 41 | 42 | As of `4.0.0` the scope of this package will be to simply parse 43 | 44 | arguments. Use [**`tilda`**](https://github.com/IonicaBizau/tilda) 45 | 46 | for a high-level interface for building cli tools. 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | ## :cloud: Installation 61 | 62 | ```sh 63 | # Using npm 64 | npm install --save clp 65 | 66 | # Using yarn 67 | yarn add clp 68 | ``` 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | ## :clipboard: Example 83 | 84 | 85 | 86 | ```js 87 | #!/usr/bin/env node 88 | 89 | const clp = require("clp"); 90 | 91 | console.log(clp(["grep", "-ri", "foo"])); 92 | // { _: [ 'grep' ], r: true, i: 'foo' } 93 | 94 | console.log(clp(["--name", "Johnny", "-a", "20", "--location", "Earth", "--no-student"])); 95 | // { _: [], name: 'Johnny', a: 20, location: 'Earth', student: false } 96 | ``` 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | ## :question: Get Help 110 | 111 | There are few ways to get help: 112 | 113 | 114 | 115 | 1. Please [post questions on Stack Overflow](https://stackoverflow.com/questions/ask). You can open issues with questions, as long you add a link to your Stack Overflow question. 116 | 2. For bug reports and feature requests, open issues. :bug: 117 | 3. For direct and quick help, you can [use Codementor](https://www.codementor.io/johnnyb). :rocket: 118 | 119 | 120 | 121 | 122 | 123 | 124 | 125 | ## :memo: Documentation 126 | 127 | 128 | ### `clp(args)` 129 | Parses the cli arguments. 130 | 131 | #### Params 132 | 133 | - **Array** `args`: The arguments to parse (default: the process arguments). 134 | 135 | #### Return 136 | - **Object** An object containing the parsed arguments. 137 | 138 | 139 | 140 | 141 | 142 | 143 | 144 | 145 | 146 | 147 | 148 | 149 | 150 | 151 | ## :yum: How to contribute 152 | Have an idea? Found a bug? See [how to contribute][contributing]. 153 | 154 | 155 | ## :sparkling_heart: Support my projects 156 | I open-source almost everything I can, and I try to reply to everyone needing help using these projects. Obviously, 157 | this takes time. You can integrate and use these projects in your applications *for free*! You can even change the source code and redistribute (even resell it). 158 | 159 | However, if you get some profit from this or just want to encourage me to continue creating stuff, there are few ways you can do it: 160 | 161 | 162 | - Starring and sharing the projects you like :rocket: 163 | - [![Buy me a book][badge_amazon]][amazon]—I love books! I will remember you after years if you buy me one. :grin: :book: 164 | - [![PayPal][badge_paypal]][paypal-donations]—You can make one-time donations via PayPal. I'll probably buy a ~~coffee~~ tea. :tea: 165 | - [![Support me on Patreon][badge_patreon]][patreon]—Set up a recurring monthly donation and you will get interesting news about what I'm doing (things that I don't share with everyone). 166 | - **Bitcoin**—You can send me bitcoins at this address (or scanning the code below): `1P9BRsmazNQcuyTxEqveUsnf5CERdq35V6` 167 | 168 | ![](https://i.imgur.com/z6OQI95.png) 169 | 170 | 171 | Thanks! :heart: 172 | 173 | 174 | 175 | 176 | 177 | 178 | 179 | 180 | 181 | ## :cake: Thanks 182 | This package is heavily based on [`minimist`](https://github.com/substack/minimist). :sparkles: 183 | 184 | 185 | 186 | 187 | 188 | 189 | 190 | 191 | ## :dizzy: Where is this library used? 192 | If you are using this library in one of your projects, add it in this list. :sparkles: 193 | 194 | - `3abn` 195 | - `@isysd/gpm` 196 | - `a-csv` 197 | - `arc-asm` 198 | - `birthday` 199 | - `cdnjs-importer` 200 | - `diable` 201 | - `electron-comrade` 202 | - `emojer-cli` 203 | - `engine-tools` 204 | - `gh-notifier` 205 | - `ghcal` 206 | - `git-issues` 207 | - `git-stats-importer` 208 | - `github-emojify` 209 | - `github-labeller` 210 | - `github-stats` 211 | - `gpm` 212 | - `grokodile` 213 | - `image-to-ascii-cli` 214 | - `kindly-license` 215 | - `name-it` 216 | - `namly` 217 | - `namy` 218 | - `npmreserve` 219 | - `photon-browser` 220 | - `rucksack.js` 221 | - `ssb-chat` 222 | - `ssh-remote` 223 | - `tilda` 224 | - `tinyreq-cli` 225 | - `tithe` 226 | - `wrabbit` 227 | - `zotero-plugin` 228 | 229 | 230 | 231 | 232 | 233 | 234 | 235 | 236 | 237 | 238 | 239 | ## :scroll: License 240 | 241 | [MIT][license] © [Ionică Bizău][website] 242 | 243 | 244 | 245 | 246 | 247 | 248 | [license]: /LICENSE 249 | [website]: https://ionicabizau.net 250 | [contributing]: /CONTRIBUTING.md 251 | [docs]: /DOCUMENTATION.md 252 | [badge_patreon]: https://ionicabizau.github.io/badges/patreon.svg 253 | [badge_amazon]: https://ionicabizau.github.io/badges/amazon.svg 254 | [badge_paypal]: https://ionicabizau.github.io/badges/paypal.svg 255 | [badge_paypal_donate]: https://ionicabizau.github.io/badges/paypal_donate.svg 256 | [patreon]: https://www.patreon.com/ionicabizau 257 | [amazon]: http://amzn.eu/hRo9sIZ 258 | [paypal-donations]: https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=RVXDDLKKLQRJW 259 | -------------------------------------------------------------------------------- /examples/index.js: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env node 2 | 3 | const clp = require("../lib"); 4 | 5 | console.log(clp(["grep", "-ri", "foo"])); 6 | // { _: [ 'grep' ], r: true, i: 'foo' } 7 | 8 | console.log(clp(["--name", "Johnny", "-a", "20", "--location", "Earth", "--no-student"])); 9 | // { _: [], name: 'Johnny', a: 20, location: 'Earth', student: false } 10 | -------------------------------------------------------------------------------- /lib/index.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | const isNumber = require("is-number") 4 | , matchIt = require("match-it") 5 | , lastChar = require("last-char") 6 | ; 7 | 8 | // Part of the code from this file was originally written by 9 | // the contributors in https://github.com/substack/minimist 10 | function hasKey (obj, keys) { 11 | let o = obj; 12 | keys.slice(0,-1).forEach(function (key) { 13 | o = (o[key] || {}); 14 | }); 15 | 16 | let key = keys[keys.length - 1]; 17 | return key in o; 18 | } 19 | 20 | /** 21 | * clp 22 | * Parses the cli arguments. 23 | * 24 | * @name clp 25 | * @function 26 | * @param {Array} args The arguments to parse (default: the process arguments). 27 | * @returns {Object} An object containing the parsed arguments. 28 | */ 29 | module.exports = function clp (args) { 30 | args = args || process.argv.slice(2); 31 | 32 | let flags = { 33 | bools : {} 34 | , strings : {} 35 | } 36 | , argv = { _ : [] } 37 | , notFlags = [] 38 | , dashDashIndex = args.indexOf("--") 39 | ; 40 | 41 | function argDefined(key, arg) { 42 | return (flags.allBools && /^--[^=]+$/.test(arg)) 43 | || flags.strings[key] || flags.bools[key] || aliases[key] 44 | ; 45 | } 46 | 47 | function setArg (key, val, arg) { 48 | if (arg && flags.unknownFn && !argDefined(key, arg)) { 49 | if (flags.unknownFn(arg) === false) return; 50 | } 51 | 52 | let value = !flags.strings[key] && isNumber(val) 53 | ? Number(val) : val 54 | ; 55 | 56 | setKey(argv, key.split("."), value); 57 | } 58 | 59 | function setKey (obj, keys, value) { 60 | let o = obj; 61 | keys.slice(0,-1).forEach(function (key) { 62 | if (o[key] === undefined) o[key] = {}; 63 | o = o[key]; 64 | }); 65 | 66 | let key = keys[keys.length - 1]; 67 | if (o[key] === undefined || flags.bools[key] || typeof o[key] === "boolean") { 68 | o[key] = value; 69 | } 70 | else if (Array.isArray(o[key])) { 71 | o[key].push(value); 72 | } 73 | else { 74 | o[key] = [ o[key], value ]; 75 | } 76 | } 77 | 78 | function aliasIsBoolean(key) { 79 | return aliases[key].some(function (x) { 80 | return flags.bools[x]; 81 | }); 82 | } 83 | 84 | if (~dashDashIndex) { 85 | notFlags = args.slice(dashDashIndex + 1); 86 | args = args.slice(0, dashDashIndex); 87 | } 88 | 89 | 90 | for (let i = 0; i < args.length; ++i) { 91 | let arg = args[i] 92 | , oneOf = { 93 | doubleDashEqual: matchIt(arg, /^--([^=]+)=([\s\S]*)$/) 94 | , negative: matchIt(arg, /^--no-(.+)/) 95 | , doubleDash: matchIt(arg, /^--(.+)/) 96 | } 97 | ; 98 | 99 | if (oneOf.doubleDashEqual.length) { 100 | let key = oneOf.doubleDashEqual[1] 101 | , value = oneOf.doubleDashEqual[2] 102 | ; 103 | 104 | setArg(key, value, arg); 105 | } else if (oneOf.negative.length) { 106 | setArg(oneOf.negative[1], false, arg); 107 | } else if (oneOf.doubleDash.length) { 108 | let key = oneOf.doubleDash[1] 109 | , next = args[i + 1] 110 | ; 111 | 112 | if (next !== undefined && next.charAt(0) !== "-") { 113 | setArg(key, next, arg); 114 | i++; 115 | } else { 116 | let isTrue = next === "true"; 117 | if (isTrue || next === "false") { 118 | setArg(key, isTrue, arg); 119 | i++; 120 | } else { 121 | setArg(key, flags.strings[key] ? "" : true, arg); 122 | } 123 | } 124 | } else if (/^-[^-]+/.test(arg)) { 125 | let letters = arg.slice(1,-1).split("") 126 | , broken = false 127 | ; 128 | 129 | for (let j = 0; j < letters.length; ++j) { 130 | let next = arg.slice(j + 2) 131 | , cLetter = letters[j] 132 | ; 133 | 134 | if (next === "-") { 135 | setArg(cLetter, next, arg) 136 | continue; 137 | } 138 | 139 | if (/[A-Za-z]/.test(cLetter) && next.includes("=")) { 140 | setArg(cLetter, next.split("=")[1], arg); 141 | broken = true; 142 | break; 143 | } 144 | 145 | if (/[A-Za-z]/.test(cLetter) && /-?\d+(\.\d*)?(e-?\d+)?$/.test(next)) { 146 | setArg(cLetter, next, arg); 147 | broken = true; 148 | break; 149 | } 150 | 151 | let nextLetter = letters[j + 1]; 152 | if (nextLetter && nextLetter.match(/\W/)) { 153 | setArg(cLetter, next, arg); 154 | broken = true; 155 | break; 156 | } else { 157 | setArg(cLetter, flags.strings[cLetter] ? "" : true, arg); 158 | } 159 | } 160 | 161 | let key = lastChar(arg); 162 | if (!broken && key !== "-") { 163 | let nextArg = args[i + 1]; 164 | if (nextArg && !/^(-|--)[^-]/.test(nextArg)) { 165 | setArg(key, args[i+1], arg); 166 | i++; 167 | } else if (nextArg && /true|false/.test(nextArg)) { 168 | setArg(key, nextArg === "true", arg); 169 | i++; 170 | } else { 171 | setArg(key, true, arg); 172 | } 173 | } 174 | } else { 175 | argv._.push( 176 | flags.strings["_"] || !isNumber(arg) ? arg : Number(arg) 177 | ); 178 | } 179 | } 180 | 181 | notFlags.forEach(function(key) { 182 | argv._.push(key); 183 | }); 184 | 185 | return argv; 186 | }; 187 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "clp", 3 | "version": "4.0.13", 4 | "description": "A tiny and fast command line arguments parser.", 5 | "main": "lib/index.js", 6 | "directories": { 7 | "example": "examples", 8 | "test": "test" 9 | }, 10 | "scripts": { 11 | "test": "node test" 12 | }, 13 | "repository": { 14 | "type": "git", 15 | "url": "git@github.com:IonicaBizau/clp.git" 16 | }, 17 | "keywords": [ 18 | "clp", 19 | "command", 20 | "line", 21 | "arguments", 22 | "parser" 23 | ], 24 | "author": "Ionică Bizău (https://ionicabizau.net)", 25 | "license": "MIT", 26 | "bugs": { 27 | "url": "https://github.com/IonicaBizau/clp/issues" 28 | }, 29 | "homepage": "https://github.com/IonicaBizau/clp", 30 | "dependencies": { 31 | "is-number": "^2.1.0", 32 | "last-char": "^1.3.1", 33 | "match-it": "^1.0.0" 34 | }, 35 | "devDependencies": { 36 | "tester": "^1.3.1" 37 | }, 38 | "blah": { 39 | "description": [ 40 | { 41 | "h2": ":rocket: Migration from `3.x.x` to `4.x.x`" 42 | }, 43 | { 44 | "p": [ 45 | [ 46 | "As of `4.0.0` the scope of this package will be to simply parse", 47 | "arguments. Use [**`tilda`**](https://github.com/IonicaBizau/tilda)", 48 | "for a high-level interface for building cli tools." 49 | ] 50 | ] 51 | } 52 | ], 53 | "thanks": "This package is heavily based on [`minimist`](https://github.com/substack/minimist). :sparkles:" 54 | }, 55 | "files": [ 56 | "bin/", 57 | "app/", 58 | "lib/", 59 | "dist/", 60 | "src/", 61 | "scripts/", 62 | "resources/", 63 | "menu/", 64 | "cli.js", 65 | "index.js", 66 | "index.d.ts", 67 | "package-lock.json", 68 | "bloggify.js", 69 | "bloggify.json", 70 | "bloggify/" 71 | ] 72 | } -------------------------------------------------------------------------------- /test/index.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | const tester = require("tester") 4 | , clp = require("..") 5 | ; 6 | 7 | tester.describe("clp", test => { 8 | 9 | test.should("parse complex options", () => { 10 | test.expect(clp(["x", "-fo", "--test", "bar"])).toEqual({ 11 | f: true, 12 | o: true, 13 | test: "bar", 14 | _ : ["x"] 15 | }); 16 | }); 17 | 18 | test.should("handle --", () => { 19 | test.expect(clp(["-f", "--", "-fo", "--test", "bar"])).toEqual({ 20 | f: true, 21 | _ : ["-fo", "--test", "bar"] 22 | }); 23 | }); 24 | 25 | test.it("boolean options", () => { 26 | var argv = clp([ "-x", "-z", "one", "two", "three" ]); 27 | test.expect(argv).toEqual({ 28 | x: true, 29 | z: "one", 30 | _ : [ "two", "three" ] 31 | }); 32 | }); 33 | 34 | test.it("boolean and --x=true", () => { 35 | var parsed = clp(["--boool", "--other=true"]); 36 | 37 | test.expect(parsed.boool).toBe(true); 38 | test.expect(parsed.other).toBe("true"); 39 | 40 | parsed = clp(["--boool", "--other=false"]); 41 | test.expect(parsed.boool).toEqual(true); 42 | test.expect(parsed.other).toEqual("false"); 43 | }); 44 | }); 45 | --------------------------------------------------------------------------------