aboutsummaryrefslogtreecommitdiffstats
path: root/commands/patch/patch.go
diff options
context:
space:
mode:
Diffstat (limited to 'commands/patch/patch.go')
-rw-r--r--commands/patch/patch.go51
1 files changed, 30 insertions, 21 deletions
diff --git a/commands/patch/patch.go b/commands/patch/patch.go
index 0807c1fa..25d7850a 100644
--- a/commands/patch/patch.go
+++ b/commands/patch/patch.go
@@ -8,32 +8,31 @@ import (
"git.sr.ht/~rjarry/go-opt"
)
-var (
- PatchCommands *commands.Commands
- subCommands *commands.Commands
-)
+var subCommands map[string]commands.Command
func register(cmd commands.Command) {
if subCommands == nil {
- subCommands = commands.NewCommands()
+ subCommands = make(map[string]commands.Command)
+ }
+ for _, alias := range cmd.Aliases() {
+ if subCommands[alias] != nil {
+ panic("duplicate sub command alias: " + alias)
+ }
+ subCommands[alias] = cmd
}
- subCommands.Register(cmd)
}
-func registerPatch(cmd commands.Command) {
- if PatchCommands == nil {
- PatchCommands = commands.NewCommands()
- }
- PatchCommands.Register(cmd)
+type Patch struct {
+ SubCmd commands.Command `opt:"command" action:"ParseSub" complete:"CompleteSubNames"`
+ Args string `opt:"..." required:"false" complete:"CompleteSubArgs"`
}
func init() {
- registerPatch(Patch{})
+ commands.Register(Patch{})
}
-type Patch struct {
- SubCmd commands.Command `opt:"command" action:"ParseSub" complete:"CompleteSubNames"`
- Args string `opt:"..." required:"false" complete:"CompleteSubArgs"`
+func (Patch) Context() commands.CommandContext {
+ return commands.GLOBAL
}
func (Patch) Aliases() []string {
@@ -41,16 +40,26 @@ func (Patch) Aliases() []string {
}
func (p *Patch) ParseSub(arg string) error {
- p.SubCmd = subCommands.ByName(arg)
- if p.SubCmd == nil {
- return fmt.Errorf("%s unknown sub-command", arg)
+ cmd, ok := subCommands[arg]
+ if ok {
+ context := commands.CurrentContext()
+ if cmd.Context()&context != 0 {
+ p.SubCmd = cmd
+ return nil
+ }
}
- return nil
+ return fmt.Errorf("%s unknown sub-command", arg)
}
func (*Patch) CompleteSubNames(arg string) []string {
- options := subCommands.Names()
- return commands.FilterList(options, arg, nil)
+ context := commands.CurrentContext()
+ options := make([]string, 0, len(subCommands))
+ for alias, cmd := range subCommands {
+ if cmd.Context()&context != 0 {
+ options = append(options, alias)
+ }
+ }
+ return commands.FilterList(options, arg, commands.QuoteSpace)
}
func (p *Patch) CompleteSubArgs(arg string) []string {