argv-flags 0.1.0 → 0.2.0
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.
- package/README.md +51 -0
- package/index.d.ts +7 -0
- package/index.js +53 -27
- package/package.json +22 -8
package/README.md
CHANGED
|
@@ -1 +1,52 @@
|
|
|
1
1
|
# argv-flags
|
|
2
|
+
|
|
3
|
+
Tiny helper for parsing simple CLI flags from `process.argv`.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```sh
|
|
8
|
+
npm install argv-flags
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Usage
|
|
12
|
+
|
|
13
|
+
```js
|
|
14
|
+
const parseFlag = require('argv-flags');
|
|
15
|
+
|
|
16
|
+
const name = parseFlag('--name', 'string');
|
|
17
|
+
const enabled = parseFlag('--enabled', 'boolean');
|
|
18
|
+
const count = parseFlag('--count', 'number');
|
|
19
|
+
const items = parseFlag('--items', 'array');
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
### Boolean flags
|
|
23
|
+
|
|
24
|
+
Boolean flags accept explicit values when provided:
|
|
25
|
+
|
|
26
|
+
```sh
|
|
27
|
+
--enabled true
|
|
28
|
+
--enabled false
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
If no value is provided, the presence of the flag returns `true`.
|
|
32
|
+
|
|
33
|
+
### Array flags
|
|
34
|
+
|
|
35
|
+
Array flags collect values until the next `--flag`:
|
|
36
|
+
|
|
37
|
+
```sh
|
|
38
|
+
--items a b c --other
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
### Testing / custom argv
|
|
42
|
+
|
|
43
|
+
You can pass a custom argv array (useful for tests):
|
|
44
|
+
|
|
45
|
+
```js
|
|
46
|
+
const argv = ['node', 'script.js', '--flag', 'true'];
|
|
47
|
+
parseFlag('--flag', 'boolean', argv);
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
## License
|
|
51
|
+
|
|
52
|
+
MIT
|
package/index.d.ts
ADDED
package/index.js
CHANGED
|
@@ -1,41 +1,67 @@
|
|
|
1
|
-
|
|
2
|
-
const processArguments = process.argv;
|
|
3
|
-
let argsIndex = [];
|
|
1
|
+
'use strict';
|
|
4
2
|
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
3
|
+
const isLongFlag = ( value ) => typeof value === 'string' && value.startsWith( '--' );
|
|
4
|
+
|
|
5
|
+
const normalizeBooleanValue = ( value ) => {
|
|
6
|
+
if ( typeof value !== 'string' ) {
|
|
7
|
+
return undefined;
|
|
8
|
+
}
|
|
9
|
+
const normalized = value.toLowerCase();
|
|
10
|
+
if ( normalized === 'true' ) {
|
|
11
|
+
return true;
|
|
12
|
+
}
|
|
13
|
+
if ( normalized === 'false' ) {
|
|
14
|
+
return false;
|
|
9
15
|
}
|
|
16
|
+
return undefined;
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
function parseFlag( targetArgument, argumentType, argv = process.argv ) {
|
|
20
|
+
const processArguments = Array.isArray( argv ) ? argv : process.argv;
|
|
21
|
+
const targetIndex = processArguments.indexOf( targetArgument );
|
|
22
|
+
|
|
23
|
+
if ( targetIndex === -1 ) {
|
|
24
|
+
return false;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
const nextValue = processArguments[ targetIndex + 1 ];
|
|
10
28
|
|
|
11
|
-
|
|
12
|
-
if (
|
|
13
|
-
return
|
|
29
|
+
if ( argumentType === 'string' ) {
|
|
30
|
+
if ( typeof nextValue !== 'string' || isLongFlag( nextValue ) ) {
|
|
31
|
+
return false;
|
|
14
32
|
}
|
|
33
|
+
return nextValue;
|
|
34
|
+
}
|
|
15
35
|
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
return processArguments.slice( Number( argsIndex[key][1] ) + 1 );
|
|
23
|
-
}
|
|
24
|
-
}
|
|
36
|
+
if ( argumentType === 'array' ) {
|
|
37
|
+
const values = [];
|
|
38
|
+
for ( let i = targetIndex + 1; i < processArguments.length; i++ ) {
|
|
39
|
+
const value = processArguments[ i ];
|
|
40
|
+
if ( isLongFlag( value ) ) {
|
|
41
|
+
break;
|
|
25
42
|
}
|
|
43
|
+
values.push( value );
|
|
26
44
|
}
|
|
45
|
+
return values;
|
|
46
|
+
}
|
|
27
47
|
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
return false;
|
|
48
|
+
if ( argumentType === 'boolean' ) {
|
|
49
|
+
const normalizedValue = normalizeBooleanValue( nextValue );
|
|
50
|
+
if ( typeof normalizedValue === 'boolean' ) {
|
|
51
|
+
return normalizedValue;
|
|
33
52
|
}
|
|
53
|
+
return true;
|
|
54
|
+
}
|
|
34
55
|
|
|
35
|
-
|
|
36
|
-
|
|
56
|
+
if ( argumentType === 'number' ) {
|
|
57
|
+
if ( typeof nextValue !== 'string' || isLongFlag( nextValue ) ) {
|
|
58
|
+
return false;
|
|
37
59
|
}
|
|
60
|
+
const parsed = Number( nextValue );
|
|
61
|
+
return Number.isNaN( parsed ) ? false : parsed;
|
|
38
62
|
}
|
|
63
|
+
|
|
64
|
+
return false;
|
|
39
65
|
}
|
|
40
66
|
|
|
41
|
-
|
|
67
|
+
module.exports = parseFlag;
|
package/package.json
CHANGED
|
@@ -1,21 +1,35 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "argv-flags",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"description": "",
|
|
3
|
+
"version": "0.2.0",
|
|
4
|
+
"description": "Minimal CLI flag parser for strings, booleans, numbers, and arrays.",
|
|
5
5
|
"main": "index.js",
|
|
6
|
-
"
|
|
6
|
+
"types": "index.d.ts",
|
|
7
|
+
"files": [
|
|
8
|
+
"LICENSE",
|
|
9
|
+
"README.md",
|
|
10
|
+
"index.js",
|
|
11
|
+
"index.d.ts"
|
|
12
|
+
],
|
|
7
13
|
"scripts": {
|
|
8
|
-
"test": "
|
|
14
|
+
"test": "node --test"
|
|
9
15
|
},
|
|
10
16
|
"repository": {
|
|
11
17
|
"type": "git",
|
|
12
18
|
"url": "git+https://github.com/Ismail-elkorchi/argv-flags.git"
|
|
13
19
|
},
|
|
14
|
-
"keywords": [
|
|
15
|
-
|
|
16
|
-
|
|
20
|
+
"keywords": [
|
|
21
|
+
"argv",
|
|
22
|
+
"cli",
|
|
23
|
+
"flags",
|
|
24
|
+
"parser"
|
|
25
|
+
],
|
|
26
|
+
"author": "Ismail El Korchi",
|
|
27
|
+
"license": "MIT",
|
|
17
28
|
"bugs": {
|
|
18
29
|
"url": "https://github.com/Ismail-elkorchi/argv-flags/issues"
|
|
19
30
|
},
|
|
20
|
-
"homepage": "https://github.com/Ismail-elkorchi/argv-flags#readme"
|
|
31
|
+
"homepage": "https://github.com/Ismail-elkorchi/argv-flags#readme",
|
|
32
|
+
"engines": {
|
|
33
|
+
"node": ">=18"
|
|
34
|
+
}
|
|
21
35
|
}
|