|
| 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; |
0 commit comments