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
24 changes: 15 additions & 9 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,15 +104,21 @@ function arg(
}

if (wholeArg.length > 1 && wholeArg[0] === '-') {
/* eslint-disable operator-linebreak */
const separatedArguments =
wholeArg[1] === '-' || wholeArg.length === 2
? [wholeArg]
: wholeArg
.slice(1)
.split('')
.map((a) => `-${a}`);
/* eslint-enable operator-linebreak */
let separatedArguments = [];
if (wholeArg[1] === '-' || wholeArg.length === 2) {
separatedArguments.push(wholeArg);
} else {
for (const char of wholeArg.slice(1)) {
separatedArguments.push(`-${char}`);
}
}

const allArgumentsExist = separatedArguments.every(
(flag) => flag in handlers
);
if (!allArgumentsExist && permissive) {
separatedArguments = [wholeArg];
}

for (let j = 0; j < separatedArguments.length; j++) {
const arg = separatedArguments[j];
Expand Down
23 changes: 21 additions & 2 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -481,8 +481,27 @@ test('should stop parsing early with permissive', () => {
);

expect(result).to.deep.equal({
_: ['-v', '--foo', 'bar'],
'-d': 2
_: ['-dvd', '--foo', 'bar']
});
});

test('should not split unknown arguments with permissive', () => {
const argv = ['-dvd', '--foo', 'bar'];

const result = arg(
{
'-d': arg.COUNT,
'--foo': Boolean
},
{
argv,
permissive: true
}
);

expect(result).to.deep.equal({
_: ['-dvd', 'bar'],
'--foo': true
});
});

Expand Down