2022-09-20 17:01:17 +03:00
|
|
|
// License: GPLv3 Copyright: 2022, Kovid Goyal, <kovid at kovidgoyal.net>
|
|
|
|
|
|
|
|
package cli
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2022-09-20 18:14:16 +03:00
|
|
|
"os"
|
2022-09-23 10:00:41 +03:00
|
|
|
"path/filepath"
|
2022-09-21 02:37:40 +03:00
|
|
|
"reflect"
|
2022-09-20 17:01:17 +03:00
|
|
|
"strings"
|
2022-09-21 03:01:19 +03:00
|
|
|
|
|
|
|
"kitty/tools/utils"
|
2022-09-20 17:01:17 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
var _ = fmt.Print
|
|
|
|
|
2022-11-25 10:28:10 +03:00
|
|
|
type RunFunc = func(cmd *Command, args []string) (int, error)
|
|
|
|
|
2022-09-25 16:35:25 +03:00
|
|
|
type Command struct {
|
2022-09-25 10:09:42 +03:00
|
|
|
Name, Group string
|
2022-09-22 21:29:07 +03:00
|
|
|
Usage, ShortDescription, HelpText string
|
|
|
|
Hidden bool
|
|
|
|
|
|
|
|
// Number of non-option arguments after which to stop parsing options. 0 means no options after the first non-option arg.
|
2022-09-20 18:14:16 +03:00
|
|
|
AllowOptionsAfterArgs int
|
2022-09-22 21:29:07 +03:00
|
|
|
// If true does not fail if the first non-option arg is not a sub-command
|
|
|
|
SubCommandIsOptional bool
|
2022-09-25 18:21:38 +03:00
|
|
|
// If true subcommands are ignored unless they are the first non-option argument
|
|
|
|
SubCommandMustBeFirst bool
|
2022-09-25 10:09:42 +03:00
|
|
|
// The entry point for this command
|
2022-11-25 10:28:10 +03:00
|
|
|
Run RunFunc
|
2022-09-25 18:21:38 +03:00
|
|
|
// The completer for args
|
|
|
|
ArgCompleter CompletionFunc
|
|
|
|
// Stop completion processing at this arg num
|
|
|
|
StopCompletingAtArg int
|
2023-02-19 18:20:58 +03:00
|
|
|
// Consider all args as non-options args when parsing for completion
|
2022-09-26 09:24:57 +03:00
|
|
|
OnlyArgsAllowed bool
|
2023-02-19 18:07:17 +03:00
|
|
|
// Pass through all args, useful for wrapper commands
|
|
|
|
IgnoreAllArgs bool
|
2023-02-19 18:20:58 +03:00
|
|
|
// Specialised arg parsing
|
2022-09-25 18:21:38 +03:00
|
|
|
ParseArgsForCompletion func(cmd *Command, args []string, completions *Completions)
|
2023-06-29 05:06:24 +03:00
|
|
|
// Callback that is called on error
|
|
|
|
CallbackOnError func(cmd *Command, err error, during_parsing bool, exit_code int) (final_exit_code int)
|
2022-09-20 18:14:16 +03:00
|
|
|
|
2022-09-20 17:01:17 +03:00
|
|
|
SubCommandGroups []*CommandGroup
|
|
|
|
OptionGroups []*OptionGroup
|
|
|
|
Parent *Command
|
|
|
|
|
2022-09-20 18:14:16 +03:00
|
|
|
Args []string
|
2022-09-22 21:29:07 +03:00
|
|
|
|
2022-09-26 05:04:49 +03:00
|
|
|
option_map map[string]*Option
|
|
|
|
IndexOfFirstArg int
|
2022-09-20 18:14:16 +03:00
|
|
|
}
|
2022-09-20 17:01:17 +03:00
|
|
|
|
2022-09-20 18:14:16 +03:00
|
|
|
func (self *Command) Clone(parent *Command) *Command {
|
|
|
|
ans := *self
|
|
|
|
ans.Args = make([]string, 0, 8)
|
|
|
|
ans.Parent = parent
|
2022-09-25 09:53:52 +03:00
|
|
|
ans.SubCommandGroups = make([]*CommandGroup, len(self.SubCommandGroups))
|
|
|
|
ans.OptionGroups = make([]*OptionGroup, len(self.OptionGroups))
|
2022-09-25 18:21:38 +03:00
|
|
|
ans.option_map = nil
|
2022-09-20 18:14:16 +03:00
|
|
|
|
|
|
|
for i, o := range self.OptionGroups {
|
|
|
|
ans.OptionGroups[i] = o.Clone(&ans)
|
|
|
|
}
|
|
|
|
for i, g := range self.SubCommandGroups {
|
|
|
|
ans.SubCommandGroups[i] = g.Clone(&ans)
|
|
|
|
}
|
|
|
|
return &ans
|
|
|
|
}
|
|
|
|
|
2022-09-25 09:53:52 +03:00
|
|
|
func (self *Command) AddClone(group string, src *Command) *Command {
|
2022-09-20 18:14:16 +03:00
|
|
|
c := src.Clone(self)
|
|
|
|
g := self.AddSubCommandGroup(group)
|
2022-09-25 10:09:42 +03:00
|
|
|
c.Group = g.Title
|
2022-09-20 18:14:16 +03:00
|
|
|
g.SubCommands = append(g.SubCommands, c)
|
2022-09-25 09:53:52 +03:00
|
|
|
return c
|
2022-09-20 17:01:17 +03:00
|
|
|
}
|
|
|
|
|
2022-09-25 10:09:42 +03:00
|
|
|
func init_cmd(c *Command) {
|
|
|
|
c.SubCommandGroups = make([]*CommandGroup, 0, 8)
|
|
|
|
c.OptionGroups = make([]*OptionGroup, 0, 8)
|
|
|
|
c.Args = make([]string, 0, 8)
|
2022-09-25 18:21:38 +03:00
|
|
|
c.option_map = nil
|
2022-09-25 10:09:42 +03:00
|
|
|
}
|
|
|
|
|
2022-09-20 17:01:17 +03:00
|
|
|
func NewRootCommand() *Command {
|
|
|
|
ans := Command{
|
2022-09-25 10:09:42 +03:00
|
|
|
Name: filepath.Base(os.Args[0]),
|
2022-09-20 17:01:17 +03:00
|
|
|
}
|
2022-09-25 10:09:42 +03:00
|
|
|
init_cmd(&ans)
|
2022-09-20 17:01:17 +03:00
|
|
|
return &ans
|
|
|
|
}
|
|
|
|
|
|
|
|
func (self *Command) AddSubCommandGroup(title string) *CommandGroup {
|
|
|
|
for _, g := range self.SubCommandGroups {
|
|
|
|
if g.Title == title {
|
|
|
|
return g
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ans := CommandGroup{Title: title, SubCommands: make([]*Command, 0, 8)}
|
|
|
|
self.SubCommandGroups = append(self.SubCommandGroups, &ans)
|
|
|
|
return &ans
|
|
|
|
}
|
|
|
|
|
2022-09-25 10:09:42 +03:00
|
|
|
func (self *Command) AddSubCommand(ans *Command) *Command {
|
|
|
|
g := self.AddSubCommandGroup(ans.Group)
|
|
|
|
g.SubCommands = append(g.SubCommands, ans)
|
|
|
|
init_cmd(ans)
|
|
|
|
ans.Parent = self
|
|
|
|
return ans
|
2022-09-20 17:01:17 +03:00
|
|
|
}
|
|
|
|
|
2022-09-21 05:04:07 +03:00
|
|
|
func (self *Command) Validate() error {
|
|
|
|
seen_sc := make(map[string]bool)
|
|
|
|
for _, g := range self.SubCommandGroups {
|
|
|
|
for _, sc := range g.SubCommands {
|
|
|
|
if seen_sc[sc.Name] {
|
|
|
|
return &ParseError{Message: fmt.Sprintf("The sub-command :yellow:`%s` occurs twice inside %s", sc.Name, self.Name)}
|
|
|
|
}
|
|
|
|
seen_sc[sc.Name] = true
|
|
|
|
err := sc.Validate()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
seen_flags := make(map[string]bool)
|
2022-09-23 18:20:35 +03:00
|
|
|
|
|
|
|
self.option_map = make(map[string]*Option, 128)
|
|
|
|
validate_options := func(opt *Option) error {
|
|
|
|
if self.option_map[opt.Name] != nil {
|
|
|
|
return &ParseError{Message: fmt.Sprintf("The option :yellow:`%s` occurs twice inside %s", opt.Name, self.Name)}
|
|
|
|
}
|
|
|
|
for _, a := range opt.Aliases {
|
|
|
|
q := a.String()
|
|
|
|
if seen_flags[q] {
|
|
|
|
return &ParseError{Message: fmt.Sprintf("The option :yellow:`%s` occurs twice inside %s", q, self.Name)}
|
2022-09-21 05:04:07 +03:00
|
|
|
}
|
2022-09-23 18:20:35 +03:00
|
|
|
seen_flags[q] = true
|
2022-09-21 05:04:07 +03:00
|
|
|
}
|
2022-09-23 18:20:35 +03:00
|
|
|
self.option_map[opt.Name] = opt
|
|
|
|
return nil
|
2022-09-21 05:04:07 +03:00
|
|
|
}
|
2022-09-23 18:20:35 +03:00
|
|
|
err := self.VisitAllOptions(validate_options)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if self.option_map["Help"] == nil {
|
2022-09-23 10:00:41 +03:00
|
|
|
if seen_flags["-h"] || seen_flags["--help"] {
|
|
|
|
return &ParseError{Message: fmt.Sprintf("The --help or -h flags are assigned to an option other than Help in %s", self.Name)}
|
2022-09-21 05:04:07 +03:00
|
|
|
}
|
2022-09-23 18:20:35 +03:00
|
|
|
self.option_map["Help"] = self.Add(OptionSpec{Name: "--help -h", Type: "bool-set", Help: "Show help for this command"})
|
2022-09-23 10:00:41 +03:00
|
|
|
}
|
2022-09-22 21:29:07 +03:00
|
|
|
|
2022-09-23 18:20:35 +03:00
|
|
|
if self.Parent == nil && self.option_map["Version"] == nil {
|
2022-09-23 10:00:41 +03:00
|
|
|
if seen_flags["--version"] {
|
|
|
|
return &ParseError{Message: fmt.Sprintf("The --version flag is assigned to an option other than Version in %s", self.Name)}
|
|
|
|
}
|
2022-09-23 18:20:35 +03:00
|
|
|
self.option_map["Version"] = self.Add(OptionSpec{Name: "--version", Type: "bool-set", Help: "Show version"})
|
2022-09-21 05:04:07 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-09-23 18:20:35 +03:00
|
|
|
func (self *Command) Root() *Command {
|
2022-09-21 05:04:07 +03:00
|
|
|
p := self
|
|
|
|
for p.Parent != nil {
|
|
|
|
p = p.Parent
|
|
|
|
}
|
|
|
|
return p
|
|
|
|
}
|
|
|
|
|
2022-09-22 21:29:07 +03:00
|
|
|
func (self *Command) CommandStringForUsage() string {
|
2022-09-21 05:29:07 +03:00
|
|
|
names := make([]string, 0, 8)
|
|
|
|
p := self
|
|
|
|
for p != nil {
|
|
|
|
if p.Name != "" {
|
|
|
|
names = append(names, p.Name)
|
|
|
|
}
|
|
|
|
p = p.Parent
|
|
|
|
}
|
2022-09-21 08:21:43 +03:00
|
|
|
return strings.Join(utils.Reverse(names), " ")
|
2022-09-21 05:04:07 +03:00
|
|
|
}
|
|
|
|
|
2022-09-20 18:14:16 +03:00
|
|
|
func (self *Command) ParseArgs(args []string) (*Command, error) {
|
2022-09-23 18:20:35 +03:00
|
|
|
for ; self.Parent != nil; self = self.Parent {
|
2022-09-21 05:04:07 +03:00
|
|
|
}
|
|
|
|
err := self.Validate()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-09-20 18:14:16 +03:00
|
|
|
if args == nil {
|
|
|
|
args = os.Args
|
|
|
|
}
|
2022-09-21 05:04:07 +03:00
|
|
|
if len(args) < 1 {
|
|
|
|
return nil, &ParseError{Message: "At least one arg must be supplied"}
|
|
|
|
}
|
2022-09-20 18:14:16 +03:00
|
|
|
ctx := Context{SeenCommands: make([]*Command, 0, 4)}
|
2022-09-21 05:04:07 +03:00
|
|
|
err = self.parse_args(&ctx, args[1:])
|
2022-09-20 18:14:16 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-09-23 18:20:35 +03:00
|
|
|
return ctx.SeenCommands[len(ctx.SeenCommands)-1], nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (self *Command) ResetAfterParseArgs() {
|
|
|
|
for _, g := range self.SubCommandGroups {
|
|
|
|
for _, sc := range g.SubCommands {
|
|
|
|
sc.ResetAfterParseArgs()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-22 21:29:07 +03:00
|
|
|
for _, g := range self.OptionGroups {
|
|
|
|
for _, o := range g.Options {
|
2022-09-23 18:20:35 +03:00
|
|
|
o.reset()
|
2022-09-22 21:29:07 +03:00
|
|
|
}
|
|
|
|
}
|
2022-09-26 08:52:48 +03:00
|
|
|
self.option_map = nil
|
|
|
|
self.IndexOfFirstArg = 0
|
2022-09-23 18:20:35 +03:00
|
|
|
self.Args = make([]string, 0, 8)
|
2022-09-20 18:14:16 +03:00
|
|
|
}
|
|
|
|
|
2022-09-20 17:01:17 +03:00
|
|
|
func (self *Command) HasSubCommands() bool {
|
|
|
|
for _, g := range self.SubCommandGroups {
|
|
|
|
if len(g.SubCommands) > 0 {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2022-09-22 21:29:07 +03:00
|
|
|
func (self *Command) HasVisibleSubCommands() bool {
|
|
|
|
for _, g := range self.SubCommandGroups {
|
|
|
|
if g.HasVisibleSubCommands() {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2022-09-23 18:20:35 +03:00
|
|
|
func (self *Command) VisitAllOptions(callback func(*Option) error) error {
|
|
|
|
depth := 0
|
|
|
|
iter_opts := func(cmd *Command) error {
|
|
|
|
for _, g := range cmd.OptionGroups {
|
|
|
|
for _, o := range g.Options {
|
|
|
|
if o.Depth >= depth {
|
|
|
|
err := callback(o)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
for p := self; p != nil; p = p.Parent {
|
|
|
|
err := iter_opts(p)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
depth++
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-09-25 18:21:38 +03:00
|
|
|
func (self *Command) AllOptions() []*Option {
|
|
|
|
ans := make([]*Option, 0, 64)
|
2022-12-13 13:49:46 +03:00
|
|
|
_ = self.VisitAllOptions(func(o *Option) error { ans = append(ans, o); return nil })
|
2022-09-25 18:21:38 +03:00
|
|
|
return ans
|
|
|
|
}
|
|
|
|
|
2022-09-22 21:29:07 +03:00
|
|
|
func (self *Command) GetVisibleOptions() ([]string, map[string][]*Option) {
|
|
|
|
group_titles := make([]string, 0, len(self.OptionGroups))
|
|
|
|
gmap := make(map[string][]*Option)
|
|
|
|
|
|
|
|
add_options := func(group_title string, opts []*Option) {
|
|
|
|
if len(opts) == 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
x := gmap[group_title]
|
|
|
|
if x == nil {
|
|
|
|
group_titles = append(group_titles, group_title)
|
|
|
|
gmap[group_title] = opts
|
|
|
|
} else {
|
|
|
|
gmap[group_title] = append(x, opts...)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
depth := 0
|
|
|
|
process_cmd := func(cmd *Command) {
|
|
|
|
for _, g := range cmd.OptionGroups {
|
|
|
|
gopts := make([]*Option, 0, len(g.Options))
|
|
|
|
for _, o := range g.Options {
|
|
|
|
if !o.Hidden && o.Depth >= depth {
|
|
|
|
gopts = append(gopts, o)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
add_options(g.Title, gopts)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for p := self; p != nil; p = p.Parent {
|
|
|
|
process_cmd(p)
|
|
|
|
depth++
|
|
|
|
}
|
|
|
|
return group_titles, gmap
|
|
|
|
}
|
|
|
|
|
2022-09-30 17:07:59 +03:00
|
|
|
func sort_levenshtein_matches(q string, matches []string) {
|
2023-08-04 20:20:13 +03:00
|
|
|
utils.StableSort(matches, func(a, b string) int {
|
2022-09-30 17:07:59 +03:00
|
|
|
la, lb := utils.LevenshteinDistance(a, q, true), utils.LevenshteinDistance(b, q, true)
|
|
|
|
if la != lb {
|
2023-08-04 20:20:13 +03:00
|
|
|
return la - lb
|
2022-09-30 17:07:59 +03:00
|
|
|
}
|
2023-08-04 20:20:13 +03:00
|
|
|
return strings.Compare(a, b)
|
2022-09-30 17:07:59 +03:00
|
|
|
})
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func (self *Command) SuggestionsForCommand(name string, max_distance int /* good default is 2 */) []string {
|
|
|
|
ans := make([]string, 0, 8)
|
|
|
|
q := strings.ToLower(name)
|
|
|
|
for _, g := range self.SubCommandGroups {
|
|
|
|
for _, sc := range g.SubCommands {
|
|
|
|
if utils.LevenshteinDistance(sc.Name, q, true) <= max_distance {
|
|
|
|
ans = append(ans, sc.Name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
sort_levenshtein_matches(q, ans)
|
|
|
|
return ans
|
|
|
|
}
|
|
|
|
|
2022-09-30 11:48:21 +03:00
|
|
|
func (self *Command) SuggestionsForOption(name_with_hyphens string, max_distance int /* good default is 2 */) []string {
|
|
|
|
ans := make([]string, 0, 8)
|
|
|
|
q := strings.ToLower(name_with_hyphens)
|
2022-12-13 13:49:46 +03:00
|
|
|
_ = self.VisitAllOptions(func(opt *Option) error {
|
2022-09-30 11:48:21 +03:00
|
|
|
for _, a := range opt.Aliases {
|
|
|
|
as := a.String()
|
|
|
|
if utils.LevenshteinDistance(as, q, true) <= max_distance {
|
|
|
|
ans = append(ans, as)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
2022-09-30 17:07:59 +03:00
|
|
|
sort_levenshtein_matches(q, ans)
|
2022-09-30 11:48:21 +03:00
|
|
|
return ans
|
|
|
|
}
|
|
|
|
|
2022-09-20 17:01:17 +03:00
|
|
|
func (self *Command) FindSubCommand(name string) *Command {
|
|
|
|
for _, g := range self.SubCommandGroups {
|
2022-09-20 18:14:16 +03:00
|
|
|
c := g.FindSubCommand(name)
|
|
|
|
if c != nil {
|
|
|
|
return c
|
2022-09-20 17:01:17 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-09-30 13:07:03 +03:00
|
|
|
func (self *Command) FindSubCommands(prefix string) []*Command {
|
|
|
|
c := self.FindSubCommand(prefix)
|
|
|
|
if c != nil {
|
|
|
|
return []*Command{c}
|
|
|
|
}
|
|
|
|
ans := make([]*Command, 0, 4)
|
|
|
|
for _, g := range self.SubCommandGroups {
|
|
|
|
ans = g.FindSubCommands(prefix, ans)
|
|
|
|
}
|
|
|
|
return ans
|
|
|
|
}
|
|
|
|
|
2022-09-20 17:01:17 +03:00
|
|
|
func (self *Command) AddOptionGroup(title string) *OptionGroup {
|
|
|
|
for _, g := range self.OptionGroups {
|
|
|
|
if g.Title == title {
|
|
|
|
return g
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ans := OptionGroup{Title: title, Options: make([]*Option, 0, 8)}
|
|
|
|
self.OptionGroups = append(self.OptionGroups, &ans)
|
|
|
|
return &ans
|
|
|
|
}
|
|
|
|
|
2022-09-23 14:47:32 +03:00
|
|
|
func (self *Command) AddOptionToGroupFromString(group string, items ...string) *Option {
|
|
|
|
ans, err := self.AddOptionGroup(group).AddOptionFromString(self, items...)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return ans
|
|
|
|
|
2022-09-21 16:45:55 +03:00
|
|
|
}
|
|
|
|
|
2022-09-23 14:47:32 +03:00
|
|
|
func (self *Command) AddToGroup(group string, s OptionSpec) *Option {
|
|
|
|
ans, err := self.AddOptionGroup(group).AddOption(self, s)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return ans
|
2022-09-21 16:45:55 +03:00
|
|
|
}
|
|
|
|
|
2022-09-23 14:47:32 +03:00
|
|
|
func (self *Command) AddOptionFromString(items ...string) *Option {
|
|
|
|
return self.AddOptionToGroupFromString("", items...)
|
2022-09-20 17:01:17 +03:00
|
|
|
}
|
|
|
|
|
2022-09-23 14:47:32 +03:00
|
|
|
func (self *Command) Add(s OptionSpec) *Option {
|
|
|
|
return self.AddToGroup("", s)
|
2022-09-20 17:01:17 +03:00
|
|
|
}
|
|
|
|
|
2022-09-30 10:59:06 +03:00
|
|
|
func (self *Command) FindOptions(name_with_hyphens string) []*Option {
|
|
|
|
ans := make([]*Option, 0, 4)
|
|
|
|
for _, g := range self.OptionGroups {
|
|
|
|
x := g.FindOptions(name_with_hyphens)
|
|
|
|
if x != nil {
|
|
|
|
ans = append(ans, x...)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
depth := 0
|
|
|
|
for p := self.Parent; p != nil; p = p.Parent {
|
|
|
|
depth++
|
2023-08-30 06:50:52 +03:00
|
|
|
for _, po := range p.FindOptions(name_with_hyphens) {
|
|
|
|
if po.Depth >= depth {
|
|
|
|
ans = append(ans, po)
|
2022-09-30 10:59:06 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ans
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2022-09-20 17:01:17 +03:00
|
|
|
func (self *Command) FindOption(name_with_hyphens string) *Option {
|
|
|
|
for _, g := range self.OptionGroups {
|
|
|
|
q := g.FindOption(name_with_hyphens)
|
|
|
|
if q != nil {
|
|
|
|
return q
|
|
|
|
}
|
|
|
|
}
|
|
|
|
depth := 0
|
|
|
|
for p := self.Parent; p != nil; p = p.Parent {
|
|
|
|
depth++
|
|
|
|
q := p.FindOption(name_with_hyphens)
|
|
|
|
if q != nil && q.Depth >= depth {
|
|
|
|
return q
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type Context struct {
|
|
|
|
SeenCommands []*Command
|
|
|
|
}
|
2022-09-21 02:37:40 +03:00
|
|
|
|
2022-09-25 09:53:52 +03:00
|
|
|
func GetOptionValue[T any](self *Command, name string) (ans T, err error) {
|
|
|
|
opt := self.option_map[name]
|
|
|
|
if opt == nil {
|
|
|
|
err = fmt.Errorf("No option with the name: %s", name)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
ans, ok := opt.parsed_value().(T)
|
|
|
|
if !ok {
|
|
|
|
err = fmt.Errorf("The option %s is not of the correct type", name)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-09-21 05:41:46 +03:00
|
|
|
func (self *Command) GetOptionValues(pointer_to_options_struct any) error {
|
2022-09-21 02:37:40 +03:00
|
|
|
val := reflect.ValueOf(pointer_to_options_struct).Elem()
|
|
|
|
if val.Kind() != reflect.Struct {
|
|
|
|
return fmt.Errorf("Need a pointer to a struct to set option values on")
|
|
|
|
}
|
|
|
|
for i := 0; i < val.NumField(); i++ {
|
|
|
|
f := val.Field(i)
|
|
|
|
field_name := val.Type().Field(i).Name
|
2022-09-21 03:01:19 +03:00
|
|
|
if utils.Capitalize(field_name) != field_name || !f.CanSet() {
|
2022-09-21 02:37:40 +03:00
|
|
|
continue
|
|
|
|
}
|
2022-09-22 21:29:07 +03:00
|
|
|
opt := self.option_map[field_name]
|
2022-09-21 02:37:40 +03:00
|
|
|
if opt == nil {
|
|
|
|
return fmt.Errorf("No option with the name: %s", field_name)
|
|
|
|
}
|
|
|
|
switch opt.OptionType {
|
|
|
|
case IntegerOption, CountOption:
|
|
|
|
if f.Kind() != reflect.Int {
|
|
|
|
return fmt.Errorf("The field: %s must be an integer", field_name)
|
|
|
|
}
|
|
|
|
v := int64(opt.parsed_value().(int))
|
|
|
|
if f.OverflowInt(v) {
|
|
|
|
return fmt.Errorf("The value: %d is too large for the integer type used for the option: %s", v, field_name)
|
|
|
|
}
|
|
|
|
f.SetInt(v)
|
|
|
|
case FloatOption:
|
|
|
|
if f.Kind() != reflect.Float64 {
|
|
|
|
return fmt.Errorf("The field: %s must be a float64", field_name)
|
|
|
|
}
|
|
|
|
v := opt.parsed_value().(float64)
|
|
|
|
if f.OverflowFloat(v) {
|
|
|
|
return fmt.Errorf("The value: %f is too large for the integer type used for the option: %s", v, field_name)
|
|
|
|
}
|
|
|
|
f.SetFloat(v)
|
|
|
|
case BoolOption:
|
|
|
|
if f.Kind() != reflect.Bool {
|
|
|
|
return fmt.Errorf("The field: %s must be a boolean", field_name)
|
|
|
|
}
|
|
|
|
v := opt.parsed_value().(bool)
|
|
|
|
f.SetBool(v)
|
|
|
|
case StringOption:
|
|
|
|
if opt.IsList {
|
2022-09-21 04:22:45 +03:00
|
|
|
if !is_string_slice(f) {
|
|
|
|
return fmt.Errorf("The field: %s must be a []string", field_name)
|
2022-09-21 02:37:40 +03:00
|
|
|
}
|
|
|
|
v := opt.parsed_value().([]string)
|
|
|
|
f.Set(reflect.ValueOf(v))
|
|
|
|
} else {
|
|
|
|
if f.Kind() != reflect.String {
|
|
|
|
return fmt.Errorf("The field: %s must be a string", field_name)
|
|
|
|
}
|
|
|
|
v := opt.parsed_value().(string)
|
|
|
|
f.SetString(v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2022-09-21 20:41:52 +03:00
|
|
|
|
2023-02-04 10:24:49 +03:00
|
|
|
func (self *Command) ExecArgs(args []string) (exit_code int) {
|
2022-09-22 21:29:07 +03:00
|
|
|
root := self
|
|
|
|
for root.Parent != nil {
|
|
|
|
root = root.Parent
|
|
|
|
}
|
|
|
|
cmd, err := root.ParseArgs(args)
|
|
|
|
if err != nil {
|
2023-06-29 05:06:24 +03:00
|
|
|
if self.CallbackOnError != nil {
|
|
|
|
return self.CallbackOnError(cmd, err, true, 1)
|
|
|
|
}
|
2022-09-22 21:29:07 +03:00
|
|
|
ShowError(err)
|
2023-02-04 10:24:49 +03:00
|
|
|
return 1
|
2022-09-22 21:29:07 +03:00
|
|
|
}
|
|
|
|
help_opt := cmd.option_map["Help"]
|
2022-09-23 18:20:35 +03:00
|
|
|
version_opt := root.option_map["Version"]
|
2022-09-22 21:29:07 +03:00
|
|
|
if help_opt != nil && help_opt.parsed_value().(bool) {
|
|
|
|
cmd.ShowHelp()
|
2023-02-04 10:24:49 +03:00
|
|
|
return
|
2022-09-22 21:29:07 +03:00
|
|
|
} else if version_opt != nil && version_opt.parsed_value().(bool) {
|
2022-09-23 18:20:35 +03:00
|
|
|
root.ShowVersion()
|
2023-02-04 10:24:49 +03:00
|
|
|
return
|
2022-09-22 21:29:07 +03:00
|
|
|
} else if cmd.Run != nil {
|
|
|
|
exit_code, err = cmd.Run(cmd, cmd.Args)
|
|
|
|
if err != nil {
|
|
|
|
if exit_code == 0 {
|
|
|
|
exit_code = 1
|
|
|
|
}
|
2023-06-29 05:06:24 +03:00
|
|
|
if self.CallbackOnError != nil {
|
|
|
|
return self.CallbackOnError(cmd, err, false, exit_code)
|
|
|
|
}
|
|
|
|
ShowError(err)
|
2022-09-22 21:29:07 +03:00
|
|
|
}
|
|
|
|
}
|
2023-02-04 10:24:49 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (self *Command) Exec(args ...string) {
|
|
|
|
if len(args) == 0 {
|
|
|
|
args = os.Args
|
|
|
|
}
|
|
|
|
os.Exit(self.ExecArgs(args))
|
2022-09-22 21:29:07 +03:00
|
|
|
}
|
2022-09-25 18:21:38 +03:00
|
|
|
|
|
|
|
func (self *Command) GetCompletions(argv []string, init_completions func(*Completions)) *Completions {
|
2023-03-07 14:14:02 +03:00
|
|
|
ans := NewCompletions()
|
2022-09-25 18:21:38 +03:00
|
|
|
if init_completions != nil {
|
2023-03-07 14:14:02 +03:00
|
|
|
init_completions(ans)
|
2022-09-25 18:21:38 +03:00
|
|
|
}
|
|
|
|
if len(argv) > 0 {
|
|
|
|
exe := argv[0]
|
2023-10-10 02:52:40 +03:00
|
|
|
exe = filepath.Base(exe) // zsh completion script passes full path to exe when using aliases
|
2022-09-25 18:21:38 +03:00
|
|
|
cmd := self.FindSubCommand(exe)
|
|
|
|
if cmd != nil {
|
|
|
|
if cmd.ParseArgsForCompletion != nil {
|
2023-03-07 14:14:02 +03:00
|
|
|
cmd.ParseArgsForCompletion(cmd, argv[1:], ans)
|
2022-09-25 18:21:38 +03:00
|
|
|
} else {
|
2023-03-07 14:14:02 +03:00
|
|
|
completion_parse_args(cmd, argv[1:], ans)
|
2022-09-25 18:21:38 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
non_empty_groups := make([]*MatchGroup, 0, len(ans.Groups))
|
|
|
|
for _, gr := range ans.Groups {
|
|
|
|
if len(gr.Matches) > 0 {
|
|
|
|
non_empty_groups = append(non_empty_groups, gr)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ans.Groups = non_empty_groups
|
2023-03-07 14:14:02 +03:00
|
|
|
return ans
|
2022-09-25 18:21:38 +03:00
|
|
|
}
|