| 71 | } |
| 72 | |
| 73 | _parseArgs(argv, configs, options) { |
| 74 | const cliOptions = {}; |
| 75 | |
| 76 | // Check for the test mode first. |
| 77 | const testIndex = argv.indexOf('--test'); |
| 78 | if (testIndex !== -1) { |
| 79 | for (const [key, rawValue] of Object.entries(configs)) { |
| 80 | let value = Array.isArray(rawValue) ? rawValue[0] : rawValue; |
| 81 | // Set numbers to one by default to reduce the runtime. |
| 82 | if (typeof value === 'number') { |
| 83 | if (key === 'dur' || key === 'duration') { |
| 84 | value = 0.05; |
| 85 | } else if (key === 'memory') { |
| 86 | // minimum Argon2 memcost with 1 lane is 8 |
| 87 | value = 8; |
| 88 | } else if (value > 1) { |
| 89 | value = 1; |
| 90 | } |
| 91 | } |
| 92 | cliOptions[key] = [value]; |
| 93 | } |
| 94 | // Override specific test options. |
| 95 | if (options.test) { |
| 96 | for (const [key, value] of Object.entries(options.test)) { |
| 97 | cliOptions[key] = Array.isArray(value) ? value : [value]; |
| 98 | } |
| 99 | } |
| 100 | argv.splice(testIndex, 1); |
| 101 | } else { |
| 102 | // Accept single values instead of arrays. |
| 103 | for (const [key, value] of Object.entries(configs)) { |
| 104 | if (!Array.isArray(value)) |
| 105 | configs[key] = [value]; |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | const extraOptions = {}; |
| 110 | const validArgRE = /^(.+?)=([\s\S]*)$/; |
| 111 | // Parse configuration arguments |
| 112 | for (const arg of argv) { |
| 113 | const match = arg.match(validArgRE); |
| 114 | if (!match) { |
| 115 | console.error(`bad argument: ${arg}`); |
| 116 | process.exit(1); |
| 117 | } |
| 118 | const [, key, value] = match; |
| 119 | if (configs[key] !== undefined) { |
| 120 | cliOptions[key] ||= []; |
| 121 | const configType = typeof configs[key][0]; |
| 122 | const configValue = configType === 'number' ? +value : configType === 'boolean' ? value === 'true' : value; |
| 123 | cliOptions[key].push(configValue); |
| 124 | } else { |
| 125 | extraOptions[key] = value; |
| 126 | } |
| 127 | } |
| 128 | return { cli: { ...configs, ...cliOptions }, extra: extraOptions }; |
| 129 | } |
| 130 | |