diff --git a/index.js b/index.js index c2b7581..c5b53f5 100644 --- a/index.js +++ b/index.js @@ -57,26 +57,38 @@ function parseCommand(cmd) { return parsedCommand; } -function unparseOption(key, value, unparsed) { +function unparseOption(key, value, unparsed, options) { + const delimiter = options.delimiter; + const useEqualSign = delimiter === '='; + const flaggedKey = keyToFlag(key); + if (typeof value === 'string') { - unparsed.push(keyToFlag(key), value); + if (useEqualSign) { + unparsed.push(`${flaggedKey}=${value}`); + } else { + unparsed.push(flaggedKey, value); + } } else if (value === true) { - unparsed.push(keyToFlag(key)); + unparsed.push(flaggedKey); } else if (value === false) { unparsed.push(`--no-${key}`); } else if (Array.isArray(value)) { - value.forEach((item) => unparseOption(key, item, unparsed)); + value.forEach((item) => unparseOption(key, item, unparsed, options)); } else if (isPlainObj(value)) { const flattened = flatten(value, { safe: true }); for (const flattenedKey in flattened) { if (!isCamelCased(flattenedKey, flattened)) { - unparseOption(`${key}.${flattenedKey}`, flattened[flattenedKey], unparsed); + unparseOption(`${key}.${flattenedKey}`, flattened[flattenedKey], unparsed, options); } } // Fallback case (numbers and other types) } else if (value != null) { - unparsed.push(keyToFlag(key), `${value}`); + if (useEqualSign) { + unparsed.push(`${flaggedKey}=${value}`); + } else { + unparsed.push(flaggedKey, `${value}`); + } } } @@ -130,7 +142,7 @@ function unparseOptions(argv, options, knownPositional, unparsed) { continue; } - unparseOption(key, argv[key], unparsed); + unparseOption(key, argv[key], unparsed, options); } } diff --git a/test/index.spec.js b/test/index.spec.js index a050d8e..ea0f38e 100644 --- a/test/index.spec.js +++ b/test/index.spec.js @@ -219,3 +219,26 @@ describe('interoperation with other libraries', () => { }); }); }); + +describe('use equal mark as delimiter', () => { + it('should success', () => { + const argv = parse(['--no-cache', '--optimize', '--host', '0.0.0.0', '--collect', 'x', 'y', '--env', 'node', '--env', 'python'], { + boolean: ['cache', 'optimize'], + string: 'host', + array: ['collect', 'env'], + }); + const argvArray = unparse(argv, { + delimiter: '=', + }); + + expect(argvArray).toEqual(['--no-cache', '--optimize', '--host=0.0.0.0', '--collect=x', '--collect=y', '--env=node', '--env=python']); + + expect(minimist(argvArray)).toMatchObject({ + cache: false, + optimize: true, + host: '0.0.0.0', + collect: ['x', 'y'], + env: ['node', 'python'], + }); + }); +});