argv-flags 0.1.1 → 0.2.1

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 +61 -0
  2. package/index.d.ts +7 -0
  3. package/index.js +81 -29
  4. package/package.json +22 -7
package/README.md CHANGED
@@ -1 +1,62 @@
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
+ ### Inline values
23
+
24
+ Flags can be passed as `--flag=value` for string, number, boolean, and array types:
25
+
26
+ ```sh
27
+ --enabled=false
28
+ --count=5
29
+ --items=a b
30
+ ```
31
+
32
+ ### Boolean flags
33
+
34
+ Boolean flags accept explicit values when provided:
35
+
36
+ ```sh
37
+ --enabled true
38
+ --enabled false
39
+ ```
40
+
41
+ If no value is provided, the presence of the flag returns `true`.
42
+
43
+ ### Array flags
44
+
45
+ Array flags collect values until the next `--flag`:
46
+
47
+ ```sh
48
+ --items a b c --other
49
+ ```
50
+
51
+ ### Testing / custom argv
52
+
53
+ You can pass a custom argv array (useful for tests):
54
+
55
+ ```js
56
+ const argv = ['node', 'script.js', '--flag', 'true'];
57
+ parseFlag('--flag', 'boolean', argv);
58
+ ```
59
+
60
+ ## License
61
+
62
+ 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,92 @@
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
+ const findFlagMatch = ( argv, targetArgument ) => {
20
+ const directIndex = argv.indexOf( targetArgument );
21
+ if ( directIndex !== -1 ) {
22
+ return { index: directIndex };
23
+ }
24
+
25
+ const prefix = `${targetArgument}=`;
26
+ const inlineIndex = argv.findIndex( ( value ) => typeof value === 'string' && value.startsWith( prefix ) );
27
+ if ( inlineIndex === -1 ) {
28
+ return null;
29
+ }
30
+
31
+ return {
32
+ index: inlineIndex,
33
+ inlineValue: argv[ inlineIndex ].slice( prefix.length )
34
+ };
35
+ };
36
+
37
+ function parseFlag( targetArgument, argumentType, argv = process.argv ) {
38
+ const processArguments = Array.isArray( argv ) ? argv : process.argv;
39
+ const match = findFlagMatch( processArguments, targetArgument );
40
+
41
+ if ( ! match ) {
42
+ return false;
43
+ }
44
+
45
+ const inlineValue = Object.prototype.hasOwnProperty.call( match, 'inlineValue' )
46
+ ? match.inlineValue
47
+ : undefined;
48
+ const nextValue = processArguments[ match.index + 1 ];
49
+
50
+ if ( argumentType === 'string' ) {
51
+ if ( typeof inlineValue === 'string' ) {
52
+ return inlineValue;
8
53
  }
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
- }
54
+ if ( typeof nextValue !== 'string' || isLongFlag( nextValue ) ) {
55
+ return false;
56
+ }
57
+ return nextValue;
58
+ }
19
59
 
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
- }
60
+ if ( argumentType === 'array' ) {
61
+ const values = [];
62
+ if ( typeof inlineValue === 'string' && inlineValue.length > 0 ) {
63
+ values.push( inlineValue );
64
+ }
65
+ for ( let i = match.index + 1; i < processArguments.length; i++ ) {
66
+ const value = processArguments[ i ];
67
+ if ( isLongFlag( value ) ) {
68
+ break;
28
69
  }
70
+ values.push( value );
29
71
  }
30
-
31
- if ( argumentType === 'boolean' ) {
32
- return true;
72
+ return values;
73
+ }
74
+
75
+ if ( argumentType === 'boolean' ) {
76
+ const normalizedValue = normalizeBooleanValue( typeof inlineValue === 'string' ? inlineValue : nextValue );
77
+ if ( typeof normalizedValue === 'boolean' ) {
78
+ return normalizedValue;
33
79
  }
34
-
35
- if ( argumentType === 'number' ) {
36
- return Number( processArguments[processArguments.indexOf( targetArgument ) + 1] );
80
+ return true;
81
+ }
82
+
83
+ if ( argumentType === 'number' ) {
84
+ const candidate = typeof inlineValue === 'string' ? inlineValue : nextValue;
85
+ if ( typeof candidate !== 'string' || isLongFlag( candidate ) ) {
86
+ return false;
37
87
  }
88
+ const parsed = Number( candidate );
89
+ return Number.isNaN( parsed ) ? false : parsed;
38
90
  }
39
91
 
40
92
  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.1",
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
  }