File: command.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 (592 lines) | stat: -rw-r--r-- 14,924 bytes parent folder | download
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
// License: GPLv3 Copyright: 2022, Kovid Goyal, <kovid at kovidgoyal.net>

package cli

import (
	"fmt"
	"os"
	"path/filepath"
	"reflect"
	"strings"

	"github.com/kovidgoyal/kitty/tools/utils"
)

var _ = fmt.Print

type RunFunc = func(cmd *Command, args []string) (int, error)

type Command struct {
	Name, Group                       string
	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.
	AllowOptionsAfterArgs int
	// If true does not fail if the first non-option arg is not a sub-command
	SubCommandIsOptional bool
	// If true subcommands are ignored unless they are the first non-option argument
	SubCommandMustBeFirst bool
	// The entry point for this command
	Run RunFunc
	// The completer for args
	ArgCompleter CompletionFunc
	// Stop completion processing at this arg num
	StopCompletingAtArg int
	// Consider all args as non-options args when parsing for completion
	OnlyArgsAllowed bool
	// Pass through all args, useful for wrapper commands
	IgnoreAllArgs bool
	// Specialised arg parsing
	ParseArgsForCompletion func(cmd *Command, args []string, completions *Completions)
	// Callback that is called on error
	CallbackOnError func(cmd *Command, err error, during_parsing bool, exit_code int) (final_exit_code int)

	SubCommandGroups []*CommandGroup
	OptionGroups     []*OptionGroup
	Parent           *Command

	Args []string

	option_map      map[string]*Option
	IndexOfFirstArg int
}

func (self *Command) Clone(parent *Command) *Command {
	ans := *self
	ans.Args = make([]string, 0, 8)
	ans.Parent = parent
	ans.SubCommandGroups = make([]*CommandGroup, len(self.SubCommandGroups))
	ans.OptionGroups = make([]*OptionGroup, len(self.OptionGroups))
	ans.option_map = nil

	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
}

func (self *Command) AddClone(group string, src *Command) *Command {
	c := src.Clone(self)
	g := self.AddSubCommandGroup(group)
	c.Group = g.Title
	g.SubCommands = append(g.SubCommands, c)
	return c
}

func init_cmd(c *Command) {
	c.SubCommandGroups = make([]*CommandGroup, 0, 8)
	c.OptionGroups = make([]*OptionGroup, 0, 8)
	c.Args = make([]string, 0, 8)
	c.option_map = nil
}

func NewRootCommand() *Command {
	ans := Command{
		Name: filepath.Base(os.Args[0]),
	}
	init_cmd(&ans)
	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
}

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
}

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)

	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)}
			}
			seen_flags[q] = true
		}
		self.option_map[opt.Name] = opt
		return nil
	}
	err := self.VisitAllOptions(validate_options)
	if err != nil {
		return err
	}

	if self.option_map["Help"] == nil {
		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)}
		}
		self.option_map["Help"] = self.Add(OptionSpec{Name: "--help -h", Type: "bool-set", Help: "Show help for this command"})
	}

	if self.Parent == nil && self.option_map["Version"] == nil {
		if seen_flags["--version"] {
			return &ParseError{Message: fmt.Sprintf("The --version flag is assigned to an option other than Version in %s", self.Name)}
		}
		self.option_map["Version"] = self.Add(OptionSpec{Name: "--version", Type: "bool-set", Help: "Show version"})
	}

	return nil
}

func (self *Command) Root() *Command {
	p := self
	for p.Parent != nil {
		p = p.Parent
	}
	return p
}

func (self *Command) CommandStringForUsage() string {
	names := make([]string, 0, 8)
	p := self
	for p != nil {
		if p.Name != "" {
			names = append(names, p.Name)
		}
		p = p.Parent
	}
	return strings.Join(utils.Reverse(names), " ")
}

func (self *Command) ParseArgs(args []string) (*Command, error) {
	for ; self.Parent != nil; self = self.Parent {
	}
	err := self.Validate()
	if err != nil {
		return nil, err
	}
	if args == nil {
		args = os.Args
	}
	if len(args) < 1 {
		return nil, &ParseError{Message: "At least one arg must be supplied"}
	}
	ctx := Context{SeenCommands: make([]*Command, 0, 4)}
	err = self.parse_args(&ctx, args[1:])
	if err != nil {
		return nil, err
	}
	return ctx.SeenCommands[len(ctx.SeenCommands)-1], nil
}

func (self *Command) ResetAfterParseArgs() {
	for _, g := range self.SubCommandGroups {
		for _, sc := range g.SubCommands {
			sc.ResetAfterParseArgs()
		}
	}

	for _, g := range self.OptionGroups {
		for _, o := range g.Options {
			o.reset()
		}
	}
	self.option_map = nil
	self.IndexOfFirstArg = 0
	self.Args = make([]string, 0, 8)
}

func (self *Command) HasSubCommands() bool {
	for _, g := range self.SubCommandGroups {
		if len(g.SubCommands) > 0 {
			return true
		}
	}
	return false
}

func (self *Command) HasVisibleSubCommands() bool {
	for _, g := range self.SubCommandGroups {
		if g.HasVisibleSubCommands() {
			return true
		}
	}
	return false
}

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
}

func (self *Command) AllOptions() []*Option {
	ans := make([]*Option, 0, 64)
	_ = self.VisitAllOptions(func(o *Option) error { ans = append(ans, o); return nil })
	return ans
}

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
}

func sort_levenshtein_matches(q string, matches []string) {
	utils.StableSort(matches, func(a, b string) int {
		la, lb := utils.LevenshteinDistance(a, q, true), utils.LevenshteinDistance(b, q, true)
		if la != lb {
			return la - lb
		}
		return strings.Compare(a, b)
	})

}

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
}

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)
	_ = self.VisitAllOptions(func(opt *Option) error {
		for _, a := range opt.Aliases {
			as := a.String()
			if utils.LevenshteinDistance(as, q, true) <= max_distance {
				ans = append(ans, as)
			}
		}
		return nil
	})
	sort_levenshtein_matches(q, ans)
	return ans
}

func (self *Command) FindSubCommand(name string) *Command {
	for _, g := range self.SubCommandGroups {
		c := g.FindSubCommand(name)
		if c != nil {
			return c
		}
	}
	return nil
}

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
}

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
}

func (self *Command) AddOptionToGroupFromString(group string, items ...string) *Option {
	ans, err := self.AddOptionGroup(group).AddOptionFromString(self, items...)
	if err != nil {
		panic(err)
	}
	return ans

}

func (self *Command) AddToGroup(group string, s OptionSpec) *Option {
	ans, err := self.AddOptionGroup(group).AddOption(self, s)
	if err != nil {
		panic(err)
	}
	return ans
}

func (self *Command) AddOptionFromString(items ...string) *Option {
	return self.AddOptionToGroupFromString("", items...)
}

func (self *Command) Add(s OptionSpec) *Option {
	return self.AddToGroup("", s)
}

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++
		for _, po := range p.FindOptions(name_with_hyphens) {
			if po.Depth >= depth {
				ans = append(ans, po)
			}
		}
	}
	return ans

}

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
}

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
}

func (self *Command) GetOptionValues(pointer_to_options_struct any) error {
	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
		if utils.Capitalize(field_name) != field_name || !f.CanSet() {
			continue
		}
		opt := self.option_map[field_name]
		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 {
				if !is_string_slice(f) {
					return fmt.Errorf("The field: %s must be a []string", field_name)
				}
				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
}

func (self *Command) ExecArgs(args []string) (exit_code int) {
	root := self
	for root.Parent != nil {
		root = root.Parent
	}
	cmd, err := root.ParseArgs(args)
	if err != nil {
		if self.CallbackOnError != nil {
			return self.CallbackOnError(cmd, err, true, 1)
		}
		ShowError(err)
		return 1
	}
	help_opt := cmd.option_map["Help"]
	version_opt := root.option_map["Version"]
	if help_opt != nil && help_opt.parsed_value().(bool) {
		cmd.ShowHelp()
		return
	} else if version_opt != nil && version_opt.parsed_value().(bool) {
		root.ShowVersion()
		return
	} else if cmd.Run != nil {
		exit_code, err = cmd.Run(cmd, cmd.Args)
		if err != nil {
			if exit_code == 0 {
				exit_code = 1
			}
			if self.CallbackOnError != nil {
				return self.CallbackOnError(cmd, err, false, exit_code)
			}
			ShowError(err)
		}
	}
	return
}

func (self *Command) Exec(args ...string) {
	if len(args) == 0 {
		args = os.Args
	}
	os.Exit(self.ExecArgs(args))
}

func (self *Command) GetCompletions(argv []string, init_completions func(*Completions)) *Completions {
	ans := NewCompletions()
	if init_completions != nil {
		init_completions(ans)
	}
	if len(argv) > 0 {
		exe := argv[0]
		exe = filepath.Base(exe) // zsh completion script passes full path to exe when using aliases
		cmd := self.FindSubCommand(exe)
		if cmd != nil {
			if cmd.ParseArgsForCompletion != nil {
				cmd.ParseArgsForCompletion(cmd, argv[1:], ans)
			} else {
				completion_parse_args(cmd, argv[1:], ans)
			}
		}
	}
	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
	return ans
}