File: group.go

package info (click to toggle)
kitty 0.42.1-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 28,564 kB
  • sloc: ansic: 82,787; python: 55,191; objc: 5,122; sh: 1,295; xml: 364; makefile: 143; javascript: 78
file content (116 lines) | stat: -rw-r--r-- 2,666 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
// License: GPLv3 Copyright: 2022, Kovid Goyal, <kovid at kovidgoyal.net>

package cli

import (
	"fmt"
	"strings"
)

var _ = fmt.Print

type CommandGroup struct {
	SubCommands []*Command
	Title       string
}

func (self *CommandGroup) HasVisibleSubCommands() bool {
	for _, c := range self.SubCommands {
		if !c.Hidden {
			return true
		}
	}
	return false
}

func (self *CommandGroup) Clone(parent *Command) *CommandGroup {
	ans := CommandGroup{Title: self.Title, SubCommands: make([]*Command, len(self.SubCommands))}
	for i, o := range self.SubCommands {
		ans.SubCommands[i] = o.Clone(parent)
	}
	return &ans
}

func (self *CommandGroup) AddSubCommand(parent *Command, name string) *Command {
	ans := NewRootCommand()
	ans.Parent = parent
	ans.Name = name
	self.SubCommands = append(self.SubCommands, ans)
	return ans
}

func (self *CommandGroup) FindSubCommand(name string) *Command {
	for _, c := range self.SubCommands {
		if c.Name == name {
			return c
		}
	}
	return nil
}

func (self *CommandGroup) FindSubCommands(prefix string, matches []*Command) []*Command {
	for _, c := range self.SubCommands {
		if strings.HasPrefix(c.Name, prefix) {
			matches = append(matches, c)
		}
	}
	return matches
}

type OptionGroup struct {
	Options []*Option
	Title   string
}

func (self *OptionGroup) Clone(parent *Command) *OptionGroup {
	ans := OptionGroup{Title: self.Title, Options: make([]*Option, len(self.Options))}
	for i, o := range self.Options {
		c := *o
		c.init_option()
		c.Parent = parent
		ans.Options[i] = &c
	}
	return &ans
}

func (self *OptionGroup) AddOption(parent *Command, spec OptionSpec) (*Option, error) {
	ans, err := option_from_spec(spec)
	if err == nil {
		ans.Parent = parent
		self.Options = append(self.Options, ans)
	}
	return ans, err
}

func (self *OptionGroup) AddOptionFromString(parent *Command, items ...string) (*Option, error) {
	ans, err := OptionFromString(items...)
	if err == nil {
		ans.Parent = parent
		self.Options = append(self.Options, ans)
	}
	return ans, err
}

func (self *OptionGroup) FindOptions(prefix_with_hyphens string) []*Option {
	is_short := !strings.HasPrefix(prefix_with_hyphens, "--")
	option_name := NormalizeOptionName(prefix_with_hyphens)
	ans := make([]*Option, 0, 4)
	for _, q := range self.Options {
		if q.MatchingAlias(option_name, is_short) != "" {
			ans = append(ans, q)
		}
	}
	return ans

}

func (self *OptionGroup) FindOption(name_with_hyphens string) *Option {
	is_short := !strings.HasPrefix(name_with_hyphens, "--")
	option_name := NormalizeOptionName(name_with_hyphens)
	for _, q := range self.Options {
		if q.HasAlias(option_name, is_short) {
			return q
		}
	}
	return nil
}