argv2object 1.0.1 → 1.1.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/index.cjs +23 -0
- package/package.json +72 -70
- package/src/argv2Object.mjs +94 -134
- package/src/constants.mjs +82 -0
- package/src/functions.mjs +85 -0
- /package/{index.js → index.mjs} +0 -0
package/index.cjs
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @author Victor Giovanni Beltrán Rodríguez
|
|
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.
|
|
10
|
+
*
|
|
11
|
+
* This function takes an array of command-line arguments and converts them to a
|
|
12
|
+
* JavaScript object with keys and values based on the provided argument format.
|
|
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").
|
|
15
|
+
*
|
|
16
|
+
* @version 1.0.0
|
|
17
|
+
* @author Victor Giovanni Beltrán Rodríguez
|
|
18
|
+
* @module argv2Object
|
|
19
|
+
*/
|
|
20
|
+
const argv2Object = require('#argv2Object');
|
|
21
|
+
|
|
22
|
+
// ━━ EXPORT MODULE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
|
|
23
|
+
module.exports = argv2Object.default;
|
package/package.json
CHANGED
|
@@ -1,70 +1,72 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "argv2object",
|
|
3
|
-
"version": "1.0
|
|
4
|
-
"private": false,
|
|
5
|
-
"description": "A utility function for converting command-line arguments to a key-value object.",
|
|
6
|
-
"keywords": [
|
|
7
|
-
"arguments",
|
|
8
|
-
"command-line",
|
|
9
|
-
"conversion",
|
|
10
|
-
"key-value",
|
|
11
|
-
"nodejs",
|
|
12
|
-
"object",
|
|
13
|
-
"parse",
|
|
14
|
-
"process.argv",
|
|
15
|
-
"unix-style"
|
|
16
|
-
],
|
|
17
|
-
"homepage": "https://github.com/vgbr-dev/argv2object#readme",
|
|
18
|
-
"bugs": {
|
|
19
|
-
"url": "https://github.com/vgbr-dev/argv2object/issues"
|
|
20
|
-
},
|
|
21
|
-
"repository": {
|
|
22
|
-
"type": "git",
|
|
23
|
-
"url": "git+https://github.com/vgbr-dev/argv2object.git"
|
|
24
|
-
},
|
|
25
|
-
"author": {
|
|
26
|
-
"name": "Victor Giovanni Beltrán Rodríguez",
|
|
27
|
-
"email": "vgbr.dev@gmail.com",
|
|
28
|
-
"url": "https://vgbr-dev.blogspot.com"
|
|
29
|
-
},
|
|
30
|
-
"contributors": [
|
|
31
|
-
{
|
|
32
|
-
"name": "Victor Giovanni Beltrán Rodriguez",
|
|
33
|
-
"email": "vgbr.dev@gmail.com",
|
|
34
|
-
"url": "https://vgbr-dev.blogspot.com"
|
|
35
|
-
}
|
|
36
|
-
],
|
|
37
|
-
"license": "MIT",
|
|
38
|
-
"type": "module",
|
|
39
|
-
"main": "index.
|
|
40
|
-
"
|
|
41
|
-
"
|
|
42
|
-
"
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
"
|
|
47
|
-
"
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
"
|
|
53
|
-
"
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
"
|
|
57
|
-
"
|
|
58
|
-
"
|
|
59
|
-
"eslint
|
|
60
|
-
"eslint-
|
|
61
|
-
"
|
|
62
|
-
"
|
|
63
|
-
"
|
|
64
|
-
"
|
|
65
|
-
"
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
1
|
+
{
|
|
2
|
+
"name": "argv2object",
|
|
3
|
+
"version": "1.1.0",
|
|
4
|
+
"private": false,
|
|
5
|
+
"description": "A utility function for converting command-line arguments to a key-value object.",
|
|
6
|
+
"keywords": [
|
|
7
|
+
"arguments",
|
|
8
|
+
"command-line",
|
|
9
|
+
"conversion",
|
|
10
|
+
"key-value",
|
|
11
|
+
"nodejs",
|
|
12
|
+
"object",
|
|
13
|
+
"parse",
|
|
14
|
+
"process.argv",
|
|
15
|
+
"unix-style"
|
|
16
|
+
],
|
|
17
|
+
"homepage": "https://github.com/vgbr-dev/argv2object#readme",
|
|
18
|
+
"bugs": {
|
|
19
|
+
"url": "https://github.com/vgbr-dev/argv2object/issues"
|
|
20
|
+
},
|
|
21
|
+
"repository": {
|
|
22
|
+
"type": "git",
|
|
23
|
+
"url": "git+https://github.com/vgbr-dev/argv2object.git"
|
|
24
|
+
},
|
|
25
|
+
"author": {
|
|
26
|
+
"name": "Victor Giovanni Beltrán Rodríguez",
|
|
27
|
+
"email": "vgbr.dev@gmail.com",
|
|
28
|
+
"url": "https://vgbr-dev.blogspot.com"
|
|
29
|
+
},
|
|
30
|
+
"contributors": [
|
|
31
|
+
{
|
|
32
|
+
"name": "Victor Giovanni Beltrán Rodriguez",
|
|
33
|
+
"email": "vgbr.dev@gmail.com",
|
|
34
|
+
"url": "https://vgbr-dev.blogspot.com"
|
|
35
|
+
}
|
|
36
|
+
],
|
|
37
|
+
"license": "MIT",
|
|
38
|
+
"type": "module",
|
|
39
|
+
"main": "index.mjs",
|
|
40
|
+
"exports": {
|
|
41
|
+
"import": "./index.mjs",
|
|
42
|
+
"require": "./index.cjs"
|
|
43
|
+
},
|
|
44
|
+
"imports": {
|
|
45
|
+
"#argv2Object": "./src/argv2Object.mjs",
|
|
46
|
+
"#constants": "./src/constants.mjs",
|
|
47
|
+
"#functions": "./src/functions.mjs"
|
|
48
|
+
},
|
|
49
|
+
"scripts": {
|
|
50
|
+
"test": "cross-env NODE_ENV=test node --test test/*.test.mjs",
|
|
51
|
+
"lint": "npx eslint . --ext .mjs --ext .js --ext .cjs",
|
|
52
|
+
"prepare": "husky install",
|
|
53
|
+
"prepublish": "npm test"
|
|
54
|
+
},
|
|
55
|
+
"devDependencies": {
|
|
56
|
+
"@commitlint/cli": "^19.8.0",
|
|
57
|
+
"@commitlint/config-conventional": "^19.8.0",
|
|
58
|
+
"cross-env": "^7.0.3",
|
|
59
|
+
"eslint": "^8.57.1",
|
|
60
|
+
"eslint-config-airbnb-base": "^15.0.0",
|
|
61
|
+
"eslint-config-prettier": "^8.10.0",
|
|
62
|
+
"eslint-import-resolver-node": "^0.3.9",
|
|
63
|
+
"eslint-plugin-import": "^2.27.5",
|
|
64
|
+
"eslint-plugin-prettier": "^4.2.1",
|
|
65
|
+
"husky": "^8.0.3",
|
|
66
|
+
"lint-staged": "^15.5.2",
|
|
67
|
+
"prettier": "^2.8.8"
|
|
68
|
+
},
|
|
69
|
+
"engines": {
|
|
70
|
+
"node": ">=12.0.0"
|
|
71
|
+
}
|
|
72
|
+
}
|
package/src/argv2Object.mjs
CHANGED
|
@@ -1,134 +1,94 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @file Converts command-line arguments to a key-value object.
|
|
3
|
-
*
|
|
4
|
-
* A utility function for converting command-line arguments to a key-value
|
|
5
|
-
* object.
|
|
6
|
-
*
|
|
7
|
-
* This file defines a single function, `argv2Object`, which parses
|
|
8
|
-
* command-line arguments and returns an object with keys and values
|
|
9
|
-
* corresponding to the provided arguments. The function can handle two types of
|
|
10
|
-
* arguments: simple key-value pairs (e.g. "key=value") and Unix-style
|
|
11
|
-
* command-line arguments (e.g. "-k value" or "--key=value").
|
|
12
|
-
*
|
|
13
|
-
* @author Victor Giovanni Beltrán Rodríguez
|
|
14
|
-
* @version 1.0.0
|
|
15
|
-
*
|
|
16
|
-
* @example
|
|
17
|
-
* Simple key-value pairs
|
|
18
|
-
* Command line input: node script.js name=John age=30 level=admin
|
|
19
|
-
* Output: { name: 'John', age: '30', level: "admin" }
|
|
20
|
-
*
|
|
21
|
-
* @example
|
|
22
|
-
* Unix-style command-line options
|
|
23
|
-
* Command line input: node script.js -h --name=John --is-admin
|
|
24
|
-
* Output: { h: true, name: 'John', is_admin: true }
|
|
25
|
-
*/
|
|
26
|
-
|
|
27
|
-
//
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
*
|
|
34
|
-
*
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
*
|
|
42
|
-
*
|
|
43
|
-
*
|
|
44
|
-
*
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
*
|
|
49
|
-
*
|
|
50
|
-
* @
|
|
51
|
-
* @
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
*
|
|
57
|
-
*
|
|
58
|
-
*
|
|
59
|
-
*
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
* @example
|
|
96
|
-
* ```js
|
|
97
|
-
* // Command line input: node script.js --task=some-task --execute=true
|
|
98
|
-
*
|
|
99
|
-
* const args = argv2Object();
|
|
100
|
-
* console.log(args.task); // prints 'some-task'
|
|
101
|
-
* console.log(args.execute); // prints 'true'
|
|
102
|
-
* ```
|
|
103
|
-
*/
|
|
104
|
-
const argv2Object = (unixmode = false) => {
|
|
105
|
-
if (typeof unixmode !== 'boolean') {
|
|
106
|
-
throw new TypeError(THROWS_ERRORS_MESSAGES.INVALID_UNIXMODE_TYPE);
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
const argumentsv = process.argv.slice(2);
|
|
110
|
-
if (argumentsv.length === 0) {
|
|
111
|
-
throw new Error(THROWS_ERRORS_MESSAGES.NO_ARGUMENTS);
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
const regexp = unixmode ? REGEXPS.UNIXMODE : REGEXPS.SIMPLE;
|
|
115
|
-
|
|
116
|
-
if (!argumentsv.every(argumentv => regexp.test(argumentv))) {
|
|
117
|
-
const message = unixmode
|
|
118
|
-
? THROWS_ERRORS_MESSAGES.NO_MATCH_UNIXMODE
|
|
119
|
-
: THROWS_ERRORS_MESSAGES.NO_MATCH_SIMPLE;
|
|
120
|
-
throw new Error(message);
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
const entries = [...argumentsv].map(argumentv => {
|
|
124
|
-
const [k, v] = argumentv.split('=');
|
|
125
|
-
const key = k.replace(/^-{1,2}/, '').replace(/-/g, '_');
|
|
126
|
-
const value = v === undefined ? true : v;
|
|
127
|
-
return [key, value];
|
|
128
|
-
});
|
|
129
|
-
|
|
130
|
-
return Object.fromEntries(entries);
|
|
131
|
-
};
|
|
132
|
-
|
|
133
|
-
// ━━ EXPORT MODULE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
|
|
134
|
-
export default argv2Object;
|
|
1
|
+
/**
|
|
2
|
+
* @file Converts command-line arguments to a key-value object.
|
|
3
|
+
*
|
|
4
|
+
* A utility function for converting command-line arguments to a key-value
|
|
5
|
+
* object.
|
|
6
|
+
*
|
|
7
|
+
* This file defines a single function, `argv2Object`, which parses
|
|
8
|
+
* command-line arguments and returns an object with keys and values
|
|
9
|
+
* corresponding to the provided arguments. The function can handle two types of
|
|
10
|
+
* arguments: simple key-value pairs (e.g. "key=value") and Unix-style
|
|
11
|
+
* command-line arguments (e.g. "-k value" or "--key=value").
|
|
12
|
+
*
|
|
13
|
+
* @author Victor Giovanni Beltrán Rodríguez
|
|
14
|
+
* @version 1.0.0
|
|
15
|
+
*
|
|
16
|
+
* @example
|
|
17
|
+
* Simple key-value pairs
|
|
18
|
+
* Command line input: node script.js name=John age=30 level=admin
|
|
19
|
+
* Output: { name: 'John', age: '30', level: "admin" }
|
|
20
|
+
*
|
|
21
|
+
* @example
|
|
22
|
+
* Unix-style command-line options
|
|
23
|
+
* Command line input: node script.js -h --name=John --is-admin
|
|
24
|
+
* Output: { h: true, name: 'John', is_admin: true }
|
|
25
|
+
*/
|
|
26
|
+
// ━━ IMPORT MODULES ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
|
|
27
|
+
// » IMPORT LOCAL MODULES
|
|
28
|
+
import { REGEXPS, THROWS_ERRORS_MESSAGES } from '#constants';
|
|
29
|
+
import { formatKey, convertValue } from '#functions';
|
|
30
|
+
|
|
31
|
+
// ━━ TYPE DEFINITIONS ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
|
|
32
|
+
/**
|
|
33
|
+
* An `Object` with keys and values corresponding to the provided arguments.
|
|
34
|
+
*
|
|
35
|
+
* @typedef {object} ArgvObject
|
|
36
|
+
* @property {string|boolean} [key=value] - The key-value pairs in the provided arguments.
|
|
37
|
+
*/
|
|
38
|
+
|
|
39
|
+
// ━━ MODULE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
|
|
40
|
+
/**
|
|
41
|
+
* The `argv2Object()` function, converts command-line arguments to a key-value
|
|
42
|
+
* object.
|
|
43
|
+
*
|
|
44
|
+
* This function takes an array of command-line arguments and converts them to a
|
|
45
|
+
* JavaScript object with keys and values based on the provided argument format.
|
|
46
|
+
* The format can be either simple key-value pairs (e.g. "key=value") or
|
|
47
|
+
* Unix-style command-line options (e.g. "-o --option=value").
|
|
48
|
+
*
|
|
49
|
+
* @function
|
|
50
|
+
* @param {boolean} [unixmode=false] - Whether to parse Unix-style command-line options, the default value is `false`.
|
|
51
|
+
* @returns {ArgvObject} Returns an object with keys and values corresponding to the provided arguments.
|
|
52
|
+
* @throws {Error} If no arguments are provided from command line.
|
|
53
|
+
* @throws {Error} If `unixmode` is `true` and argument does not follow Unix-style command-line format.
|
|
54
|
+
* @throws {Error} If `unixmode` is `false` and argument does not follow 'key=value' format.
|
|
55
|
+
* @example
|
|
56
|
+
* ```js
|
|
57
|
+
* // Command line input: node script.js --task=some-task --execute=true
|
|
58
|
+
*
|
|
59
|
+
* const args = argv2Object();
|
|
60
|
+
* console.log(args.task); // prints 'some-task'
|
|
61
|
+
* console.log(args.execute); // prints 'true'
|
|
62
|
+
* ```
|
|
63
|
+
*/
|
|
64
|
+
const argv2Object = (unixmode = false) => {
|
|
65
|
+
if (typeof unixmode !== 'boolean') {
|
|
66
|
+
throw new TypeError(THROWS_ERRORS_MESSAGES.INVALID_UNIXMODE_TYPE);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
const argumentsv = process.argv.slice(2);
|
|
70
|
+
if (argumentsv.length === 0) {
|
|
71
|
+
throw new Error(THROWS_ERRORS_MESSAGES.NO_ARGUMENTS);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
const regexp = unixmode ? REGEXPS.UNIXMODE : REGEXPS.SIMPLE;
|
|
75
|
+
|
|
76
|
+
if (!argumentsv.every(argumentv => regexp.test(argumentv))) {
|
|
77
|
+
const message = unixmode
|
|
78
|
+
? THROWS_ERRORS_MESSAGES.NO_MATCH_UNIXMODE
|
|
79
|
+
: THROWS_ERRORS_MESSAGES.NO_MATCH_SIMPLE;
|
|
80
|
+
throw new Error(message);
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
const entries = [...argumentsv].map(argumentv => {
|
|
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);
|
|
91
|
+
};
|
|
92
|
+
|
|
93
|
+
// ━━ EXPORT MODULE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
|
|
94
|
+
export default argv2Object;
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @file Module containing constants for argument validation.
|
|
3
|
+
*
|
|
4
|
+
* This module exports regular expressions for argument pattern matching
|
|
5
|
+
* and standardized error messages used throughout the argv2object library.
|
|
6
|
+
* These constants ensure consistent validation and error reporting when
|
|
7
|
+
* parsing command-line arguments.
|
|
8
|
+
*
|
|
9
|
+
* @module constants
|
|
10
|
+
* @since 1.0.0
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
// ━━ TYPE DEFINITIONS ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
|
|
14
|
+
/**
|
|
15
|
+
* Regular expression patterns for argument validation.
|
|
16
|
+
*
|
|
17
|
+
* @typedef {object} Regexps
|
|
18
|
+
* @property {RegExp} UNIXMODE - Pattern for Unix-style arguments (-a, --arg=value).
|
|
19
|
+
* @property {RegExp} SIMPLE - Pattern for simple key=value arguments.
|
|
20
|
+
*/
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Standardized error messages for argument validation failures.
|
|
24
|
+
*
|
|
25
|
+
* @typedef {object} ErrorMessages
|
|
26
|
+
* @property {string} INVALID_UNIXMODE_TYPE - When unixmode parameter is not boolean.
|
|
27
|
+
* @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
|
|
30
|
+
*/
|
|
31
|
+
|
|
32
|
+
// ━━ MODULE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
|
|
33
|
+
/**
|
|
34
|
+
* Regular expressions for validating different argument formats.
|
|
35
|
+
*
|
|
36
|
+
* @exports
|
|
37
|
+
* @private
|
|
38
|
+
* @constant REGEXPS
|
|
39
|
+
* @type {Regexps}
|
|
40
|
+
* @example
|
|
41
|
+
* // Unix-style pattern matches:
|
|
42
|
+
* // -a, -b=value, --flag, --option=value
|
|
43
|
+
* REGEXPS.UNIXMODE.test('--output=json') // true
|
|
44
|
+
*
|
|
45
|
+
* @example
|
|
46
|
+
* // Simple pattern matches:
|
|
47
|
+
* // key=value, multi-part-key=value
|
|
48
|
+
* REGEXPS.SIMPLE.test('output_format=json') // true
|
|
49
|
+
*/
|
|
50
|
+
const REGEXPS = {
|
|
51
|
+
UNIXMODE: /^-[a-z]{1}|-[a-z]{1}=.*$|--[a-zA-Z]+|--[a-zA-Z]+(?:-[a-zA-Z]+)*=.*$/,
|
|
52
|
+
SIMPLE: /^[a-zA-Z]+(?:-[a-zA-Z]+)*=.*$/,
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Standardized error messages for consistent validation feedback.
|
|
57
|
+
*
|
|
58
|
+
* @exports
|
|
59
|
+
* @private
|
|
60
|
+
* @constant THROWS_ERRORS_MESSAGES
|
|
61
|
+
* @type {ErrorMessages}
|
|
62
|
+
* @example
|
|
63
|
+
* // Error cases:
|
|
64
|
+
* throw new Error(THROWS_ERRORS_MESSAGES.INVALID_UNIXMODE_TYPE);
|
|
65
|
+
* throw new Error(THROWS_ERRORS_MESSAGES.NO_ARGUMENTS);
|
|
66
|
+
*/
|
|
67
|
+
const THROWS_ERRORS_MESSAGES = {
|
|
68
|
+
INVALID_UNIXMODE_TYPE: 'The "unixmode" parameter must be a boolean value',
|
|
69
|
+
NO_ARGUMENTS: 'No command-line arguments were provided',
|
|
70
|
+
NO_MATCH_SIMPLE: 'Arguments must follow "key=value" format',
|
|
71
|
+
NO_MATCH_UNIXMODE: 'Arguments must follow Unix-style format (-a, --option=value)',
|
|
72
|
+
};
|
|
73
|
+
|
|
74
|
+
// ━━ EXPORT MODULE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
|
|
75
|
+
/**
|
|
76
|
+
* Exported constants for argument validation.
|
|
77
|
+
*
|
|
78
|
+
* @type {object}
|
|
79
|
+
* @property {Regexps} REGEXPS - Validation patterns.
|
|
80
|
+
* @property {ErrorMessages} THROWS_ERRORS_MESSAGES - Standard error messages.
|
|
81
|
+
*/
|
|
82
|
+
export { REGEXPS, THROWS_ERRORS_MESSAGES };
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @file TODO ADD SHORT MODULE DESCRIPTION.
|
|
3
|
+
*
|
|
4
|
+
* @module Functions
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
// ━━ MODULE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
|
|
8
|
+
/**
|
|
9
|
+
* Normalizes and formats argument keys according to specified casing convention.
|
|
10
|
+
*
|
|
11
|
+
* Removes leading dashes from command-line flags and applies optional case conversion.
|
|
12
|
+
*
|
|
13
|
+
* Supports:
|
|
14
|
+
*
|
|
15
|
+
* - camelCase conversion (--some-flag → someFlag)
|
|
16
|
+
* - snake_case conversion (--some-flag → some_flag)
|
|
17
|
+
* - No conversion (--some-flag → some-flag)
|
|
18
|
+
*
|
|
19
|
+
* @exports
|
|
20
|
+
* @private
|
|
21
|
+
* @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
|
|
26
|
+
* @example
|
|
27
|
+
* formatKey('--output-format', 'camelcase') // returns 'outputFormat'
|
|
28
|
+
* formatKey('-v', 'snakecase') // returns 'v' (no underscores needed)
|
|
29
|
+
* formatKey('--enable-logging') // returns 'enable-logging'
|
|
30
|
+
*/
|
|
31
|
+
const formatKey = (key, mode) => {
|
|
32
|
+
if (typeof key !== 'string') {
|
|
33
|
+
throw new TypeError('Key must be a string');
|
|
34
|
+
}
|
|
35
|
+
const sanitizedKey = key.replace(/^-{1,2}/, '');
|
|
36
|
+
|
|
37
|
+
if (mode === 'camelcase') {
|
|
38
|
+
return sanitizedKey.replace(/-([a-z])/g, (_, char) => char.toUpperCase());
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
if (mode === 'snakecase') {
|
|
42
|
+
return sanitizedKey.replace(/-/g, '_');
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
return sanitizedKey;
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Converts string values from command-line arguments to proper JavaScript types.
|
|
50
|
+
*
|
|
51
|
+
* Performs automatic conversion of:
|
|
52
|
+
*
|
|
53
|
+
* - 'true'/'false' strings → boolean
|
|
54
|
+
* - Numeric strings → number
|
|
55
|
+
* - undefined values → true (for flag arguments)
|
|
56
|
+
* - All other values remain as strings
|
|
57
|
+
*
|
|
58
|
+
* @exports
|
|
59
|
+
* @private
|
|
60
|
+
* @function convertValue
|
|
61
|
+
* @param {string} [value] - The value to convert
|
|
62
|
+
* @returns {string|boolean|number} Converted value
|
|
63
|
+
* @example
|
|
64
|
+
* convertValue('true') // returns true
|
|
65
|
+
* convertValue('42') // returns 42
|
|
66
|
+
* convertValue() // returns true (for flag arguments)
|
|
67
|
+
* convertValue('text') // returns 'text'
|
|
68
|
+
*/
|
|
69
|
+
const convertValue = value => {
|
|
70
|
+
if (value === undefined) return true;
|
|
71
|
+
if (value === 'true') return true;
|
|
72
|
+
if (value === 'false') return false;
|
|
73
|
+
if (!Number.isNaN(value) && !Number.isNaN(parseFloat(value))) return Number(value);
|
|
74
|
+
return value;
|
|
75
|
+
};
|
|
76
|
+
|
|
77
|
+
// ━━ EXPORT MODULE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
|
|
78
|
+
/**
|
|
79
|
+
* Utility functions for argument processing.
|
|
80
|
+
*
|
|
81
|
+
* @namespace
|
|
82
|
+
* @property {Function} formatKey - Key formatting/normalization function.
|
|
83
|
+
* @property {Function} convertValue - Value type conversion function.
|
|
84
|
+
*/
|
|
85
|
+
export { formatKey, convertValue };
|
/package/{index.js → index.mjs}
RENAMED
|
File without changes
|