argv2object 1.1.0 → 1.1.2
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 +26 -9
- package/index.cjs +15 -15
- package/index.mjs +15 -15
- package/package.json +18 -13
- package/src/argv2Object.mjs +18 -23
- package/src/constants.mjs +56 -23
- package/src/functions.mjs +54 -15
package/README.md
CHANGED
|
@@ -7,6 +7,7 @@ A utility function for converting command-line arguments to a key-value object.
|
|
|
7
7
|
- [Installation](#installation)
|
|
8
8
|
- [Importing](#importing)
|
|
9
9
|
- [Usage](#usage)
|
|
10
|
+
- [Dual Module Support](#dual-module-support)
|
|
10
11
|
- [Examples](#examples)
|
|
11
12
|
- [Simple key-value pairs](#simple-key-value-pairs)
|
|
12
13
|
- [Unix-style command-line options](#unix-style-command-line-options)
|
|
@@ -61,34 +62,49 @@ console.log(args.name); // "John"
|
|
|
61
62
|
|
|
62
63
|
```
|
|
63
64
|
|
|
65
|
+
## Dual Module Support
|
|
66
|
+
|
|
67
|
+
This package supports both:
|
|
68
|
+
|
|
69
|
+
- ES Modules: `import argv2object from 'argv2object'`
|
|
70
|
+
- CommonJS: `const argv2object = require('argv2object')`
|
|
71
|
+
|
|
72
|
+
The architecture uses separate entry points while sharing the same implementation.
|
|
73
|
+
|
|
74
|
+
## System Requirements
|
|
75
|
+
|
|
76
|
+
- **Node.js 14+** (Recommended for full support)
|
|
77
|
+
- **Node.js 12.x** (Experimental support)
|
|
78
|
+
- **Versiones anteriores**: Use version 1.x of this package
|
|
79
|
+
|
|
64
80
|
## Examples
|
|
65
81
|
|
|
66
82
|
### Simple key-value pairs
|
|
67
83
|
|
|
68
84
|
```sh
|
|
69
85
|
# Command line input:
|
|
70
|
-
node script.js task=some-task name=John level=admin
|
|
86
|
+
node script.js task=some-task age=30 name=John level=admin
|
|
71
87
|
|
|
72
88
|
```
|
|
73
89
|
|
|
74
90
|
```js
|
|
75
91
|
const args = argv2Object();
|
|
76
92
|
console.log(args);
|
|
77
|
-
// Output: { name: 'John', age:
|
|
93
|
+
// Output: { name: 'John', age: 30, level: 'admin' }
|
|
78
94
|
```
|
|
79
95
|
|
|
80
96
|
### Unix-style command-line options
|
|
81
97
|
|
|
82
98
|
```sh
|
|
83
99
|
# Command line input:
|
|
84
|
-
node --watch script.js -h --name=John --is-admin
|
|
100
|
+
node --watch script.js -h --help --name=John --is-admin
|
|
85
101
|
|
|
86
102
|
```
|
|
87
103
|
|
|
88
104
|
```js
|
|
89
105
|
const args = argv2Object(true);
|
|
90
106
|
console.log(args);
|
|
91
|
-
// Output: { h: true, name: 'John', is_admin: true }
|
|
107
|
+
// Output: { h: true, help: true, name: 'John', is_admin: true }
|
|
92
108
|
```
|
|
93
109
|
|
|
94
110
|
## API
|
|
@@ -113,11 +129,12 @@ Returns an `Object` with keys and values corresponding to the provided arguments
|
|
|
113
129
|
|
|
114
130
|
Throws
|
|
115
131
|
|
|
116
|
-
| Type | Description
|
|
117
|
-
|
|
118
|
-
| `
|
|
119
|
-
| `Error`
|
|
120
|
-
| `Error`
|
|
132
|
+
| Type | Description |
|
|
133
|
+
|---------|-------------------------------------------------------------------------------------------------|
|
|
134
|
+
| `TypeError` | If `unixmode` is not of type `boolean`. |
|
|
135
|
+
| `Error` | If no arguments are provided from command line. |
|
|
136
|
+
| `Error` | If `unixmode` is `true` and an argument does not follow the Unix-style command-line format. |
|
|
137
|
+
| `Error` | If `unixmode` is `false` and an argument does not follow the "key=value" format. |
|
|
121
138
|
|
|
122
139
|
## Contributing
|
|
123
140
|
|
package/index.cjs
CHANGED
|
@@ -1,22 +1,22 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* @
|
|
3
|
-
* @file Manages main entry point.
|
|
4
|
-
*/
|
|
5
|
-
|
|
6
|
-
// ━━ TYPE DEFINITIONS ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
|
|
7
|
-
/**
|
|
8
|
-
* The `argv2Object()` function, converts command-line arguments to a key-value
|
|
9
|
-
* object.
|
|
2
|
+
* @file Entry point for CommonJS consumers.
|
|
10
3
|
*
|
|
11
|
-
*
|
|
12
|
-
*
|
|
13
|
-
* The format can be either simple key-value pairs (e.g. "key=value") or
|
|
14
|
-
* Unix-style command-line options (e.g. "-o --option=value").
|
|
4
|
+
* Provides compatibility layer for Node.js environments
|
|
5
|
+
* using CommonJS require() system.
|
|
15
6
|
*
|
|
16
|
-
*
|
|
17
|
-
*
|
|
18
|
-
*
|
|
7
|
+
* This wrapper adapts the ESM default export to CommonJS
|
|
8
|
+
* module.exports pattern.
|
|
9
|
+
*
|
|
10
|
+
* @module argv2object/cjs
|
|
11
|
+
* @see module:argv2Object
|
|
12
|
+
* @since 1.1.0
|
|
13
|
+
* @example
|
|
14
|
+
* // CJS Usage
|
|
15
|
+
* const argv2object = require('argv2object');
|
|
19
16
|
*/
|
|
17
|
+
|
|
18
|
+
// ━━ IMPORT MODULES ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
|
|
19
|
+
// » IMPORT LOCAL MODULES
|
|
20
20
|
const argv2Object = require('#argv2Object');
|
|
21
21
|
|
|
22
22
|
// ━━ EXPORT MODULE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
|
package/index.mjs
CHANGED
|
@@ -1,22 +1,22 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* @
|
|
3
|
-
* @file Manages main entry point.
|
|
4
|
-
*/
|
|
5
|
-
|
|
6
|
-
// ━━ TYPE DEFINITIONS ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
|
|
7
|
-
/**
|
|
8
|
-
* The `argv2Object()` function, converts command-line arguments to a key-value
|
|
9
|
-
* object.
|
|
2
|
+
* @file Entry point for ES Module consumers.
|
|
10
3
|
*
|
|
11
|
-
*
|
|
12
|
-
*
|
|
13
|
-
* The format can be either simple key-value pairs (e.g. "key=value") or
|
|
14
|
-
* Unix-style command-line options (e.g. "-o --option=value").
|
|
4
|
+
* Provides the default export of argv2object functionality
|
|
5
|
+
* for modern JavaScript environments using ES Modules.
|
|
15
6
|
*
|
|
16
|
-
*
|
|
17
|
-
*
|
|
18
|
-
*
|
|
7
|
+
* This file re-exports the main functionality from the
|
|
8
|
+
* implementation module while maintaining ESM compatibility.
|
|
9
|
+
*
|
|
10
|
+
* @module argv2object/esm
|
|
11
|
+
* @see module:argv2Object
|
|
12
|
+
* @since 1.1.0
|
|
13
|
+
* @example
|
|
14
|
+
* // ESM Usage
|
|
15
|
+
* import argv2object from 'argv2object';
|
|
19
16
|
*/
|
|
17
|
+
|
|
18
|
+
// ━━ IMPORT MODULES ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
|
|
19
|
+
// » IMPORT LOCAL MODULES
|
|
20
20
|
import argv2Object from '#argv2Object';
|
|
21
21
|
|
|
22
22
|
// ━━ EXPORT MODULE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "argv2object",
|
|
3
|
-
"version": "1.1.
|
|
3
|
+
"version": "1.1.2",
|
|
4
4
|
"private": false,
|
|
5
5
|
"description": "A utility function for converting command-line arguments to a key-value object.",
|
|
6
6
|
"keywords": [
|
|
@@ -49,24 +49,29 @@
|
|
|
49
49
|
"scripts": {
|
|
50
50
|
"test": "cross-env NODE_ENV=test node --test test/*.test.mjs",
|
|
51
51
|
"lint": "npx eslint . --ext .mjs --ext .js --ext .cjs",
|
|
52
|
-
"prepare": "husky
|
|
52
|
+
"prepare": "husky",
|
|
53
53
|
"prepublish": "npm test"
|
|
54
54
|
},
|
|
55
55
|
"devDependencies": {
|
|
56
|
-
"@commitlint/cli": "^
|
|
57
|
-
"@commitlint/config-conventional": "^
|
|
58
|
-
"
|
|
59
|
-
"eslint": "^
|
|
56
|
+
"@commitlint/cli": "^20.1.0",
|
|
57
|
+
"@commitlint/config-conventional": "^20.0.0",
|
|
58
|
+
"@eslint/compat": "^1.4.1",
|
|
59
|
+
"@eslint/core": "^0.17.0",
|
|
60
|
+
"@eslint/eslintrc": "^3.3.3",
|
|
61
|
+
"cross-env": "^10.1.0",
|
|
62
|
+
"eslint": "^9.39.1",
|
|
60
63
|
"eslint-config-airbnb-base": "^15.0.0",
|
|
61
|
-
"eslint-config-prettier": "^
|
|
64
|
+
"eslint-config-prettier": "^10.1.8",
|
|
65
|
+
"eslint-import-resolver-alias": "^1.1.2",
|
|
62
66
|
"eslint-import-resolver-node": "^0.3.9",
|
|
63
|
-
"eslint-plugin-import": "^2.
|
|
64
|
-
"eslint-plugin-
|
|
65
|
-
"
|
|
66
|
-
"
|
|
67
|
-
"
|
|
67
|
+
"eslint-plugin-import": "^2.32.0",
|
|
68
|
+
"eslint-plugin-jsdoc": "^61.4.1",
|
|
69
|
+
"eslint-plugin-prettier": "^5.5.4",
|
|
70
|
+
"husky": "^9.1.7",
|
|
71
|
+
"lint-staged": "^16.2.7",
|
|
72
|
+
"prettier": "^3.7.3"
|
|
68
73
|
},
|
|
69
74
|
"engines": {
|
|
70
|
-
"node": ">=
|
|
75
|
+
"node": ">=14.0.0"
|
|
71
76
|
}
|
|
72
77
|
}
|
package/src/argv2Object.mjs
CHANGED
|
@@ -10,8 +10,7 @@
|
|
|
10
10
|
* arguments: simple key-value pairs (e.g. "key=value") and Unix-style
|
|
11
11
|
* command-line arguments (e.g. "-k value" or "--key=value").
|
|
12
12
|
*
|
|
13
|
-
* @
|
|
14
|
-
* @version 1.0.0
|
|
13
|
+
* @version 1.1.0
|
|
15
14
|
*
|
|
16
15
|
* @example
|
|
17
16
|
* Simple key-value pairs
|
|
@@ -25,8 +24,8 @@
|
|
|
25
24
|
*/
|
|
26
25
|
// ━━ IMPORT MODULES ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
|
|
27
26
|
// » IMPORT LOCAL MODULES
|
|
28
|
-
import {
|
|
29
|
-
import {
|
|
27
|
+
import { COMMAND_LINE_PATTERNS, ERROR_MESSAGES } from '#constants';
|
|
28
|
+
import { createObjectFromArguments } from '#functions';
|
|
30
29
|
|
|
31
30
|
// ━━ TYPE DEFINITIONS ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
|
|
32
31
|
/**
|
|
@@ -47,8 +46,9 @@ import { formatKey, convertValue } from '#functions';
|
|
|
47
46
|
* Unix-style command-line options (e.g. "-o --option=value").
|
|
48
47
|
*
|
|
49
48
|
* @function
|
|
50
|
-
* @param {boolean} [unixmode
|
|
49
|
+
* @param {boolean} [unixmode] - Whether to parse Unix-style command-line options, the default value is `false`.
|
|
51
50
|
* @returns {ArgvObject} Returns an object with keys and values corresponding to the provided arguments.
|
|
51
|
+
* @throws {TypeError} If `unixmode` is not is not of type `boolean`.
|
|
52
52
|
* @throws {Error} If no arguments are provided from command line.
|
|
53
53
|
* @throws {Error} If `unixmode` is `true` and argument does not follow Unix-style command-line format.
|
|
54
54
|
* @throws {Error} If `unixmode` is `false` and argument does not follow 'key=value' format.
|
|
@@ -56,38 +56,33 @@ import { formatKey, convertValue } from '#functions';
|
|
|
56
56
|
* ```js
|
|
57
57
|
* // Command line input: node script.js --task=some-task --execute=true
|
|
58
58
|
*
|
|
59
|
-
* const
|
|
60
|
-
* console.log(
|
|
61
|
-
* console.log(
|
|
59
|
+
* const argumentsv = argv2Object();
|
|
60
|
+
* console.log(argumentsv.task); // prints 'some-task'
|
|
61
|
+
* console.log(argumentsv.execute); // prints true
|
|
62
62
|
* ```
|
|
63
63
|
*/
|
|
64
64
|
const argv2Object = (unixmode = false) => {
|
|
65
65
|
if (typeof unixmode !== 'boolean') {
|
|
66
|
-
throw new TypeError(
|
|
66
|
+
throw new TypeError(ERROR_MESSAGES.INVALID_UNIXMODE_TYPE);
|
|
67
67
|
}
|
|
68
68
|
|
|
69
69
|
const argumentsv = process.argv.slice(2);
|
|
70
70
|
if (argumentsv.length === 0) {
|
|
71
|
-
throw new Error(
|
|
71
|
+
throw new Error(ERROR_MESSAGES.NO_ARGUMENTS);
|
|
72
72
|
}
|
|
73
73
|
|
|
74
|
-
const
|
|
74
|
+
const patterns = unixmode
|
|
75
|
+
? [COMMAND_LINE_PATTERNS.UNIX_LONG, COMMAND_LINE_PATTERNS.UNIX_SHORT]
|
|
76
|
+
: [COMMAND_LINE_PATTERNS.SIMPLE];
|
|
75
77
|
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
78
|
+
const isValid = argumentsv.every(argument => patterns.some(pattern => pattern.test(argument)));
|
|
79
|
+
|
|
80
|
+
if (!isValid) {
|
|
81
|
+
const message = unixmode ? ERROR_MESSAGES.NO_MATCH_UNIXMODE : ERROR_MESSAGES.NO_MATCH_SIMPLE;
|
|
80
82
|
throw new Error(message);
|
|
81
83
|
}
|
|
82
84
|
|
|
83
|
-
|
|
84
|
-
const [k, v] = argumentv.split('=');
|
|
85
|
-
const key = formatKey(k, 'snakecase');
|
|
86
|
-
const value = convertValue(v);
|
|
87
|
-
return [key, value];
|
|
88
|
-
});
|
|
89
|
-
|
|
90
|
-
return Object.fromEntries(entries);
|
|
85
|
+
return createObjectFromArguments(argumentsv, 'snakecase');
|
|
91
86
|
};
|
|
92
87
|
|
|
93
88
|
// ━━ EXPORT MODULE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
|
package/src/constants.mjs
CHANGED
|
@@ -12,11 +12,12 @@
|
|
|
12
12
|
|
|
13
13
|
// ━━ TYPE DEFINITIONS ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
|
|
14
14
|
/**
|
|
15
|
-
*
|
|
15
|
+
* Type definition for command line argument patterns.
|
|
16
16
|
*
|
|
17
|
-
* @typedef {object}
|
|
18
|
-
* @property {RegExp}
|
|
19
|
-
* @property {RegExp}
|
|
17
|
+
* @typedef {object} CommandLinePatterns
|
|
18
|
+
* @property {RegExp} UNIX_SHORT - Matches short Unix flags: `-a`, `-v`, `-f=value`.
|
|
19
|
+
* @property {RegExp} UNIX_LONG - Matches long Unix flags: `--help`, `--output=json`.
|
|
20
|
+
* @property {RegExp} SIMPLE - Pattern for simple key=value arguments: key=value, multi-part-key=value.
|
|
20
21
|
*/
|
|
21
22
|
|
|
22
23
|
/**
|
|
@@ -25,30 +26,60 @@
|
|
|
25
26
|
* @typedef {object} ErrorMessages
|
|
26
27
|
* @property {string} INVALID_UNIXMODE_TYPE - When unixmode parameter is not boolean.
|
|
27
28
|
* @property {string} NO_ARGUMENTS - When no arguments are provided.
|
|
28
|
-
* @property {string} NO_MATCH_SIMPLE - When simple args don't match key=value format
|
|
29
|
-
* @property {string} NO_MATCH_UNIXMODE - When args don't match Unix-style format
|
|
29
|
+
* @property {string} NO_MATCH_SIMPLE - When simple args don't match key=value format.
|
|
30
|
+
* @property {string} NO_MATCH_UNIXMODE - When args don't match Unix-style format.
|
|
30
31
|
*/
|
|
31
32
|
|
|
32
33
|
// ━━ MODULE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
|
|
33
34
|
/**
|
|
34
|
-
* Regular
|
|
35
|
+
* Regular expression patterns for validating command line argument formats.
|
|
36
|
+
*
|
|
37
|
+
* Provides comprehensive pattern matching for different command line argument
|
|
38
|
+
* styles commonly used in Node.js applications and CLI tools.
|
|
35
39
|
*
|
|
36
40
|
* @exports
|
|
37
41
|
* @private
|
|
38
|
-
* @constant
|
|
39
|
-
* @type {
|
|
42
|
+
* @constant COMMAND_LINE_PATTERNS
|
|
43
|
+
* @type {CommandLinePatterns}
|
|
44
|
+
*
|
|
40
45
|
* @example
|
|
41
|
-
*
|
|
42
|
-
* //
|
|
43
|
-
*
|
|
46
|
+
* ```js
|
|
47
|
+
* // Valid short flags:
|
|
48
|
+
* COMMAND_LINE_PATTERNS.UNIX_SHORT.test('-h') // true
|
|
49
|
+
* COMMAND_LINE_PATTERNS.UNIX_SHORT.test('-v') // true
|
|
50
|
+
* COMMAND_LINE_PATTERNS.UNIX_SHORT.test('-f=json') // true
|
|
51
|
+
*
|
|
52
|
+
* // Invalid short flags:
|
|
53
|
+
* COMMAND_LINE_PATTERNS.UNIX_SHORT.test('--help') // false
|
|
54
|
+
* COMMAND_LINE_PATTERNS.UNIX_SHORT.test('-abc') // false
|
|
55
|
+
* ```
|
|
44
56
|
*
|
|
45
57
|
* @example
|
|
46
|
-
*
|
|
47
|
-
* //
|
|
48
|
-
*
|
|
58
|
+
* ```js
|
|
59
|
+
* // Valid long flags:
|
|
60
|
+
* COMMAND_LINE_PATTERNS.UNIX_LONG.test('--help') // true
|
|
61
|
+
* COMMAND_LINE_PATTERNS.UNIX_LONG.test('--output=json') // true
|
|
62
|
+
* COMMAND_LINE_PATTERNS.UNIX_LONG.test('--dry-run') // true
|
|
63
|
+
*
|
|
64
|
+
* // Invalid long flags:
|
|
65
|
+
* COMMAND_LINE_PATTERNS.UNIX_LONG.test('-h') // false
|
|
66
|
+
* COMMAND_LINE_PATTERNS.UNIX_LONG.test('--123') // false
|
|
67
|
+
* ```
|
|
68
|
+
*
|
|
69
|
+
* @example
|
|
70
|
+
* ```js
|
|
71
|
+
* // Valid simple arguments:
|
|
72
|
+
* COMMAND_LINE_PATTERNS.SIMPLE.test('output=json') // true
|
|
73
|
+
* COMMAND_LINE_PATTERNS.SIMPLE.test('config-file=settings') // true
|
|
74
|
+
*
|
|
75
|
+
* // Invalid simple arguments:
|
|
76
|
+
* COMMAND_LINE_PATTERNS.SIMPLE.test('--output=json') // false
|
|
77
|
+
* COMMAND_LINE_PATTERNS.SIMPLE.test('output') // false
|
|
78
|
+
* ```
|
|
49
79
|
*/
|
|
50
|
-
const
|
|
51
|
-
|
|
80
|
+
const COMMAND_LINE_PATTERNS = {
|
|
81
|
+
UNIX_SHORT: /^-[a-zA-Z](=.*)?$/,
|
|
82
|
+
UNIX_LONG: /^--[a-zA-Z][a-zA-Z0-9-]*(=.*)?$/,
|
|
52
83
|
SIMPLE: /^[a-zA-Z]+(?:-[a-zA-Z]+)*=.*$/,
|
|
53
84
|
};
|
|
54
85
|
|
|
@@ -60,11 +91,13 @@ const REGEXPS = {
|
|
|
60
91
|
* @constant THROWS_ERRORS_MESSAGES
|
|
61
92
|
* @type {ErrorMessages}
|
|
62
93
|
* @example
|
|
94
|
+
* ```js
|
|
63
95
|
* // Error cases:
|
|
64
|
-
* throw new Error(
|
|
65
|
-
* throw new Error(
|
|
96
|
+
* throw new Error(ERROR_MESSAGES.INVALID_UNIXMODE_TYPE);
|
|
97
|
+
* throw new Error(ERROR_MESSAGES.NO_ARGUMENTS);
|
|
98
|
+
* ```
|
|
66
99
|
*/
|
|
67
|
-
const
|
|
100
|
+
const ERROR_MESSAGES = {
|
|
68
101
|
INVALID_UNIXMODE_TYPE: 'The "unixmode" parameter must be a boolean value',
|
|
69
102
|
NO_ARGUMENTS: 'No command-line arguments were provided',
|
|
70
103
|
NO_MATCH_SIMPLE: 'Arguments must follow "key=value" format',
|
|
@@ -76,7 +109,7 @@ const THROWS_ERRORS_MESSAGES = {
|
|
|
76
109
|
* Exported constants for argument validation.
|
|
77
110
|
*
|
|
78
111
|
* @type {object}
|
|
79
|
-
* @property {
|
|
80
|
-
* @property {ErrorMessages}
|
|
112
|
+
* @property {CommandLinePatterns} REGEXPS - Validation patterns.
|
|
113
|
+
* @property {ErrorMessages} THROWS_ERRORS_MESSAGES - Standard error messages.
|
|
81
114
|
*/
|
|
82
|
-
export {
|
|
115
|
+
export { COMMAND_LINE_PATTERNS, ERROR_MESSAGES };
|
package/src/functions.mjs
CHANGED
|
@@ -1,7 +1,12 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* @file
|
|
2
|
+
* @file Module containing utility functions for argument processing.
|
|
3
|
+
*
|
|
4
|
+
* Provides formatting and type conversion functions used internally
|
|
5
|
+
* by the argv2object library to normalize and transform command-line
|
|
6
|
+
* arguments into consistent JavaScript data structures.
|
|
3
7
|
*
|
|
4
8
|
* @module Functions
|
|
9
|
+
* @since 1.0.0
|
|
5
10
|
*/
|
|
6
11
|
|
|
7
12
|
// ━━ MODULE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
|
|
@@ -12,21 +17,23 @@
|
|
|
12
17
|
*
|
|
13
18
|
* Supports:
|
|
14
19
|
*
|
|
15
|
-
* - camelCase conversion (--some-flag → someFlag)
|
|
16
|
-
* - snake_case conversion (--some-flag → some_flag)
|
|
17
|
-
* - No conversion (--some-flag → some-flag)
|
|
20
|
+
* - camelCase conversion (--some-flag → someFlag).
|
|
21
|
+
* - snake_case conversion (--some-flag → some_flag).
|
|
22
|
+
* - No conversion (--some-flag → some-flag).
|
|
18
23
|
*
|
|
19
24
|
* @exports
|
|
20
25
|
* @private
|
|
21
26
|
* @function formatKey
|
|
22
|
-
* @param {string} key - The raw key from command-line arguments
|
|
23
|
-
* @param {'camelcase'|'snakecase'} [mode] - Target formatting mode
|
|
24
|
-
* @returns {string} Normalized and formatted key
|
|
25
|
-
* @throws {TypeError} If key is not a string
|
|
27
|
+
* @param {string} key - The raw key from command-line arguments.
|
|
28
|
+
* @param {'camelcase'|'snakecase'} [mode] - Target formatting mode.
|
|
29
|
+
* @returns {string} Normalized and formatted key.
|
|
30
|
+
* @throws {TypeError} If key is not a string.
|
|
26
31
|
* @example
|
|
32
|
+
* ```js
|
|
27
33
|
* formatKey('--output-format', 'camelcase') // returns 'outputFormat'
|
|
28
34
|
* formatKey('-v', 'snakecase') // returns 'v' (no underscores needed)
|
|
29
35
|
* formatKey('--enable-logging') // returns 'enable-logging'
|
|
36
|
+
* ```
|
|
30
37
|
*/
|
|
31
38
|
const formatKey = (key, mode) => {
|
|
32
39
|
if (typeof key !== 'string') {
|
|
@@ -50,21 +57,23 @@ const formatKey = (key, mode) => {
|
|
|
50
57
|
*
|
|
51
58
|
* Performs automatic conversion of:
|
|
52
59
|
*
|
|
53
|
-
* - 'true'/'false' strings → boolean
|
|
54
|
-
* - Numeric strings → number
|
|
55
|
-
* - undefined values → true (for flag arguments)
|
|
56
|
-
* - All other values remain as strings
|
|
60
|
+
* - 'true'/'false' strings → boolean.
|
|
61
|
+
* - Numeric strings → number.
|
|
62
|
+
* - undefined values → true (for flag arguments).
|
|
63
|
+
* - All other values remain as strings.
|
|
57
64
|
*
|
|
58
65
|
* @exports
|
|
59
66
|
* @private
|
|
60
67
|
* @function convertValue
|
|
61
|
-
* @param {string} [value] - The value to convert
|
|
62
|
-
* @returns {string|boolean|number} Converted value
|
|
68
|
+
* @param {string} [value] - The value to convert.
|
|
69
|
+
* @returns {string|boolean|number} Converted value.
|
|
63
70
|
* @example
|
|
71
|
+
* ```js
|
|
64
72
|
* convertValue('true') // returns true
|
|
65
73
|
* convertValue('42') // returns 42
|
|
66
74
|
* convertValue() // returns true (for flag arguments)
|
|
67
75
|
* convertValue('text') // returns 'text'
|
|
76
|
+
* ```
|
|
68
77
|
*/
|
|
69
78
|
const convertValue = value => {
|
|
70
79
|
if (value === undefined) return true;
|
|
@@ -74,6 +83,36 @@ const convertValue = value => {
|
|
|
74
83
|
return value;
|
|
75
84
|
};
|
|
76
85
|
|
|
86
|
+
/**
|
|
87
|
+
* Creates an object from command line arguments by parsing key-value pairs.
|
|
88
|
+
*
|
|
89
|
+
* @param {string[]} argumentsv - Array of command line arguments to process.
|
|
90
|
+
* @param {'camelcase'|'snakecase'} mode - Target formatting mode.
|
|
91
|
+
* @returns {object} Object with parsed keys and converted values.
|
|
92
|
+
* @throws {Error} When arguments cannot be parsed into valid key-value pairs.
|
|
93
|
+
*
|
|
94
|
+
* @example
|
|
95
|
+
* ```js
|
|
96
|
+
* // Simple mode: key=value pairs
|
|
97
|
+
* createObjectFromArguments(['name=John', 'age=30'], 'snakecase');
|
|
98
|
+
* // Returns: { name: 'John', age: 30 }
|
|
99
|
+
*
|
|
100
|
+
* // Unix mode: flags and options
|
|
101
|
+
* createObjectFromArguments(['--name=John', '-a'], 'snakecase');
|
|
102
|
+
* // Returns: { name: 'John', a: true }
|
|
103
|
+
* ```
|
|
104
|
+
*/
|
|
105
|
+
const createObjectFromArguments = (argumentsv, mode) => {
|
|
106
|
+
const entries = [...argumentsv].map(argumentv => {
|
|
107
|
+
const [k, v] = argumentv.split('=');
|
|
108
|
+
const key = formatKey(k, mode);
|
|
109
|
+
const value = convertValue(v);
|
|
110
|
+
return [key, value];
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
return Object.fromEntries(entries);
|
|
114
|
+
};
|
|
115
|
+
|
|
77
116
|
// ━━ EXPORT MODULE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
|
|
78
117
|
/**
|
|
79
118
|
* Utility functions for argument processing.
|
|
@@ -82,4 +121,4 @@ const convertValue = value => {
|
|
|
82
121
|
* @property {Function} formatKey - Key formatting/normalization function.
|
|
83
122
|
* @property {Function} convertValue - Value type conversion function.
|
|
84
123
|
*/
|
|
85
|
-
export { formatKey, convertValue };
|
|
124
|
+
export { formatKey, convertValue, createObjectFromArguments };
|