argv2object 1.0.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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2019 Beltran Rodriguez Victor
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,128 @@
1
+ # argv2Object
2
+
3
+ A utility function for converting command-line arguments to a key-value object.
4
+
5
+ ## Table of Contents
6
+
7
+ - [Installation](#installation)
8
+ - [Importing](#importing)
9
+ - [Usage](#usage)
10
+ - [Examples](#examples)
11
+ - [Simple key-value pairs](#simple-key-value-pairs)
12
+ - [Unix-style command-line options](#unix-style-command-line-options)
13
+ - [API](#api)
14
+ - [Type Definitions](#type-definitions)
15
+ - [`argv2Object()`](#argv2object-1)
16
+ - [Contributing](#contributing)
17
+ - [License](#license)
18
+
19
+ ## Installation
20
+
21
+ You can install argv2Object via [npm](https://www.npmjs.com/package/argv2Object):
22
+
23
+ ```sh
24
+ npm install argv2object
25
+ ```
26
+
27
+ ## Importing
28
+
29
+ To use argv2Object in your JavaScript application, first import it:
30
+
31
+ ```js
32
+ // Using ES6 imports
33
+ import argv2Object from 'argv2object';
34
+
35
+ // Using Node.js `require()`
36
+ const argv2Object = require('argv2object');
37
+ ```
38
+
39
+ ## Usage
40
+
41
+ Converts command-line arguments to a key-value object.
42
+
43
+ ```sh
44
+ # Command line input:
45
+ node script.js --task=some-task --execute -h --name=John
46
+
47
+ ```
48
+
49
+ ```js
50
+ const argv2Object = require('argv2object');
51
+
52
+ // UNIX command line mode true
53
+ const args = argv2Object(true);
54
+ console.log(args.task); // "some-task"
55
+
56
+ console.log(args.execute); // true
57
+
58
+ console.log(args.h); // true
59
+
60
+ console.log(args.name); // "John"
61
+
62
+ ```
63
+
64
+ ## Examples
65
+
66
+ ### Simple key-value pairs
67
+
68
+ ```sh
69
+ # Command line input:
70
+ node script.js task=some-task name=John level=admin
71
+
72
+ ```
73
+
74
+ ```js
75
+ const args = argv2Object();
76
+ console.log(args);
77
+ // Output: { name: 'John', age: '30', level: 'admin' }
78
+ ```
79
+
80
+ ### Unix-style command-line options
81
+
82
+ ```sh
83
+ # Command line input:
84
+ node --watch script.js -h --name=John --is-admin
85
+
86
+ ```
87
+
88
+ ```js
89
+ const args = argv2Object(true);
90
+ console.log(args);
91
+ // Output: { h: true, name: 'John', is_admin: true }
92
+ ```
93
+
94
+ ## API
95
+
96
+ ### Type Definitions
97
+
98
+ - `ArgvObject`: An `Object` with keys and values corresponding to the provided arguments.
99
+
100
+ ### `argv2Object()`
101
+
102
+ The `argv2Object(unixmode = false): => ArgvObject` function takes the command-line arguments and converts them to a JavaScript object with keys and values based on the provided argument format. The format can be either simple key-value pairs (e.g. "key=value") or Unix-style command-line options (e.g. "-o --option=value").
103
+
104
+ Arguments
105
+
106
+ | Name | Type | Default | Description |
107
+ |------------|-----------|---------|---------------------------------------------------|
108
+ | `unixmode` | `boolean` | `false` | Whether to parse Unix-style command-line options. |
109
+
110
+ Returns
111
+
112
+ Returns an `Object` with keys and values corresponding to the provided arguments.
113
+
114
+ Throws
115
+
116
+ | Type | Description |
117
+ |---------|---------------------------------------------------------------------------------------------|
118
+ | `Error` | If no arguments are provided from command line. |
119
+ | `Error` | If `unixmode` is `true` and an argument does not follow the Unix-style command-line format. |
120
+ | `Error` | If `unixmode` is `false` and an argument does not follow the "key=value" format. |
121
+
122
+ ## Contributing
123
+
124
+ Contributions, issues and feature requests are welcome. Feel free to check [issues page](https://github.com/vgbr-dev/argv2object/issues) if you want to contribute.
125
+
126
+ ## License
127
+
128
+ Distributed under the MIT License. See LICENSE for more information.
package/index.js 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('./src');
21
+
22
+ // ━━ EXPORT MODULE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
23
+ module.exports = argv2Object;
package/package.json ADDED
@@ -0,0 +1,67 @@
1
+ {
2
+ "name": "argv2object",
3
+ "version": "1.0.0",
4
+ "private": false,
5
+ "description": "A utility function for converting command-line arguments to a key-value object.",
6
+ "keywords": [
7
+ "command-line",
8
+ "arguments",
9
+ "object",
10
+ "conversion",
11
+ "key-value",
12
+ "unix-style",
13
+ "parse"
14
+ ],
15
+ "directories": {
16
+ "doc": "docs",
17
+ "test": "test"
18
+ },
19
+ "homepage": "https://github.com/vgbr-dev/argv2object#readme",
20
+ "bugs": {
21
+ "url": "https://github.com/vgbr-dev/argv2object/issues"
22
+ },
23
+ "repository": {
24
+ "type": "git",
25
+ "url": "git+https://github.com/vgbr-dev/argv2object.git"
26
+ },
27
+ "author": {
28
+ "name": "Victor Giovanni Beltrán Rodríguez",
29
+ "email": "vgbr.dev@gmail.com",
30
+ "url": "https://vgbr-dev.blogspot.com"
31
+ },
32
+ "contributors": [
33
+ {
34
+ "name": "Victor Giovanni Beltrán Rodriguez",
35
+ "email": "vgbr.dev@gmail.com",
36
+ "url": "https://vgbr-dev.blogspot.com"
37
+ }
38
+ ],
39
+ "license": "MIT",
40
+ "main": "index.js",
41
+ "scripts": {
42
+ "test": "cross-env NODE_ENV=test node --test test/",
43
+ "develop": "node --watch develop.js",
44
+ "lint": "npx eslint",
45
+ "prepare": "husky install",
46
+ "prepublish": "npm test"
47
+ },
48
+ "devDependencies": {
49
+ "@commitlint/cli": "^17.6.1",
50
+ "@commitlint/config-conventional": "^17.6.1",
51
+ "cross-env": "^7.0.3",
52
+ "eslint": "^7.32.0",
53
+ "eslint-config-airbnb-base": "^15.0.0",
54
+ "eslint-config-prettier": "^8.8.0",
55
+ "eslint-plugin-import": "^2.27.5",
56
+ "eslint-plugin-jsdoc": "^41.1.1",
57
+ "eslint-plugin-prettier": "^4.2.1",
58
+ "husky": "^8.0.3",
59
+ "jsdoc": "^4.0.2",
60
+ "jsdoc-to-markdown": "^8.0.0",
61
+ "lint-staged": "^13.2.1",
62
+ "prettier": "^2.8.7"
63
+ },
64
+ "engines": {
65
+ "node": ">=12.0.0"
66
+ }
67
+ }
package/src/index.js ADDED
@@ -0,0 +1,130 @@
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
+ // ━━ TYPE DEFINITIONS ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
28
+ /**
29
+ * Regular expressions used to validate command-line arguments.
30
+ *
31
+ * @private
32
+ * @typedef {object} Regexps
33
+ * @property {RegExp} UNIXMODE - Regular expression for validating Unix-style command-line arguments.
34
+ * @property {RegExp} SIMPLE - Regular expression for validating simple key-value arguments.
35
+ */
36
+
37
+ /**
38
+ * Error messages used in case of invalid command-line arguments.
39
+ *
40
+ * @private
41
+ * @typedef {object} Errors
42
+ * @property {string} NO_ARGS - Error message for when no arguments are provided.
43
+ * @property {string} NO_MATCH_SIMPLE - Error message for when one or more simple key-value arguments do not follow the expected format.
44
+ * @property {string} NO_MATCH_UNIXMODE - Error message for when one or more Unix-style command-line arguments do not follow the expected format.
45
+ */
46
+
47
+ /**
48
+ * An `Object` with keys and values corresponding to the provided arguments.
49
+ *
50
+ * @typedef {object} ArgvObject
51
+ * @property {string|boolean} [key=value] - The key-value pairs in the provided arguments.
52
+ */
53
+
54
+ // ━━ CONSTANTS ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
55
+ /**
56
+ * Regular expressions used to validate command-line arguments.
57
+ *
58
+ * @private
59
+ * @type {Regexps}
60
+ */
61
+ const REGEXPS = {
62
+ UNIXMODE: /^-[a-z]{1}|-[a-z]{1}=.*$|--[a-zA-Z]+|--[a-zA-Z]+(?:-[a-zA-Z]+)*=.*$/,
63
+ SIMPLE: /^[a-zA-Z]+(?:-[a-zA-Z]+)*=.*$/,
64
+ };
65
+
66
+ /**
67
+ * Error messages used in case of invalid command-line arguments.
68
+ *
69
+ * @private
70
+ * @type {Errors}
71
+ */
72
+ const ERRORS = {
73
+ NO_ARGS: 'No arguments added',
74
+ NO_MATCH_SIMPLE: `Some argument(s) do not follow the 'key=value' format.`,
75
+ NO_MATCH_UNIXMODE: `Some argument(s) do not follow the Unix-style command-line format.`,
76
+ };
77
+
78
+ // ━━ MODULE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
79
+ /**
80
+ * The `argv2Object()` function, converts command-line arguments to a key-value
81
+ * object.
82
+ *
83
+ * This function takes an array of command-line arguments and converts them to a
84
+ * JavaScript object with keys and values based on the provided argument format.
85
+ * The format can be either simple key-value pairs (e.g. "key=value") or
86
+ * Unix-style command-line options (e.g. "-o --option=value").
87
+ *
88
+ * @function
89
+ * @param {boolean} [unixmode=false] - Whether to parse Unix-style command-line options, the default value is `false`.
90
+ * @returns {ArgvObject} Returns an object with keys and values corresponding to the provided arguments.
91
+ * @throws {Error} If no arguments are provided from command line.
92
+ * @throws {Error} If `unixmode` is `true` and argument does not follow Unix-style command-line format.
93
+ * @throws {Error} If `unixmode` is `false` and argument does not follow 'key=value' format.
94
+ * @example
95
+ * ```js
96
+ * // Command line input: node script.js --task=some-task --execute=true
97
+ *
98
+ * const args = argv2Object();
99
+ * console.log(args.task); // prints 'some-task'
100
+ * console.log(args.execute); // prints 'true'
101
+ * ```
102
+ */
103
+ const argv2Object = (unixmode = false) => {
104
+ const regexp = unixmode ? REGEXPS.UNIXMODE : REGEXPS.SIMPLE;
105
+
106
+ const args = process.argv.slice(2);
107
+ if (args.length === 0) {
108
+ throw new Error(ERRORS.NO_ARGS);
109
+ }
110
+
111
+ if (!args.every(arg => regexp.test(arg))) {
112
+ throw new Error(unixmode ? ERRORS.NO_MATCH_UNIXMODE : ERRORS.NO_MATCH_SIMPLE);
113
+ }
114
+
115
+ return [...args]
116
+ .map(arg => {
117
+ const [k, v] = arg.split('=');
118
+ const key = k.replace(/^-{1,2}/, '').replace(/-/g, '_');
119
+ const value = v === undefined ? true : v;
120
+ return [key, value];
121
+ })
122
+ .reduce((acc, arg) => {
123
+ const [key, value] = arg;
124
+ acc[key] = value;
125
+ return acc;
126
+ }, {});
127
+ };
128
+
129
+ // ━━ EXPORT MODULE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
130
+ module.exports = argv2Object;