util: parseArgs() treats a `-` inside a short option group as the `--` terminator
Maintainer thường phản hồi trong vòng 1 ngày
Đánh giá
- Độ khó
- 2/5
- Thời gian dự kiến
- 1-3 giờ
- Mức phù hợp với người mới
- 75/100
- Loại issue
- Lỗi
- Độ rõ ràng
- Đặc tả rõ ràng
- Mức độ hoạt động
- Sôi nổi
- Công nghệ
- javascript, nodejs
- Lĩnh vực
- cli
Hướng nghiên cứu
Bắt đầu từ argsToTokens() trong lib/internal/util/parse_args/parse_args.js, nơi một nhóm tùy chọn ngắn như -a-b được mở rộng và đẩy trở lại danh sách đối số. Kiểm tra thứ tự vòng lặp so sánh điều kiện arg === '--' với bộ đếm của nhóm, để một -- được tạo ra không bị coi là dấu kết thúc thật. Chạy lại tái hiện từ issue ở chế độ strict và non-strict, cùng với tokens: true, và xác nhận -a-b gây lỗi tùy chọn không xác định, đồng thời chỉ số của mỗi token khớp với đối số nguồn. Hoàn tất khi các test parseArgs hiện có đều qua và các trường hợp mới bao phủ -a-b và -a-.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Mô tả
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.
- Ngôn ngữ chính
- JavaScript
- Star
- 123k
- Fork
- 39.3k
- Merge trung bình
- 4 ngày 15 giờ
- Pull request đã merge (30 ngày)
- 238
Chuẩn bị môi trường
- Không có Dockerfile hay tệp Docker Compose
- Có mẫu pull request
- Đọc hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Issue khác của nodejs/node
-
http2: content-encoding is incorrectly listed in kSingleValueHeadersCó thể đã có người làm @inoway46 đã nhận 2 ngày trước. Đang mởconfirmed-bug http2
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 74/100
nodejs/node#67151 · 1 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
-
util.parseEnv and --env-file keep a leading UTF-8 BOM in the first keyCó thể đã có người làm @helmipradita đã nhận 4 ngày trước. Đang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 78/100
Maintainer thường phản hồi trong vòng 1 ngày
-
node:internal/inspector/network_http: `TypeError: Missing dataLength` in event when response uses `setEncoding()`Có thể làm lại được Pull request cho issue này đã bị đóng mà không được merge. Đang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 78/100
nodejs/node#66551 · 1 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
-
[Docs] `process.loadEnvFile()` does not document behaviour when variables already existCó thể đã có người làm @Sepandard đã nhận 38 ngày trước. Đang mởdoc
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 90/100
Maintainer thường phản hồi trong vòng 1 ngày
-
Stream.prototype.forEach will block in first promise in queue before read more chunkCó thể đã có người làm @mmustafasenoglu đã nhận 39 ngày trước. Đang mởdoc
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 65/100
Maintainer thường phản hồi trong vòng 1 ngày
Issue tương tự
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 66/100
riverqueue/river#1576 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
documentation good first issue
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 72/100
Maintainer thường phản hồi trong vòng 1 ngày
-
good first issue help wanted
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 88/100
modrollstudio/Modroll-Studio-Website#80 · 1 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Add tests for CSV email parsingĐang mởgood first issue help wanted
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 85/100
mafzalkalwardev/mailforge#21 ·
-
good first issue help wanted
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 82/100