Code to get exe name for usage message
This commit is contained in:
parent
2f83bbdc85
commit
8807f6d539
@ -46,7 +46,7 @@ Run-time dependencies:
|
|||||||
Build-time dependencies:
|
Build-time dependencies:
|
||||||
|
|
||||||
* ``gcc`` or ``clang``
|
* ``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``
|
* ``pkg-config``
|
||||||
* For building on Linux in addition to the above dependencies you might also
|
* 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
|
need to install the following packages, if they are not already installed by
|
||||||
|
|||||||
@ -241,7 +241,7 @@ func (self *OptionGroup) FindOption(name_with_hyphens string) *Option {
|
|||||||
// }}}
|
// }}}
|
||||||
|
|
||||||
type Command struct {
|
type Command struct {
|
||||||
Name string
|
Name, ExeName string
|
||||||
Usage, HelpText string
|
Usage, HelpText string
|
||||||
Hidden bool
|
Hidden bool
|
||||||
AllowOptionsAfterArgs int
|
AllowOptionsAfterArgs int
|
||||||
@ -357,7 +357,19 @@ func (self *Command) Root(args []string) *Command {
|
|||||||
return p
|
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) {
|
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"}
|
return nil, &ParseError{Message: "At least one arg must be supplied"}
|
||||||
}
|
}
|
||||||
ctx := Context{SeenCommands: make([]*Command, 0, 4)}
|
ctx := Context{SeenCommands: make([]*Command, 0, 4)}
|
||||||
if self.Name == "" {
|
self.ExeName = args[0]
|
||||||
self.Name = args[0]
|
|
||||||
}
|
|
||||||
err = self.parse_args(&ctx, args[1:])
|
err = self.parse_args(&ctx, args[1:])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
|||||||
23
tools/utils/misc.go
Normal file
23
tools/utils/misc.go
Normal 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
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user