command.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593
  1. // License: GPLv3 Copyright: 2022, Kovid Goyal, <kovid at kovidgoyal.net>
  2. package cli
  3. import (
  4. "fmt"
  5. "os"
  6. "path/filepath"
  7. "reflect"
  8. "strings"
  9. "kitty/tools/utils"
  10. )
  11. var _ = fmt.Print
  12. type RunFunc = func(cmd *Command, args []string) (int, error)
  13. type Command struct {
  14. Name, Group string
  15. Usage, ShortDescription, HelpText string
  16. Hidden bool
  17. // Number of non-option arguments after which to stop parsing options. 0 means no options after the first non-option arg.
  18. AllowOptionsAfterArgs int
  19. // If true does not fail if the first non-option arg is not a sub-command
  20. SubCommandIsOptional bool
  21. // If true subcommands are ignored unless they are the first non-option argument
  22. SubCommandMustBeFirst bool
  23. // The entry point for this command
  24. Run RunFunc
  25. // The completer for args
  26. ArgCompleter CompletionFunc
  27. // Stop completion processing at this arg num
  28. StopCompletingAtArg int
  29. // Consider all args as non-options args when parsing for completion
  30. OnlyArgsAllowed bool
  31. // Pass through all args, useful for wrapper commands
  32. IgnoreAllArgs bool
  33. // Specialised arg parsing
  34. ParseArgsForCompletion func(cmd *Command, args []string, completions *Completions)
  35. // Callback that is called on error
  36. CallbackOnError func(cmd *Command, err error, during_parsing bool, exit_code int) (final_exit_code int)
  37. SubCommandGroups []*CommandGroup
  38. OptionGroups []*OptionGroup
  39. Parent *Command
  40. Args []string
  41. option_map map[string]*Option
  42. IndexOfFirstArg int
  43. }
  44. func (self *Command) Clone(parent *Command) *Command {
  45. ans := *self
  46. ans.Args = make([]string, 0, 8)
  47. ans.Parent = parent
  48. ans.SubCommandGroups = make([]*CommandGroup, len(self.SubCommandGroups))
  49. ans.OptionGroups = make([]*OptionGroup, len(self.OptionGroups))
  50. ans.option_map = nil
  51. for i, o := range self.OptionGroups {
  52. ans.OptionGroups[i] = o.Clone(&ans)
  53. }
  54. for i, g := range self.SubCommandGroups {
  55. ans.SubCommandGroups[i] = g.Clone(&ans)
  56. }
  57. return &ans
  58. }
  59. func (self *Command) AddClone(group string, src *Command) *Command {
  60. c := src.Clone(self)
  61. g := self.AddSubCommandGroup(group)
  62. c.Group = g.Title
  63. g.SubCommands = append(g.SubCommands, c)
  64. return c
  65. }
  66. func init_cmd(c *Command) {
  67. c.SubCommandGroups = make([]*CommandGroup, 0, 8)
  68. c.OptionGroups = make([]*OptionGroup, 0, 8)
  69. c.Args = make([]string, 0, 8)
  70. c.option_map = nil
  71. }
  72. func NewRootCommand() *Command {
  73. ans := Command{
  74. Name: filepath.Base(os.Args[0]),
  75. }
  76. init_cmd(&ans)
  77. return &ans
  78. }
  79. func (self *Command) AddSubCommandGroup(title string) *CommandGroup {
  80. for _, g := range self.SubCommandGroups {
  81. if g.Title == title {
  82. return g
  83. }
  84. }
  85. ans := CommandGroup{Title: title, SubCommands: make([]*Command, 0, 8)}
  86. self.SubCommandGroups = append(self.SubCommandGroups, &ans)
  87. return &ans
  88. }
  89. func (self *Command) AddSubCommand(ans *Command) *Command {
  90. g := self.AddSubCommandGroup(ans.Group)
  91. g.SubCommands = append(g.SubCommands, ans)
  92. init_cmd(ans)
  93. ans.Parent = self
  94. return ans
  95. }
  96. func (self *Command) Validate() error {
  97. seen_sc := make(map[string]bool)
  98. for _, g := range self.SubCommandGroups {
  99. for _, sc := range g.SubCommands {
  100. if seen_sc[sc.Name] {
  101. return &ParseError{Message: fmt.Sprintf("The sub-command :yellow:`%s` occurs twice inside %s", sc.Name, self.Name)}
  102. }
  103. seen_sc[sc.Name] = true
  104. err := sc.Validate()
  105. if err != nil {
  106. return err
  107. }
  108. }
  109. }
  110. seen_flags := make(map[string]bool)
  111. self.option_map = make(map[string]*Option, 128)
  112. validate_options := func(opt *Option) error {
  113. if self.option_map[opt.Name] != nil {
  114. return &ParseError{Message: fmt.Sprintf("The option :yellow:`%s` occurs twice inside %s", opt.Name, self.Name)}
  115. }
  116. for _, a := range opt.Aliases {
  117. q := a.String()
  118. if seen_flags[q] {
  119. return &ParseError{Message: fmt.Sprintf("The option :yellow:`%s` occurs twice inside %s", q, self.Name)}
  120. }
  121. seen_flags[q] = true
  122. }
  123. self.option_map[opt.Name] = opt
  124. return nil
  125. }
  126. err := self.VisitAllOptions(validate_options)
  127. if err != nil {
  128. return err
  129. }
  130. if self.option_map["Help"] == nil {
  131. if seen_flags["-h"] || seen_flags["--help"] {
  132. return &ParseError{Message: fmt.Sprintf("The --help or -h flags are assigned to an option other than Help in %s", self.Name)}
  133. }
  134. self.option_map["Help"] = self.Add(OptionSpec{Name: "--help -h", Type: "bool-set", Help: "Show help for this command"})
  135. }
  136. if self.Parent == nil && self.option_map["Version"] == nil {
  137. if seen_flags["--version"] {
  138. return &ParseError{Message: fmt.Sprintf("The --version flag is assigned to an option other than Version in %s", self.Name)}
  139. }
  140. self.option_map["Version"] = self.Add(OptionSpec{Name: "--version", Type: "bool-set", Help: "Show version"})
  141. }
  142. return nil
  143. }
  144. func (self *Command) Root() *Command {
  145. p := self
  146. for p.Parent != nil {
  147. p = p.Parent
  148. }
  149. return p
  150. }
  151. func (self *Command) CommandStringForUsage() string {
  152. names := make([]string, 0, 8)
  153. p := self
  154. for p != nil {
  155. if p.Name != "" {
  156. names = append(names, p.Name)
  157. }
  158. p = p.Parent
  159. }
  160. return strings.Join(utils.Reverse(names), " ")
  161. }
  162. func (self *Command) ParseArgs(args []string) (*Command, error) {
  163. for ; self.Parent != nil; self = self.Parent {
  164. }
  165. err := self.Validate()
  166. if err != nil {
  167. return nil, err
  168. }
  169. if args == nil {
  170. args = os.Args
  171. }
  172. if len(args) < 1 {
  173. return nil, &ParseError{Message: "At least one arg must be supplied"}
  174. }
  175. ctx := Context{SeenCommands: make([]*Command, 0, 4)}
  176. err = self.parse_args(&ctx, args[1:])
  177. if err != nil {
  178. return nil, err
  179. }
  180. return ctx.SeenCommands[len(ctx.SeenCommands)-1], nil
  181. }
  182. func (self *Command) ResetAfterParseArgs() {
  183. for _, g := range self.SubCommandGroups {
  184. for _, sc := range g.SubCommands {
  185. sc.ResetAfterParseArgs()
  186. }
  187. }
  188. for _, g := range self.OptionGroups {
  189. for _, o := range g.Options {
  190. o.reset()
  191. }
  192. }
  193. self.option_map = nil
  194. self.IndexOfFirstArg = 0
  195. self.Args = make([]string, 0, 8)
  196. }
  197. func (self *Command) HasSubCommands() bool {
  198. for _, g := range self.SubCommandGroups {
  199. if len(g.SubCommands) > 0 {
  200. return true
  201. }
  202. }
  203. return false
  204. }
  205. func (self *Command) HasVisibleSubCommands() bool {
  206. for _, g := range self.SubCommandGroups {
  207. if g.HasVisibleSubCommands() {
  208. return true
  209. }
  210. }
  211. return false
  212. }
  213. func (self *Command) VisitAllOptions(callback func(*Option) error) error {
  214. depth := 0
  215. iter_opts := func(cmd *Command) error {
  216. for _, g := range cmd.OptionGroups {
  217. for _, o := range g.Options {
  218. if o.Depth >= depth {
  219. err := callback(o)
  220. if err != nil {
  221. return err
  222. }
  223. }
  224. }
  225. }
  226. return nil
  227. }
  228. for p := self; p != nil; p = p.Parent {
  229. err := iter_opts(p)
  230. if err != nil {
  231. return err
  232. }
  233. depth++
  234. }
  235. return nil
  236. }
  237. func (self *Command) AllOptions() []*Option {
  238. ans := make([]*Option, 0, 64)
  239. _ = self.VisitAllOptions(func(o *Option) error { ans = append(ans, o); return nil })
  240. return ans
  241. }
  242. func (self *Command) GetVisibleOptions() ([]string, map[string][]*Option) {
  243. group_titles := make([]string, 0, len(self.OptionGroups))
  244. gmap := make(map[string][]*Option)
  245. add_options := func(group_title string, opts []*Option) {
  246. if len(opts) == 0 {
  247. return
  248. }
  249. x := gmap[group_title]
  250. if x == nil {
  251. group_titles = append(group_titles, group_title)
  252. gmap[group_title] = opts
  253. } else {
  254. gmap[group_title] = append(x, opts...)
  255. }
  256. }
  257. depth := 0
  258. process_cmd := func(cmd *Command) {
  259. for _, g := range cmd.OptionGroups {
  260. gopts := make([]*Option, 0, len(g.Options))
  261. for _, o := range g.Options {
  262. if !o.Hidden && o.Depth >= depth {
  263. gopts = append(gopts, o)
  264. }
  265. }
  266. add_options(g.Title, gopts)
  267. }
  268. }
  269. for p := self; p != nil; p = p.Parent {
  270. process_cmd(p)
  271. depth++
  272. }
  273. return group_titles, gmap
  274. }
  275. func sort_levenshtein_matches(q string, matches []string) {
  276. utils.StableSort(matches, func(a, b string) int {
  277. la, lb := utils.LevenshteinDistance(a, q, true), utils.LevenshteinDistance(b, q, true)
  278. if la != lb {
  279. return la - lb
  280. }
  281. return strings.Compare(a, b)
  282. })
  283. }
  284. func (self *Command) SuggestionsForCommand(name string, max_distance int /* good default is 2 */) []string {
  285. ans := make([]string, 0, 8)
  286. q := strings.ToLower(name)
  287. for _, g := range self.SubCommandGroups {
  288. for _, sc := range g.SubCommands {
  289. if utils.LevenshteinDistance(sc.Name, q, true) <= max_distance {
  290. ans = append(ans, sc.Name)
  291. }
  292. }
  293. }
  294. sort_levenshtein_matches(q, ans)
  295. return ans
  296. }
  297. func (self *Command) SuggestionsForOption(name_with_hyphens string, max_distance int /* good default is 2 */) []string {
  298. ans := make([]string, 0, 8)
  299. q := strings.ToLower(name_with_hyphens)
  300. _ = self.VisitAllOptions(func(opt *Option) error {
  301. for _, a := range opt.Aliases {
  302. as := a.String()
  303. if utils.LevenshteinDistance(as, q, true) <= max_distance {
  304. ans = append(ans, as)
  305. }
  306. }
  307. return nil
  308. })
  309. sort_levenshtein_matches(q, ans)
  310. return ans
  311. }
  312. func (self *Command) FindSubCommand(name string) *Command {
  313. for _, g := range self.SubCommandGroups {
  314. c := g.FindSubCommand(name)
  315. if c != nil {
  316. return c
  317. }
  318. }
  319. return nil
  320. }
  321. func (self *Command) FindSubCommands(prefix string) []*Command {
  322. c := self.FindSubCommand(prefix)
  323. if c != nil {
  324. return []*Command{c}
  325. }
  326. ans := make([]*Command, 0, 4)
  327. for _, g := range self.SubCommandGroups {
  328. ans = g.FindSubCommands(prefix, ans)
  329. }
  330. return ans
  331. }
  332. func (self *Command) AddOptionGroup(title string) *OptionGroup {
  333. for _, g := range self.OptionGroups {
  334. if g.Title == title {
  335. return g
  336. }
  337. }
  338. ans := OptionGroup{Title: title, Options: make([]*Option, 0, 8)}
  339. self.OptionGroups = append(self.OptionGroups, &ans)
  340. return &ans
  341. }
  342. func (self *Command) AddOptionToGroupFromString(group string, items ...string) *Option {
  343. ans, err := self.AddOptionGroup(group).AddOptionFromString(self, items...)
  344. if err != nil {
  345. panic(err)
  346. }
  347. return ans
  348. }
  349. func (self *Command) AddToGroup(group string, s OptionSpec) *Option {
  350. ans, err := self.AddOptionGroup(group).AddOption(self, s)
  351. if err != nil {
  352. panic(err)
  353. }
  354. return ans
  355. }
  356. func (self *Command) AddOptionFromString(items ...string) *Option {
  357. return self.AddOptionToGroupFromString("", items...)
  358. }
  359. func (self *Command) Add(s OptionSpec) *Option {
  360. return self.AddToGroup("", s)
  361. }
  362. func (self *Command) FindOptions(name_with_hyphens string) []*Option {
  363. ans := make([]*Option, 0, 4)
  364. for _, g := range self.OptionGroups {
  365. x := g.FindOptions(name_with_hyphens)
  366. if x != nil {
  367. ans = append(ans, x...)
  368. }
  369. }
  370. depth := 0
  371. for p := self.Parent; p != nil; p = p.Parent {
  372. depth++
  373. for _, po := range p.FindOptions(name_with_hyphens) {
  374. if po.Depth >= depth {
  375. ans = append(ans, po)
  376. }
  377. }
  378. }
  379. return ans
  380. }
  381. func (self *Command) FindOption(name_with_hyphens string) *Option {
  382. for _, g := range self.OptionGroups {
  383. q := g.FindOption(name_with_hyphens)
  384. if q != nil {
  385. return q
  386. }
  387. }
  388. depth := 0
  389. for p := self.Parent; p != nil; p = p.Parent {
  390. depth++
  391. q := p.FindOption(name_with_hyphens)
  392. if q != nil && q.Depth >= depth {
  393. return q
  394. }
  395. }
  396. return nil
  397. }
  398. type Context struct {
  399. SeenCommands []*Command
  400. }
  401. func GetOptionValue[T any](self *Command, name string) (ans T, err error) {
  402. opt := self.option_map[name]
  403. if opt == nil {
  404. err = fmt.Errorf("No option with the name: %s", name)
  405. return
  406. }
  407. ans, ok := opt.parsed_value().(T)
  408. if !ok {
  409. err = fmt.Errorf("The option %s is not of the correct type", name)
  410. }
  411. return
  412. }
  413. func (self *Command) GetOptionValues(pointer_to_options_struct any) error {
  414. val := reflect.ValueOf(pointer_to_options_struct).Elem()
  415. if val.Kind() != reflect.Struct {
  416. return fmt.Errorf("Need a pointer to a struct to set option values on")
  417. }
  418. for i := 0; i < val.NumField(); i++ {
  419. f := val.Field(i)
  420. field_name := val.Type().Field(i).Name
  421. if utils.Capitalize(field_name) != field_name || !f.CanSet() {
  422. continue
  423. }
  424. opt := self.option_map[field_name]
  425. if opt == nil {
  426. return fmt.Errorf("No option with the name: %s", field_name)
  427. }
  428. switch opt.OptionType {
  429. case IntegerOption, CountOption:
  430. if f.Kind() != reflect.Int {
  431. return fmt.Errorf("The field: %s must be an integer", field_name)
  432. }
  433. v := int64(opt.parsed_value().(int))
  434. if f.OverflowInt(v) {
  435. return fmt.Errorf("The value: %d is too large for the integer type used for the option: %s", v, field_name)
  436. }
  437. f.SetInt(v)
  438. case FloatOption:
  439. if f.Kind() != reflect.Float64 {
  440. return fmt.Errorf("The field: %s must be a float64", field_name)
  441. }
  442. v := opt.parsed_value().(float64)
  443. if f.OverflowFloat(v) {
  444. return fmt.Errorf("The value: %f is too large for the integer type used for the option: %s", v, field_name)
  445. }
  446. f.SetFloat(v)
  447. case BoolOption:
  448. if f.Kind() != reflect.Bool {
  449. return fmt.Errorf("The field: %s must be a boolean", field_name)
  450. }
  451. v := opt.parsed_value().(bool)
  452. f.SetBool(v)
  453. case StringOption:
  454. if opt.IsList {
  455. if !is_string_slice(f) {
  456. return fmt.Errorf("The field: %s must be a []string", field_name)
  457. }
  458. v := opt.parsed_value().([]string)
  459. f.Set(reflect.ValueOf(v))
  460. } else {
  461. if f.Kind() != reflect.String {
  462. return fmt.Errorf("The field: %s must be a string", field_name)
  463. }
  464. v := opt.parsed_value().(string)
  465. f.SetString(v)
  466. }
  467. }
  468. }
  469. return nil
  470. }
  471. func (self *Command) ExecArgs(args []string) (exit_code int) {
  472. root := self
  473. for root.Parent != nil {
  474. root = root.Parent
  475. }
  476. cmd, err := root.ParseArgs(args)
  477. if err != nil {
  478. if self.CallbackOnError != nil {
  479. return self.CallbackOnError(cmd, err, true, 1)
  480. }
  481. ShowError(err)
  482. return 1
  483. }
  484. help_opt := cmd.option_map["Help"]
  485. version_opt := root.option_map["Version"]
  486. if help_opt != nil && help_opt.parsed_value().(bool) {
  487. cmd.ShowHelp()
  488. return
  489. } else if version_opt != nil && version_opt.parsed_value().(bool) {
  490. root.ShowVersion()
  491. return
  492. } else if cmd.Run != nil {
  493. exit_code, err = cmd.Run(cmd, cmd.Args)
  494. if err != nil {
  495. if exit_code == 0 {
  496. exit_code = 1
  497. }
  498. if self.CallbackOnError != nil {
  499. return self.CallbackOnError(cmd, err, false, exit_code)
  500. }
  501. ShowError(err)
  502. }
  503. }
  504. return
  505. }
  506. func (self *Command) Exec(args ...string) {
  507. if len(args) == 0 {
  508. args = os.Args
  509. }
  510. os.Exit(self.ExecArgs(args))
  511. }
  512. func (self *Command) GetCompletions(argv []string, init_completions func(*Completions)) *Completions {
  513. ans := NewCompletions()
  514. if init_completions != nil {
  515. init_completions(ans)
  516. }
  517. if len(argv) > 0 {
  518. exe := argv[0]
  519. exe = filepath.Base(exe) // zsh completion script passes full path to exe when using aliases
  520. cmd := self.FindSubCommand(exe)
  521. if cmd != nil {
  522. if cmd.ParseArgsForCompletion != nil {
  523. cmd.ParseArgsForCompletion(cmd, argv[1:], ans)
  524. } else {
  525. completion_parse_args(cmd, argv[1:], ans)
  526. }
  527. }
  528. }
  529. non_empty_groups := make([]*MatchGroup, 0, len(ans.Groups))
  530. for _, gr := range ans.Groups {
  531. if len(gr.Matches) > 0 {
  532. non_empty_groups = append(non_empty_groups, gr)
  533. }
  534. }
  535. ans.Groups = non_empty_groups
  536. return ans
  537. }