Skip to content
This repository was archived by the owner on Jan 2, 2026. It is now read-only.

Commit 1660fdd

Browse files
committed
Move aliasing setup outside of prerun
1 parent c1fe54d commit 1660fdd

File tree

2 files changed

+72
-32
lines changed

2 files changed

+72
-32
lines changed

cli/cli.go

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -74,16 +74,21 @@ func (s *sd) initAliasing() {
7474
panic(err)
7575
}
7676

77-
s.root.PersistentPreRunE = func(_ *cobra.Command, args []string) error {
78-
alias, err := s.root.PersistentFlags().GetString("alias")
79-
if err != nil {
80-
return err
77+
s.root.Use = "sd"
78+
79+
// Flags haven't been parsed yet, we need to do it ourselves
80+
for i, arg := range os.Args {
81+
if (arg == "-a" || arg == "--alias") && len(os.Args) >= i+2 {
82+
alias := os.Args[i+1]
83+
if alias == "" {
84+
break
85+
}
86+
s.root.Use = alias
87+
s.root.Version = fmt.Sprintf("%s (aliased to %s)", s.root.Version, alias)
88+
logrus.Debug("Aliasing: sd replaced with ", alias, " in help text")
8189
}
82-
s.root.Use = alias
83-
s.root.Version = fmt.Sprintf("%s (aliased to %s)", s.root.Version, alias)
84-
logrus.Debug("Aliasing: sd replaced with ", alias, " in help text")
85-
return nil
8690
}
91+
8792
s.root.RunE = showUsage
8893
}
8994

cli/cli_test.go

Lines changed: 59 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -16,34 +16,69 @@ import (
1616
)
1717

1818
func TestInitAliasing(t *testing.T) {
19-
sd := &sd{
20-
root: &cobra.Command{
21-
Version: "1.0",
19+
var tests = []struct {
20+
name string
21+
args []string
22+
expected string
23+
}{
24+
{
25+
"defaults to sd",
26+
[]string{},
27+
"sd",
28+
},
29+
{
30+
"changes usage string (short)",
31+
[]string{"-a", "quack"},
32+
"quack",
33+
},
34+
{
35+
"changes usage string (long)",
36+
[]string{"--alias", "quack"},
37+
"quack",
38+
},
39+
{
40+
"does not change usage when empty",
41+
[]string{"--alias", ""},
42+
"sd",
43+
},
44+
{
45+
"does not change usage when not given",
46+
[]string{"--alias"},
47+
"sd",
48+
},
49+
{
50+
"keeps last when multiple given",
51+
[]string{"--alias", "foo", "--alias", "bar"},
52+
"bar",
53+
},
54+
{
55+
"ignores other params",
56+
[]string{"--foo", "foo", "--alias", "quack", "--bar", "bar"},
57+
"quack",
2258
},
2359
}
24-
sd.initAliasing()
25-
26-
t.Run("flag is hidden", func(t *testing.T) {
27-
assert.True(t, sd.root.PersistentFlags().Lookup("alias").Hidden)
28-
})
29-
30-
t.Run("adds a default alias flag", func(t *testing.T) {
31-
sd.root.ParseFlags([]string{""})
32-
33-
v, err := sd.root.PersistentFlags().GetString("alias")
34-
assert.NoError(t, err)
35-
assert.Equal(t, "sd", v)
36-
})
60+
for _, test := range tests {
61+
t.Run(test.name, func(t *testing.T) {
62+
var restore []string
63+
copy(restore, os.Args)
64+
defer func() {
65+
copy(os.Args, restore)
66+
}()
67+
68+
os.Args = test.args
69+
70+
sd := &sd{
71+
root: &cobra.Command{
72+
Version: "1.0",
73+
},
74+
}
3775

38-
t.Run("sets the name of the root command when aliased", func(t *testing.T) {
39-
sd.root.ParseFlags([]string{"-a", "quack"})
40-
sd.root.PersistentPreRunE(sd.root, []string{})
76+
sd.initAliasing()
4177

42-
_, err := sd.root.PersistentFlags().GetString("alias")
43-
assert.NoError(t, err)
44-
assert.Equal(t, "quack", sd.root.Use)
45-
assert.Equal(t, "1.0 (aliased to quack)", sd.root.Version)
46-
})
78+
assert.True(t, sd.root.PersistentFlags().Lookup("alias").Hidden)
79+
assert.Equal(t, test.expected, sd.root.Use)
80+
})
81+
}
4782
}
4883

4984
func TestInitCompletions(t *testing.T) {

0 commit comments

Comments
 (0)