Skip to content

Commit f593d3c

Browse files
committed
cmd/tailscale/cli: add "help" alias for --help
Fixes tailscale#14053 Change-Id: I0a13e11af089f02b0656fea0d316543c67591fb5 Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
1 parent bfe5cd8 commit f593d3c

File tree

2 files changed

+38
-2
lines changed

2 files changed

+38
-2
lines changed

cmd/tailscale/cli/cli.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,13 @@ func Run(args []string) (err error) {
9393

9494
args = CleanUpArgs(args)
9595

96-
if len(args) == 1 && (args[0] == "-V" || args[0] == "--version") {
97-
args = []string{"version"}
96+
if len(args) == 1 {
97+
switch args[0] {
98+
case "-V", "--version":
99+
args = []string{"version"}
100+
case "help":
101+
args = []string{"--help"}
102+
}
98103
}
99104

100105
var warnOnce sync.Once

cmd/tailscale/cli/cli_test.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"encoding/json"
1010
"flag"
1111
"fmt"
12+
"io"
1213
"net/netip"
1314
"reflect"
1415
"strings"
@@ -1480,3 +1481,33 @@ func TestParseNLArgs(t *testing.T) {
14801481
})
14811482
}
14821483
}
1484+
1485+
func TestHelpAlias(t *testing.T) {
1486+
var stdout, stderr bytes.Buffer
1487+
tstest.Replace[io.Writer](t, &Stdout, &stdout)
1488+
tstest.Replace[io.Writer](t, &Stderr, &stderr)
1489+
1490+
gotExit0 := false
1491+
defer func() {
1492+
if !gotExit0 {
1493+
t.Error("expected os.Exit(0) to be called")
1494+
return
1495+
}
1496+
if !strings.Contains(stderr.String(), "SUBCOMMANDS") {
1497+
t.Errorf("expected help output to contain SUBCOMMANDS; got stderr=%q; stdout=%q", stderr.String(), stdout.String())
1498+
}
1499+
}()
1500+
defer func() {
1501+
if e := recover(); e != nil {
1502+
if strings.Contains(fmt.Sprint(e), "unexpected call to os.Exit(0)") {
1503+
gotExit0 = true
1504+
} else {
1505+
t.Errorf("unexpected panic: %v", e)
1506+
}
1507+
}
1508+
}()
1509+
err := Run([]string{"help"})
1510+
if err != nil {
1511+
t.Fatalf("Run: %v", err)
1512+
}
1513+
}

0 commit comments

Comments
 (0)