Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 19 additions & 7 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}`);
}
}
}

Expand Down Expand Up @@ -130,7 +142,7 @@ function unparseOptions(argv, options, knownPositional, unparsed) {
continue;
}

unparseOption(key, argv[key], unparsed);
unparseOption(key, argv[key], unparsed, options);
}
}

Expand Down
23 changes: 23 additions & 0 deletions test/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'],
});
});
});