argv-flags 0.1.1 → 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.
Files changed (4) hide show
  1. package/README.md +51 -0
  2. package/index.d.ts +7 -0
  3. package/index.js +53 -29
  4. package/package.json +22 -7
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
@@ -0,0 +1,7 @@
1
+ declare function parseFlag(
2
+ flag: string,
3
+ type?: 'string' | 'boolean' | 'array' | 'number',
4
+ argv?: string[]
5
+ ): string | boolean | string[] | number | false;
6
+
7
+ export = parseFlag;
package/index.js CHANGED
@@ -1,40 +1,64 @@
1
- function parseFlag ( targetArgument, argumentType ) {
2
- const processArguments = process.argv;
1
+ 'use strict';
3
2
 
4
- if ( processArguments.includes( targetArgument ) ) {
3
+ const isLongFlag = ( value ) => typeof value === 'string' && value.startsWith( '--' );
5
4
 
6
- if ( argumentType === 'string' ) {
7
- return processArguments[processArguments.indexOf( targetArgument ) + 1];
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;
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 ];
28
+
29
+ if ( argumentType === 'string' ) {
30
+ if ( typeof nextValue !== 'string' || isLongFlag( nextValue ) ) {
31
+ return false;
8
32
  }
9
-
10
- if ( argumentType === 'array' ) {
11
-
12
- let flagsIndex = [];
13
-
14
- for ( const key in processArguments ) {
15
- if ( processArguments[key].charAt( 0 ) === '-' && processArguments[key].charAt( 0 ) === '-' ) {
16
- flagsIndex.push( [ processArguments[key], key ] );
17
- }
18
- }
33
+ return nextValue;
34
+ }
19
35
 
20
- for ( const key in flagsIndex ) {
21
- if ( flagsIndex[key][0] === targetArgument ) {
22
- if ( flagsIndex[key][1] < flagsIndex[flagsIndex.length - 1][1] ) {
23
- return processArguments.slice( Number( flagsIndex[key][1] ) + 1, flagsIndex[Number( key ) + 1][1] );
24
- } else {
25
- return processArguments.slice( Number( flagsIndex[key][1] ) + 1 );
26
- }
27
- }
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;
28
42
  }
43
+ values.push( value );
29
44
  }
30
-
31
- if ( argumentType === 'boolean' ) {
32
- return true;
45
+ return values;
46
+ }
47
+
48
+ if ( argumentType === 'boolean' ) {
49
+ const normalizedValue = normalizeBooleanValue( nextValue );
50
+ if ( typeof normalizedValue === 'boolean' ) {
51
+ return normalizedValue;
33
52
  }
34
-
35
- if ( argumentType === 'number' ) {
36
- return Number( processArguments[processArguments.indexOf( targetArgument ) + 1] );
53
+ return true;
54
+ }
55
+
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
  }
39
63
 
40
64
  return false;
package/package.json CHANGED
@@ -1,20 +1,35 @@
1
1
  {
2
2
  "name": "argv-flags",
3
- "version": "0.1.1",
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
+ "types": "index.d.ts",
7
+ "files": [
8
+ "LICENSE",
9
+ "README.md",
10
+ "index.js",
11
+ "index.d.ts"
12
+ ],
6
13
  "scripts": {
7
- "test": "echo \"Error: no test specified\" && exit 1"
14
+ "test": "node --test"
8
15
  },
9
16
  "repository": {
10
17
  "type": "git",
11
18
  "url": "git+https://github.com/Ismail-elkorchi/argv-flags.git"
12
19
  },
13
- "keywords": [],
14
- "author": "",
15
- "license": "ISC",
20
+ "keywords": [
21
+ "argv",
22
+ "cli",
23
+ "flags",
24
+ "parser"
25
+ ],
26
+ "author": "Ismail El Korchi",
27
+ "license": "MIT",
16
28
  "bugs": {
17
29
  "url": "https://github.com/Ismail-elkorchi/argv-flags/issues"
18
30
  },
19
- "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
+ }
20
35
  }