clp 4.0.13 → 4.0.14

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (4) hide show
  1. package/LICENSE +1 -1
  2. package/README.md +15 -172
  3. package/lib/index.js +82 -66
  4. package/package.json +1 -2
package/LICENSE CHANGED
@@ -1,6 +1,6 @@
1
1
  The MIT License (MIT)
2
2
 
3
- Copyright (c) 2015-25 Ionică Bizău <bizauionica@gmail.com> (https://ionicabizau.net)
3
+ Copyright (c) 2015-26 Ionică Bizău <bizauionica@gmail.com> (https://ionicabizau.net)
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy
6
6
  of this software and associated documentation files (the "Software"), to deal
package/README.md CHANGED
@@ -1,41 +1,13 @@
1
1
  <!-- Please do not edit this file. Edit the `blah` field in the `package.json` instead. If in doubt, open an issue. -->
2
2
 
3
-
4
-
5
-
6
-
7
-
8
-
9
-
10
-
11
-
12
-
13
-
14
-
15
-
16
-
17
-
18
-
19
-
20
3
  # clp
21
4
 
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)
5
+ [![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://app.travis-ci.com/IonicaBizau/clp.svg)](https://app.travis-ci.com/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
6
 
24
7
  <a href="https://www.buymeacoffee.com/H96WwChMy" target="_blank"><img src="https://www.buymeacoffee.com/assets/img/custom_images/yellow_img.png" alt="Buy Me A Coffee"></a>
25
8
 
26
-
27
-
28
-
29
-
30
-
31
-
32
9
  > A tiny and fast command line arguments parser.
33
10
 
34
-
35
-
36
-
37
-
38
-
39
11
  ## :rocket: Migration from `3.x.x` to `4.x.x`
40
12
 
41
13
 
@@ -45,18 +17,6 @@ arguments. Use [**`tilda`**](https://github.com/IonicaBizau/tilda)
45
17
 
46
18
  for a high-level interface for building cli tools.
47
19
 
48
-
49
-
50
-
51
-
52
-
53
-
54
-
55
-
56
-
57
-
58
-
59
-
60
20
  ## :cloud: Installation
61
21
 
62
22
  ```sh
@@ -67,22 +27,8 @@ npm install --save clp
67
27
  yarn add clp
68
28
  ```
69
29
 
70
-
71
-
72
-
73
-
74
-
75
-
76
-
77
-
78
-
79
-
80
-
81
-
82
30
  ## :clipboard: Example
83
31
 
84
-
85
-
86
32
  ```js
87
33
  #!/usr/bin/env node
88
34
 
@@ -95,156 +41,53 @@ console.log(clp(["--name", "Johnny", "-a", "20", "--location", "Earth", "--no-st
95
41
  // { _: [], name: 'Johnny', a: 20, location: 'Earth', student: false }
96
42
  ```
97
43
 
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
44
  ## :memo: Documentation
126
45
 
127
-
128
46
  ### `clp(args)`
129
47
  Parses the cli arguments.
130
48
 
131
49
  #### Params
132
-
133
50
  - **Array** `args`: The arguments to parse (default: the process arguments).
134
51
 
135
52
  #### Return
136
53
  - **Object** An object containing the parsed arguments.
137
54
 
55
+ ## :question: Get Help
138
56
 
57
+ There are a few ways to get help:
139
58
 
59
+ 1. Please [post questions on Stack Overflow](https://stackoverflow.com/questions/ask). You can open issues with questions, as long as you add a link to your Stack Overflow question.
60
+ 2. For bug reports and feature requests, open issues. :bug:
61
+ 3. For direct and quick help, you can [use Codementor](https://www.codementor.io/johnnyb). :rocket:
140
62
 
63
+ ## :yum: How to Contribute
141
64
 
142
-
143
-
144
-
145
-
146
-
147
-
148
-
149
-
150
-
151
- ## :yum: How to contribute
152
65
  Have an idea? Found a bug? See [how to contribute][contributing].
153
66
 
67
+ ## :sparkling_heart: Support My Projects
154
68
 
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:
69
+ I open-source almost everything I can, and I try to reply to everyone needing help using these projects. Obviously, this takes time. You can integrate and use these projects in your applications *for free*! You can even change the source code and redistribute it (even resell it).
160
70
 
71
+ However, if you get some profit from this or just want to encourage me to continue creating stuff, there are a few ways you can do it:
161
72
 
162
73
  - 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:
74
+ - [![Buy me a book][badge_amazon]][amazon]—I love books! I will remember you for years if you buy me one. :grin: :book:
164
75
  - [![PayPal][badge_paypal]][paypal-donations]—You can make one-time donations via PayPal. I'll probably buy a ~~coffee~~ tea. :tea:
165
76
  - [![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)
77
+ - **Bitcoin**—You can send me bitcoins at this address (or by scanning the code below): `1P9BRsmazNQcuyTxEqveUsnf5CERdq35V6`
169
78
 
79
+ ![Bitcoin QR code](https://i.imgur.com/z6OQI95.png)
170
80
 
171
81
  Thanks! :heart:
172
82
 
173
-
174
-
175
-
176
-
177
-
178
-
179
-
180
-
181
83
  ## :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
84
 
85
+ This package is heavily based on [`minimist`](https://github.com/substack/minimist). :sparkles:
238
86
 
239
87
  ## :scroll: License
240
88
 
241
89
  [MIT][license] © [Ionică Bizău][website]
242
90
 
243
-
244
-
245
-
246
-
247
-
248
91
  [license]: /LICENSE
249
92
  [website]: https://ionicabizau.net
250
93
  [contributing]: /CONTRIBUTING.md
@@ -254,5 +97,5 @@ If you are using this library in one of your projects, add it in this list. :spa
254
97
  [badge_paypal]: https://ionicabizau.github.io/badges/paypal.svg
255
98
  [badge_paypal_donate]: https://ionicabizau.github.io/badges/paypal_donate.svg
256
99
  [patreon]: https://www.patreon.com/ionicabizau
257
- [amazon]: http://amzn.eu/hRo9sIZ
100
+ [amazon]: https://amzn.eu/hRo9sIZ
258
101
  [paypal-donations]: https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=RVXDDLKKLQRJW
package/lib/index.js CHANGED
@@ -1,18 +1,19 @@
1
1
  "use strict";
2
2
 
3
- var isNumber = require("is-number"),
4
- matchIt = require("match-it"),
5
- lastChar = require("last-char");
3
+ const isNumber = require("is-number")
4
+ , matchIt = require("match-it")
5
+ , lastChar = require("last-char")
6
+ ;
6
7
 
7
8
  // Part of the code from this file was originally written by
8
9
  // the contributors in https://github.com/substack/minimist
9
- function hasKey(obj, keys) {
10
- var o = obj;
11
- keys.slice(0, -1).forEach(function (key) {
12
- o = o[key] || {};
10
+ function hasKey (obj, keys) {
11
+ let o = obj;
12
+ keys.slice(0,-1).forEach(function (key) {
13
+ o = (o[key] || {});
13
14
  });
14
15
 
15
- var key = keys[keys.length - 1];
16
+ let key = keys[keys.length - 1];
16
17
  return key in o;
17
18
  }
18
19
 
@@ -25,52 +26,59 @@ function hasKey(obj, keys) {
25
26
  * @param {Array} args The arguments to parse (default: the process arguments).
26
27
  * @returns {Object} An object containing the parsed arguments.
27
28
  */
28
- module.exports = function clp(args) {
29
+ module.exports = function clp (args) {
29
30
  args = args || process.argv.slice(2);
30
31
 
31
- var flags = {
32
- bools: {},
33
- strings: {}
34
- },
35
- argv = { _: [] },
36
- notFlags = [],
37
- dashDashIndex = args.indexOf("--");
32
+ let flags = {
33
+ bools : {}
34
+ , strings : {}
35
+ }
36
+ , argv = { _ : [] }
37
+ , notFlags = []
38
+ , dashDashIndex = args.indexOf("--")
39
+ ;
38
40
 
39
41
  function argDefined(key, arg) {
40
- return flags.allBools && /^--[^=]+$/.test(arg) || flags.strings[key] || flags.bools[key] || aliases[key];
42
+ return (flags.allBools && /^--[^=]+$/.test(arg))
43
+ || flags.strings[key] || flags.bools[key] || aliases[key]
44
+ ;
41
45
  }
42
46
 
43
- function setArg(key, val, arg) {
47
+ function setArg (key, val, arg) {
44
48
  if (arg && flags.unknownFn && !argDefined(key, arg)) {
45
49
  if (flags.unknownFn(arg) === false) return;
46
50
  }
47
51
 
48
- var value = !flags.strings[key] && isNumber(val) ? Number(val) : val;
52
+ let value = !flags.strings[key] && isNumber(val)
53
+ ? Number(val) : val
54
+ ;
49
55
 
50
56
  setKey(argv, key.split("."), value);
51
57
  }
52
58
 
53
- function setKey(obj, keys, value) {
54
- var o = obj;
55
- keys.slice(0, -1).forEach(function (key) {
59
+ function setKey (obj, keys, value) {
60
+ let o = obj;
61
+ keys.slice(0,-1).forEach(function (key) {
56
62
  if (o[key] === undefined) o[key] = {};
57
63
  o = o[key];
58
64
  });
59
65
 
60
- var key = keys[keys.length - 1];
66
+ let key = keys[keys.length - 1];
61
67
  if (o[key] === undefined || flags.bools[key] || typeof o[key] === "boolean") {
62
68
  o[key] = value;
63
- } else if (Array.isArray(o[key])) {
69
+ }
70
+ else if (Array.isArray(o[key])) {
64
71
  o[key].push(value);
65
- } else {
66
- o[key] = [o[key], value];
72
+ }
73
+ else {
74
+ o[key] = [ o[key], value ];
67
75
  }
68
76
  }
69
77
 
70
78
  function aliasIsBoolean(key) {
71
- return aliases[key].some(function (x) {
72
- return flags.bools[x];
73
- });
79
+ return aliases[key].some(function (x) {
80
+ return flags.bools[x];
81
+ });
74
82
  }
75
83
 
76
84
  if (~dashDashIndex) {
@@ -78,65 +86,71 @@ module.exports = function clp(args) {
78
86
  args = args.slice(0, dashDashIndex);
79
87
  }
80
88
 
81
- for (var i = 0; i < args.length; ++i) {
82
- var arg = args[i],
83
- oneOf = {
84
- doubleDashEqual: matchIt(arg, /^--([^=]+)=([\s\S]*)$/),
85
- negative: matchIt(arg, /^--no-(.+)/),
86
- doubleDash: matchIt(arg, /^--(.+)/)
87
- };
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
+ ;
88
98
 
89
99
  if (oneOf.doubleDashEqual.length) {
90
- var key = oneOf.doubleDashEqual[1],
91
- value = oneOf.doubleDashEqual[2];
100
+ let key = oneOf.doubleDashEqual[1]
101
+ , value = oneOf.doubleDashEqual[2]
102
+ ;
92
103
 
93
104
  setArg(key, value, arg);
94
105
  } else if (oneOf.negative.length) {
95
106
  setArg(oneOf.negative[1], false, arg);
96
107
  } else if (oneOf.doubleDash.length) {
97
- var _key = oneOf.doubleDash[1],
98
- next = args[i + 1];
108
+ let key = oneOf.doubleDash[1]
109
+ , next = args[i + 1]
110
+ ;
99
111
 
100
112
  if (next !== undefined && next.charAt(0) !== "-") {
101
- setArg(_key, next, arg);
113
+ setArg(key, next, arg);
102
114
  i++;
103
115
  } else {
104
- var isTrue = next === "true";
116
+ let isTrue = next === "true";
105
117
  if (isTrue || next === "false") {
106
- setArg(_key, isTrue, arg);
118
+ setArg(key, isTrue, arg);
107
119
  i++;
108
120
  } else {
109
- setArg(_key, flags.strings[_key] ? "" : true, arg);
121
+ setArg(key, flags.strings[key] ? "" : true, arg);
110
122
  }
111
123
  }
112
124
  } else if (/^-[^-]+/.test(arg)) {
113
- var letters = arg.slice(1, -1).split(""),
114
- broken = false;
125
+ let letters = arg.slice(1,-1).split("")
126
+ , broken = false
127
+ ;
115
128
 
116
- for (var j = 0; j < letters.length; ++j) {
117
- var _next = arg.slice(j + 2),
118
- cLetter = letters[j];
129
+ for (let j = 0; j < letters.length; ++j) {
130
+ let next = arg.slice(j + 2)
131
+ , cLetter = letters[j]
132
+ ;
119
133
 
120
- if (_next === "-") {
121
- setArg(cLetter, _next, arg);
134
+ if (next === "-") {
135
+ setArg(cLetter, next, arg)
122
136
  continue;
123
137
  }
124
138
 
125
- if (/[A-Za-z]/.test(cLetter) && _next.includes("=")) {
126
- setArg(cLetter, _next.split("=")[1], arg);
139
+ if (/[A-Za-z]/.test(cLetter) && next.includes("=")) {
140
+ setArg(cLetter, next.split("=")[1], arg);
127
141
  broken = true;
128
142
  break;
129
143
  }
130
144
 
131
- if (/[A-Za-z]/.test(cLetter) && /-?\d+(\.\d*)?(e-?\d+)?$/.test(_next)) {
132
- setArg(cLetter, _next, arg);
145
+ if (/[A-Za-z]/.test(cLetter) && /-?\d+(\.\d*)?(e-?\d+)?$/.test(next)) {
146
+ setArg(cLetter, next, arg);
133
147
  broken = true;
134
148
  break;
135
149
  }
136
150
 
137
- var nextLetter = letters[j + 1];
151
+ let nextLetter = letters[j + 1];
138
152
  if (nextLetter && nextLetter.match(/\W/)) {
139
- setArg(cLetter, _next, arg);
153
+ setArg(cLetter, next, arg);
140
154
  broken = true;
141
155
  break;
142
156
  } else {
@@ -144,27 +158,29 @@ module.exports = function clp(args) {
144
158
  }
145
159
  }
146
160
 
147
- var _key2 = lastChar(arg);
148
- if (!broken && _key2 !== "-") {
149
- var nextArg = args[i + 1];
161
+ let key = lastChar(arg);
162
+ if (!broken && key !== "-") {
163
+ let nextArg = args[i + 1];
150
164
  if (nextArg && !/^(-|--)[^-]/.test(nextArg)) {
151
- setArg(_key2, args[i + 1], arg);
165
+ setArg(key, args[i+1], arg);
152
166
  i++;
153
167
  } else if (nextArg && /true|false/.test(nextArg)) {
154
- setArg(_key2, nextArg === "true", arg);
168
+ setArg(key, nextArg === "true", arg);
155
169
  i++;
156
170
  } else {
157
- setArg(_key2, true, arg);
171
+ setArg(key, true, arg);
158
172
  }
159
173
  }
160
174
  } else {
161
- argv._.push(flags.strings["_"] || !isNumber(arg) ? arg : Number(arg));
175
+ argv._.push(
176
+ flags.strings["_"] || !isNumber(arg) ? arg : Number(arg)
177
+ );
162
178
  }
163
179
  }
164
180
 
165
- notFlags.forEach(function (key) {
181
+ notFlags.forEach(function(key) {
166
182
  argv._.push(key);
167
183
  });
168
184
 
169
185
  return argv;
170
- };
186
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "clp",
3
- "version": "4.0.13",
3
+ "version": "4.0.14",
4
4
  "description": "A tiny and fast command line arguments parser.",
5
5
  "main": "lib/index.js",
6
6
  "directories": {
@@ -64,7 +64,6 @@
64
64
  "cli.js",
65
65
  "index.js",
66
66
  "index.d.ts",
67
- "package-lock.json",
68
67
  "bloggify.js",
69
68
  "bloggify.json",
70
69
  "bloggify/"