Skip to content

Commit 2448b7b

Browse files
authored
completion: recognize renamed binary (#470)
If the command is renamed, don't use "gs" as the name in help or in the completion script. Use the renamed command name instead. e.g. ``` ❯ make bin/gs ❯ mv bin/gs gsp ❯ ./gsp shell completion bash complete -C /.../src/git-spice/gsp gsp ``` Refs #469
1 parent 586f84c commit 2448b7b

File tree

2 files changed

+13
-9
lines changed

2 files changed

+13
-9
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
kind: Fixed
2+
body: 'Shell completion: Don''t use an incorrect command name if the binary is renamed.'
3+
time: 2024-11-11T18:21:44.646934-08:00

main.go

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,9 @@ func main() {
112112
}
113113
}
114114

115+
cmdName := filepath.Base(os.Args[0])
115116
parser, err := kong.New(&cmd,
116-
kong.Name("gs"),
117+
kong.Name(cmdName),
117118
kong.Description("gs (git-spice) is a command line tool for stacking Git branches."),
118119
kong.Resolvers(spiceConfig),
119120
kong.Bind(logger, &cmd.globalOptions),
@@ -133,11 +134,11 @@ func main() {
133134
// For the help of the top-level command,
134135
// print a note about shorthand aliases.
135136
if len(ctx.Command()) == 0 {
136-
_, _ = fmt.Fprint(ctx.Stdout,
137-
"\n",
138-
"Aliases can be combined to form shorthands for commands. For example:\n",
139-
" gs bc => gs branch create\n",
140-
" gs cc => gs commit create\n",
137+
_, _ = fmt.Fprintf(ctx.Stdout,
138+
"\nAliases can be combined to form shorthands for commands. For example:\n"+
139+
" %[1]v bc => %[1]v branch create\n"+
140+
" %[1]v cc => %[1]v commit create\n",
141+
cmdName,
141142
)
142143
}
143144

@@ -186,7 +187,7 @@ func main() {
186187
args = []string{"--help"}
187188
parser.Exit = func(int) {
188189
logger.Print("")
189-
logger.Fatal("gs: please provide a command")
190+
logger.Fatalf("%v: please provide a command", cmdName)
190191
}
191192
} else {
192193
// Otherwise, expand the shorthands before parsing.
@@ -195,11 +196,11 @@ func main() {
195196

196197
kctx, err := parser.Parse(args)
197198
if err != nil {
198-
logger.Fatalf("gs: %v", err)
199+
logger.Fatalf("%v: %v", cmdName, err)
199200
}
200201

201202
if err := kctx.Run(builtinShorthands); err != nil {
202-
logger.Fatalf("gs: %v", err)
203+
logger.Fatalf("%v: %v", cmdName, err)
203204
}
204205
}
205206

0 commit comments

Comments
 (0)