Start work on quick-access-terminal kitten

This commit is contained in:
Kovid Goyal
2025-04-28 19:52:04 +05:30
parent 83bb1553f7
commit 5683314784
13 changed files with 164 additions and 46 deletions

View File

@@ -78,10 +78,6 @@ func (self *Option) FormatOptionForMan(output io.Writer) {
fmt.Fprint(output, "\" ")
defval := self.Default
switch self.OptionType {
case StringOption:
if self.IsList {
defval = ""
}
case CountOption:
defval = ""
case BoolOption:
@@ -109,10 +105,6 @@ func (self *Option) FormatOption(output io.Writer, formatter *markup.Context, sc
}
defval := self.Default
switch self.OptionType {
case StringOption:
if self.IsList {
defval = ""
}
case CountOption:
defval = ""
case BoolOption:

View File

@@ -11,6 +11,7 @@ import (
"kitty/tools/cli/markup"
"kitty/tools/utils"
"kitty/tools/utils/shlex"
)
var _ = fmt.Print
@@ -178,7 +179,11 @@ func option_from_spec(spec OptionSpec) (*Option, error) {
}
ans.parsed_default = pval
if ans.IsList {
ans.parsed_default = []string{}
if ans.Default == "" {
ans.parsed_default = nil
} else if ans.parsed_default, err = shlex.Split(ans.Default); err != nil {
return nil, err
}
}
ans.Completer = spec.Completer
if ans.Aliases == nil || len(ans.Aliases) == 0 {

View File

@@ -105,6 +105,11 @@ func NormalizeOptionName(name string) string {
func (self *Option) parsed_value() any {
if len(self.values_from_cmdline) == 0 {
if self.IsList {
if self.parsed_default == nil {
return []string{}
}
}
return self.parsed_default
}
switch self.OptionType {
@@ -112,9 +117,12 @@ func (self *Option) parsed_value() any {
return len(self.parsed_values_from_cmdline)
case StringOption:
if self.IsList {
ans := make([]string, len(self.parsed_values_from_cmdline))
for i, x := range self.parsed_values_from_cmdline {
ans[i] = x.(string)
ans := make([]string, 0, len(self.parsed_values_from_cmdline)+2)
if self.parsed_default != nil {
ans = append(ans, self.parsed_default.([]string)...)
}
for _, x := range self.parsed_values_from_cmdline {
ans = append(ans, x.(string))
}
return ans
}
@@ -128,9 +136,9 @@ func (self *Option) parse_value(val string) (any, error) {
switch self.OptionType {
case BoolOption:
switch val {
case "true":
case "y", "yes", "true":
return true, nil
case "false":
case "n", "no", "false":
return false, nil
default:
return nil, &ParseError{Option: self, Message: fmt.Sprintf(":yellow:`%s` is not a valid value for :bold:`%s`.", val, self.seen_option)}

View File

@@ -23,18 +23,22 @@ type base_options struct {
}
type options struct {
FromParent int
SimpleString string
Choices string
SetMe bool
Int int
Float float64
List []string
FromParent int
SimpleString string
Choices string
SetMe bool
Int int
Float float64
List []string
ListWithDefault []string
}
func TestCLIParsing(t *testing.T) {
rt := func(expected_cmd *Command, cmdline string, expected_options any, expected_args ...string) {
if opts, ok := expected_options.(*options); ok && opts.ListWithDefault == nil {
opts.ListWithDefault = []string{`1`, `2`}
}
cp, err := shlex.Split(cmdline)
if err != nil {
t.Fatal(err)
@@ -77,6 +81,7 @@ func TestCLIParsing(t *testing.T) {
child1.Add(OptionSpec{Name: "--int -i", Type: "int"})
child1.Add(OptionSpec{Name: "--float", Type: "float"})
child1.Add(OptionSpec{Name: "--list", Type: "list"})
child1.Add(OptionSpec{Name: "--list-with-default -L", Type: "list", Default: "1 2"})
child1.SubCommandIsOptional = true
gc1 := child1.AddSubCommand(&Command{Name: "gc1"})
@@ -97,6 +102,7 @@ func TestCLIParsing(t *testing.T) {
rt(child1, "test child1 --int -3 --simple-s -s --float=3.3", &options{SimpleString: "-s", Int: -3, Float: 3.3})
rt(child1, "test child1 -bi=3 --float=3.3", &options{SetMe: true, Int: 3, Float: 3.3})
rt(child1, "test child1 --list -3 -p --list one", &options{FromParent: 1, List: []string{"-3", "one"}})
rt(child1, "test child1 -L 3 -L 4", &options{ListWithDefault: []string{`1`, `2`, `3`, `4`}})
rt(gc1, "test -p child1 -p gc1 xxx", &empty_options{}, "xxx")
_, err := child1.ParseArgs(strings.Split("test child1 --choices x", " "))