argv-flags 0.2.0 → 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.
- package/README.md +10 -0
- package/index.js +35 -7
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -19,6 +19,16 @@ const count = parseFlag('--count', 'number');
|
|
|
19
19
|
const items = parseFlag('--items', 'array');
|
|
20
20
|
```
|
|
21
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
|
+
|
|
22
32
|
### Boolean flags
|
|
23
33
|
|
|
24
34
|
Boolean flags accept explicit values when provided:
|
package/index.js
CHANGED
|
@@ -16,17 +16,41 @@ const normalizeBooleanValue = ( value ) => {
|
|
|
16
16
|
return undefined;
|
|
17
17
|
};
|
|
18
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
|
+
|
|
19
37
|
function parseFlag( targetArgument, argumentType, argv = process.argv ) {
|
|
20
38
|
const processArguments = Array.isArray( argv ) ? argv : process.argv;
|
|
21
|
-
const
|
|
39
|
+
const match = findFlagMatch( processArguments, targetArgument );
|
|
22
40
|
|
|
23
|
-
if (
|
|
41
|
+
if ( ! match ) {
|
|
24
42
|
return false;
|
|
25
43
|
}
|
|
26
44
|
|
|
27
|
-
const
|
|
45
|
+
const inlineValue = Object.prototype.hasOwnProperty.call( match, 'inlineValue' )
|
|
46
|
+
? match.inlineValue
|
|
47
|
+
: undefined;
|
|
48
|
+
const nextValue = processArguments[ match.index + 1 ];
|
|
28
49
|
|
|
29
50
|
if ( argumentType === 'string' ) {
|
|
51
|
+
if ( typeof inlineValue === 'string' ) {
|
|
52
|
+
return inlineValue;
|
|
53
|
+
}
|
|
30
54
|
if ( typeof nextValue !== 'string' || isLongFlag( nextValue ) ) {
|
|
31
55
|
return false;
|
|
32
56
|
}
|
|
@@ -35,7 +59,10 @@ function parseFlag( targetArgument, argumentType, argv = process.argv ) {
|
|
|
35
59
|
|
|
36
60
|
if ( argumentType === 'array' ) {
|
|
37
61
|
const values = [];
|
|
38
|
-
|
|
62
|
+
if ( typeof inlineValue === 'string' && inlineValue.length > 0 ) {
|
|
63
|
+
values.push( inlineValue );
|
|
64
|
+
}
|
|
65
|
+
for ( let i = match.index + 1; i < processArguments.length; i++ ) {
|
|
39
66
|
const value = processArguments[ i ];
|
|
40
67
|
if ( isLongFlag( value ) ) {
|
|
41
68
|
break;
|
|
@@ -46,7 +73,7 @@ function parseFlag( targetArgument, argumentType, argv = process.argv ) {
|
|
|
46
73
|
}
|
|
47
74
|
|
|
48
75
|
if ( argumentType === 'boolean' ) {
|
|
49
|
-
const normalizedValue = normalizeBooleanValue( nextValue );
|
|
76
|
+
const normalizedValue = normalizeBooleanValue( typeof inlineValue === 'string' ? inlineValue : nextValue );
|
|
50
77
|
if ( typeof normalizedValue === 'boolean' ) {
|
|
51
78
|
return normalizedValue;
|
|
52
79
|
}
|
|
@@ -54,10 +81,11 @@ function parseFlag( targetArgument, argumentType, argv = process.argv ) {
|
|
|
54
81
|
}
|
|
55
82
|
|
|
56
83
|
if ( argumentType === 'number' ) {
|
|
57
|
-
|
|
84
|
+
const candidate = typeof inlineValue === 'string' ? inlineValue : nextValue;
|
|
85
|
+
if ( typeof candidate !== 'string' || isLongFlag( candidate ) ) {
|
|
58
86
|
return false;
|
|
59
87
|
}
|
|
60
|
-
const parsed = Number(
|
|
88
|
+
const parsed = Number( candidate );
|
|
61
89
|
return Number.isNaN( parsed ) ? false : parsed;
|
|
62
90
|
}
|
|
63
91
|
|