Code to get exe name for usage message

This commit is contained in:
Kovid Goyal 2022-09-21 07:59:07 +05:30
parent 2f83bbdc85
commit 8807f6d539
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
3 changed files with 39 additions and 6 deletions

View File

@ -46,7 +46,7 @@ Run-time dependencies:
Build-time dependencies:
* ``gcc`` or ``clang``
* ``go >= 1.17`` (see :file:`go.mod` for go packages used during building)
* ``go >= 1.18`` (see :file:`go.mod` for go packages used during building)
* ``pkg-config``
* For building on Linux in addition to the above dependencies you might also
need to install the following packages, if they are not already installed by

View File

@ -241,7 +241,7 @@ func (self *OptionGroup) FindOption(name_with_hyphens string) *Option {
// }}}
type Command struct {
Name string
Name, ExeName string
Usage, HelpText string
Hidden bool
AllowOptionsAfterArgs int
@ -357,7 +357,19 @@ func (self *Command) Root(args []string) *Command {
return p
}
func (self *Command) CommandStringForUsage(args []string) (*Command, error) {
func (self *Command) CommandStringForUsage(args []string) string {
names := make([]string, 0, 8)
p := self
for p != nil {
if p.Name != "" {
names = append(names, p.Name)
} else if p.ExeName != "" {
names = append(names, p.Name)
}
p = p.Parent
}
utils.Reverse(names)
return strings.Join(names, " ")
}
func (self *Command) ParseArgs(args []string) (*Command, error) {
@ -375,9 +387,7 @@ func (self *Command) ParseArgs(args []string) (*Command, error) {
return nil, &ParseError{Message: "At least one arg must be supplied"}
}
ctx := Context{SeenCommands: make([]*Command, 0, 4)}
if self.Name == "" {
self.Name = args[0]
}
self.ExeName = args[0]
err = self.parse_args(&ctx, args[1:])
if err != nil {
return nil, err

23
tools/utils/misc.go Normal file
View File

@ -0,0 +1,23 @@
// License: GPLv3 Copyright: 2022, Kovid Goyal, <kovid at kovidgoyal.net>
package utils
import (
"fmt"
)
var _ = fmt.Print
func Reverse[T any](s []T) {
for i, j := 0, len(s)-1; i < j; i, j = i+1, j-1 {
s[i], s[j] = s[j], s[i]
}
}
func Reversed[T any](s []T) []T {
ans := make([]T, len(s))
for i, x := range s {
ans[len(s)-1-i] = x
}
return ans
}