From a32a8d096d5baad79393528ef315420cf21602f6 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Sat, 17 Sep 2022 12:41:10 +0530 Subject: [PATCH] Allow commands to customize argument parsing --- tools/completion/parse-args.go | 2 +- tools/completion/types.go | 8 +++++++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/tools/completion/parse-args.go b/tools/completion/parse-args.go index e570378cf..9e142770e 100644 --- a/tools/completion/parse-args.go +++ b/tools/completion/parse-args.go @@ -124,7 +124,7 @@ func complete_word(word string, completions *Completions, only_args_allowed bool return } -func (cmd *Command) parse_args(words []string, completions *Completions) { +func default_parse_args(cmd *Command, words []string, completions *Completions) { completions.current_cmd = cmd if len(words) == 0 { complete_word("", completions, false, nil, 0) diff --git a/tools/completion/types.go b/tools/completion/types.go index bc4316061..141471687 100644 --- a/tools/completion/types.go +++ b/tools/completion/types.go @@ -80,6 +80,8 @@ type Command struct { Stop_processing_at_arg int First_arg_may_not_be_subcommand bool Subcommand_must_be_first bool + + Parse_args func(*Command, []string, *Completions) } func (self *Command) clone_options_from(other *Command) { @@ -157,7 +159,11 @@ func (self *Command) GetCompletions(argv []string) *Completions { exe := argv[0] cmd := self.find_subcommand_with_name(exe) 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))