Allow commands to customize argument parsing

This commit is contained in:
Kovid Goyal 2022-09-17 12:41:10 +05:30
parent 67f556bd7c
commit a32a8d096d
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
2 changed files with 8 additions and 2 deletions

View File

@ -124,7 +124,7 @@ func complete_word(word string, completions *Completions, only_args_allowed bool
return return
} }
func (cmd *Command) parse_args(words []string, completions *Completions) { func default_parse_args(cmd *Command, words []string, completions *Completions) {
completions.current_cmd = cmd completions.current_cmd = cmd
if len(words) == 0 { if len(words) == 0 {
complete_word("", completions, false, nil, 0) complete_word("", completions, false, nil, 0)

View File

@ -80,6 +80,8 @@ type Command struct {
Stop_processing_at_arg int Stop_processing_at_arg int
First_arg_may_not_be_subcommand bool First_arg_may_not_be_subcommand bool
Subcommand_must_be_first bool Subcommand_must_be_first bool
Parse_args func(*Command, []string, *Completions)
} }
func (self *Command) clone_options_from(other *Command) { func (self *Command) clone_options_from(other *Command) {
@ -157,7 +159,11 @@ func (self *Command) GetCompletions(argv []string) *Completions {
exe := argv[0] exe := argv[0]
cmd := self.find_subcommand_with_name(exe) cmd := self.find_subcommand_with_name(exe)
if cmd != nil { if cmd != nil {
cmd.parse_args(argv[1:], &ans) if cmd.Parse_args != nil {
cmd.Parse_args(cmd, argv[1:], &ans)
} else {
default_parse_args(cmd, argv[1:], &ans)
}
} }
} }
non_empty_groups := make([]*MatchGroup, 0, len(ans.Groups)) non_empty_groups := make([]*MatchGroup, 0, len(ans.Groups))