From 5207c32a01682f0188a36f5ffcc95b024df64bc4 Mon Sep 17 00:00:00 2001 From: tsosunchia <59512455+tsosunchia@users.noreply.github.com> Date: Sun, 6 Sep 2026 11:24:11 +0800 Subject: [PATCH] =?UTF-8?q?fix(cli):=20=E6=8C=89=E6=A8=A1=E5=BC=8F?= =?UTF-8?q?=E5=B1=95=E7=A4=BA=E6=9F=A5=E8=AF=A2=E9=BB=98=E8=AE=A4=E5=80=BC?= =?UTF-8?q?=E5=B9=B6=E4=BF=9D=E7=95=99=E4=BC=A0=E7=BB=9F=E9=87=87=E6=A0=B7?= =?UTF-8?q?=E6=AC=A1=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 3 +-- README_zh_CN.md | 3 +-- cmd/cmd.go | 10 ++++++++- cmd/cmd_test.go | 28 ++++++++++++++++++++++++ docs/mtr-default-migration-validation.md | 4 ++-- 5 files changed, 41 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 96491b1..19db2d4 100644 --- a/README.md +++ b/README.md @@ -894,8 +894,7 @@ Arguments: -q --queries Traceroute: latency samples per hop (default 3). MTR: max probes per hop; TUI/raw defaults to 0 (unlimited), - --report defaults to 10 when omitted. - Default: 3 + --report defaults to 10 when omitted --max-attempts Advanced: hard cap on probe packets per hop. Leave unset for auto sizing; raise on lossy links if --queries is not enough diff --git a/README_zh_CN.md b/README_zh_CN.md index 39bff76..4dd1f1e 100644 --- a/README_zh_CN.md +++ b/README_zh_CN.md @@ -869,8 +869,7 @@ Arguments: -q --queries Traceroute: latency samples per hop (default 3). MTR: max probes per hop; TUI/raw defaults to 0 (unlimited), - --report defaults to 10 when omitted. - Default: 3 + --report defaults to 10 when omitted --max-attempts Advanced: hard cap on probe packets per hop. Leave unset for auto sizing; raise on lossy links if --queries is not enough diff --git a/cmd/cmd.go b/cmd/cmd.go index 509de9f..859639b 100644 --- a/cmd/cmd.go +++ b/cmd/cmd.go @@ -395,6 +395,14 @@ func registerDataProviderFlag(parser *argparse.Parser) *string { }) } +func registerQueriesFlag(parser *argparse.Parser) *int { + queries := parser.Int("q", "queries", &argparse.Options{Help: buildQueriesHelp()}) + // Keep the traditional fallback without argparse advertising it for every mode. + // MTR applies its own defaults when this flag was not explicitly parsed. + *queries = 3 + return queries +} + func buildQueriesHelp() string { if !enableTraceroute { return "MTR only: max probes per hop. 0 = unlimited in TUI/raw; --report defaults to 10 when omitted. Start with 10-20 on unstable paths" @@ -1373,7 +1381,7 @@ func Execute() { fastTraceFlag := registerFastTraceFlag(parser) port := parser.Int("p", "port", &argparse.Options{Help: "Set the destination port to use. With default of 80 for \"tcp\", 33494 for \"udp\""}) icmpMode := registerICMPModeFlag(parser) - numMeasurements := parser.Int("q", "queries", &argparse.Options{Default: 3, Help: buildQueriesHelp()}) + numMeasurements := registerQueriesFlag(parser) maxAttempts := parser.Int("", "max-attempts", &argparse.Options{Help: buildMaxAttemptsHelp()}) parallelRequests := parser.Int("", "parallel-requests", &argparse.Options{Default: 18, Help: buildParallelRequestsHelp()}) maxHops := parser.Int("m", "max-hops", &argparse.Options{Default: 30, Help: "Set the max number of hops (max TTL to be reached)"}) diff --git a/cmd/cmd_test.go b/cmd/cmd_test.go index 6bd426d..083996a 100644 --- a/cmd/cmd_test.go +++ b/cmd/cmd_test.go @@ -1074,6 +1074,34 @@ func TestQueriesHelpDescribesModeSemantics(t *testing.T) { if enableTraceroute && !strings.Contains(help, "Traceroute: latency samples per hop (default 3)") { t.Fatalf("queries help missing traditional semantics: %s", help) } + if !enableTraceroute && !strings.HasPrefix(help, "MTR only: max probes per hop.") { + t.Fatalf("queries help missing MTR-only semantics: %s", help) + } +} + +func TestQueriesFlagDefaultsAndHelp(t *testing.T) { + for _, tc := range []struct { + args []string + want int + explicit bool + }{ + {nil, 3, false}, + {[]string{"-q", "0"}, 0, true}, + {[]string{"--queries", "10"}, 10, true}, + } { + parser := argparse.NewParser(appBinName, "") + queries := registerQueriesFlag(parser) + if err := parser.Parse(append([]string{appBinName}, tc.args...)); err != nil { + t.Fatal(err) + } + explicit, _, _, _ := detectExplicitProbeFlags(parser) + if *queries != tc.want || explicit != tc.explicit { + t.Fatalf("%v: queries=%d explicit=%v", tc.args, *queries, explicit) + } + if strings.Contains(parser.Usage(nil), "Default: 3") { + t.Fatal("queries help advertises an unconditional default") + } + } } func TestAdvancedHelpTextMentionsTuningGuidance(t *testing.T) { diff --git a/docs/mtr-default-migration-validation.md b/docs/mtr-default-migration-validation.md index fd1ff94..bd21b21 100644 --- a/docs/mtr-default-migration-validation.md +++ b/docs/mtr-default-migration-validation.md @@ -16,8 +16,8 @@ | UPX 产物 | 基线字节 | 本次实现字节 | 差值 | |---|---:|---:|---:| -| Linux amd64 | 4,733,908 | 4,553,524 | -180,384 | -| Linux arm64 | 4,129,916 | 4,132,428 | +2,512 | +| Linux amd64 | 4,733,908 | 4,553,388 | -180,520 | +| Linux arm64 | 4,129,916 | 4,132,492 | +2,576 | 规划阶段仅将基线 tiny 的 `enableMTR` 改为 `true`,未压缩体积也均未变化,UPX amd64/arm64 分别增加 624/1,012 字节。本次实现还包含显式传统模式和模式解析准备,因此应使用上表作为本次整体变更的比较。