From 7c0892cba7847207d003fd8ba515bb600d0abf38 Mon Sep 17 00:00:00 2001 From: harveyshao Date: Mon, 23 May 2022 08:31:26 +0800 Subject: [PATCH] opt some --- exec.go | 2 +- misc.go | 25 ++++++++++++++++++++++++- type.go | 4 ++-- 3 files changed, 27 insertions(+), 4 deletions(-) diff --git a/exec.go b/exec.go index e7e66377..8f45cfd2 100644 --- a/exec.go +++ b/exec.go @@ -52,7 +52,7 @@ func (m *Message) Assert(expr interface{}) bool { panic(expr) } func (m *Message) Sleep(d string, arg ...interface{}) *Message { - m.Debug("sleep %s %s", d, kit.FileLine(2, 3)) + // m.Debug("sleep %s %s", d, kit.FileLine(2, 3)) if time.Sleep(kit.Duration(d)); len(arg) > 0 { m.Cmdy(arg...) } diff --git a/misc.go b/misc.go index 0f316227..b99c32d7 100644 --- a/misc.go +++ b/misc.go @@ -280,7 +280,7 @@ func (c *Context) _cmd(m *Message, cmd *Command, key string, sub string, h *Acti h.Hand(m, arg...) return m } -func (c *Context) split(name string) (list []interface{}) { +func SplitCmd(name string) (list []interface{}) { const ( TEXT = "text" ARGS = "args" @@ -343,6 +343,29 @@ func (c *Context) split(name string) (list []interface{}) { } return list } +func (m *Message) Design(action interface{}, help string, input ...interface{}) { + list := kit.List() + for _, input := range input { + switch input := input.(type) { + case string: + list = append(list, SplitCmd("action "+input)...) + case map[string]interface{}: + if kit.Format(input[TYPE]) != "" && kit.Format(input[NAME]) != "" { + list = append(list, input) + continue + } + kit.Fetch(kit.KeyValue(nil, "", input), func(k string, v interface{}) { + list = append(list, kit.Dict(NAME, k, TYPE, "text", VALUE, v)) + }) + } + } + k := kit.Format(action) + if a, ok := m._cmd.Action[k]; ok { + a.List = list + m._cmd.Meta[k] = list + kit.Value(m._cmd.Meta, kit.Keys("_trans", k), help) + } +} func MergeAction(list ...interface{}) map[string]*Action { if len(list) == 0 { diff --git a/type.go b/type.go index ce7946bf..4f443573 100644 --- a/type.go +++ b/type.go @@ -145,7 +145,7 @@ func (c *Context) Merge(s *Context) *Context { } if c.Commands[key] = cmd; cmd.List == nil { - cmd.List = c.split(cmd.Name) + cmd.List = SplitCmd(cmd.Name) } if cmd.Meta == nil { cmd.Meta = kit.Dict() @@ -183,7 +183,7 @@ func (c *Context) Merge(s *Context) *Context { continue // alias cmd } if a.List == nil { - a.List = c.split(a.Name) + a.List = SplitCmd(a.Name) } if len(a.List) > 0 { cmd.Meta[k] = a.List