util: parseArgs() treats a `-` inside a short option group as the `--` terminator
Maintainers usually reply within 1 day
Assessment
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Newbie friendliness
- 75/100
- Issue type
- Bug
- Clarity
- Clearly specified
- Activity status
- Active
- Tech stack
- javascript, nodejs
- Domain
- cli
Research direction
Start with argsToTokens() in lib/internal/util/parse_args/parse_args.js, where a short option group like -a-b is expanded and pushed back onto the argument list. Check how the loop orders the arg === '--' test against the group counter, so a synthesized -- is not treated as the real terminator. Run the reproduction from the issue with strict and non-strict modes, plus tokens: true, and confirm -a-b yields an unknown-option error and each token's index matches its source argument. Done when the existing parseArgs tests pass and new cases cover -a-b and -a-.
Written by the indexing model from the issue text.
Description
Version
v28.0.0-pre (main, d2812af0), also v22.20.0
Platform
Linux 7.0.0-38-generic x86_64
Subsystem
util
What steps will reproduce the bug?
const { parseArgs } = require('node:util');
const options = { a: { type: 'boolean' }, b: { type: 'boolean' } };
// Non-strict, with tokens.
const args = ['-a-b', 'pos'];
const { values, positionals, tokens } = parseArgs({
args, options, strict: false, allowPositionals: true, tokens: true,
});
console.log(values, positionals);
for (const token of tokens) {
console.log(token, '-> args[index] =', args[token.index]);
}
// Strict.
for (const arg of ['-a-b', '-a-']) {
try {
parseArgs({ args: [arg], options });
console.log(arg, 'ok');
} catch (err) {
console.log(arg, err.code, '-', err.message);
}
}
How often does it reproduce? Is there a required condition?
Every time. Needs a - character inside a short option group, for example -a-b or -a-.
What is the expected behavior?
- is not a valid option character, so -a-b should be reported the same way as any other unknown short option in a group (compare -ar with r unknown). In strict mode that is ERR_PARSE_ARGS_UNKNOWN_OPTION. With tokens: true, every token's index should point at the argument it came from, as the docs say: "the source argument for a token is args[token.index]".
What do you see instead?
[Object: null prototype] { a: true } [ '-b', 'pos' ]
{ kind: 'option', name: 'a', rawName: '-a', index: 0, value: undefined, inlineValue: undefined } -> args[index] = -a-b
{ kind: 'option-terminator', index: 0 } -> args[index] = -a-b
{ kind: 'positional', index: 1, value: '-b' } -> args[index] = pos
{ kind: 'positional', index: 2, value: 'pos' } -> args[index] = undefined
-a-b ERR_PARSE_ARGS_UNEXPECTED_POSITIONAL - Unexpected argument '-b'. This command does not take positional arguments
-a- ok
-bbecomes a positional instead of an option.- An
option-terminatortoken appears although the user never passed--. - The positional indexes are shifted by one, so
args[token.index]is the wrong argument orundefined. - In strict mode
-a-breports a misleading unexpected-positional error, and-a-is accepted silently.
Additional information
argsToTokens() in lib/internal/util/parse_args/parse_args.js expands a short option group -a-b into -a, --, -b and pushes them back onto the argument list. The loop then checks arg === '--' before it looks at whether the argument came from a group, so the synthesized -- is handled as the real terminator. The group counter (groupCount) already knows the argument was expanded.
For comparison, getopt(3) reports -a-b as invalid option -- '-'.
I have a small fix with tests and can open a PR.
- Dominant language
- JavaScript
- Stars
- 123k
- Forks
- 39.3k
- Avg merge
- 4d 15h
- Merged PRs (30d)
- 238
Getting set up
- No Dockerfile or Docker Compose file
- Has a pull request template
- Read the contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from nodejs/node
-
http2: content-encoding is incorrectly listed in kSingleValueHeadersPossibly taken @inoway46 claimed this 2 days ago. Openconfirmed-bug http2
Difficulty 2/5 1-3 hours Newbie friendliness 74/100
nodejs/node#67151 · 1 comment ·
Maintainers usually reply within 1 day
-
util.parseEnv and --env-file keep a leading UTF-8 BOM in the first keyPossibly taken @helmipradita claimed this 4 days ago. Open
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
Maintainers usually reply within 1 day
-
node:internal/inspector/network_http: `TypeError: Missing dataLength` in event when response uses `setEncoding()`May be free again A pull request for this issue was closed without being merged. Open
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
nodejs/node#66551 · 1 comment ·
Maintainers usually reply within 1 day
-
[Docs] `process.loadEnvFile()` does not document behaviour when variables already existPossibly taken @Sepandard claimed this 38 days ago. Opendoc
Difficulty 1/5 Under an hour Newbie friendliness 90/100
Maintainers usually reply within 1 day
-
Stream.prototype.forEach will block in first promise in queue before read more chunkPossibly taken @mmustafasenoglu claimed this 39 days ago. Opendoc
Difficulty 2/5 1-3 hours Newbie friendliness 65/100
Maintainers usually reply within 1 day
Similar issues
-
Difficulty 2/5 1-3 hours Newbie friendliness 66/100
riverqueue/river#1576 ·
Maintainers usually reply within 1 day
-
documentation good first issue
Difficulty 1/5 Under an hour Newbie friendliness 72/100
Maintainers usually reply within 1 day
-
good first issue help wanted
Difficulty 1/5 Under an hour Newbie friendliness 88/100
modrollstudio/Modroll-Studio-Website#80 · 1 comment ·
Maintainers usually reply within 1 day
-
good first issue help wanted
Difficulty 2/5 1-3 hours Newbie friendliness 85/100
mafzalkalwardev/mailforge#21 ·
-
good first issue help wanted
Difficulty 1/5 Under an hour Newbie friendliness 82/100