Skip to content

Commit

Permalink
help
Browse files Browse the repository at this point in the history
  • Loading branch information
pinguo-yangbing committed Oct 22, 2020
1 parent 3d51597 commit 9792c3e
Show file tree
Hide file tree
Showing 7 changed files with 425 additions and 34 deletions.
5 changes: 5 additions & 0 deletions application.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,11 @@ func (app *Application) Mode() string {
return app.mode
}

// CmdMode
func (app *Application) CmdMode() bool {
return app.mode == ModeCmd
}

// Env running env
func (app *Application) Env() string {
return app.env
Expand Down
11 changes: 11 additions & 0 deletions container.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,17 @@ func (c *Container) GetType(name string) reflect.Type {
panic("Container: class not found, " + name)
}

// getNew get new class object
func (c *Container) getNew(name string) reflect.Value{
item, ok := c.items[name]
if !ok {
panic("Container: class not found, " + name)
}

// get new object from pool
return item.pool.Get().(reflect.Value)
}

// Get get new class object. name is class name, config is properties map,
// params is optional construct parameters.
func (c *Container) Get(name string, ctx iface.IContext, params ...interface{}) reflect.Value {
Expand Down
6 changes: 3 additions & 3 deletions controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -250,8 +250,8 @@ func (c *Controller) Error(status int, message string) {
c.Json(EmptyObject, status, message)
}

// SetActionDesc
// Deprecated: Delete the next version directly
func (c *Controller) SetActionDesc(message string) {
if handler, has := App().Router().cmdHandlers[c.Context().Path()]; has {
handler.SetDesc(message)
}

}
78 changes: 52 additions & 26 deletions init.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ var (
"env": {Name: "env", Usage: "set running env (optional), eg. --env=online"},
"cmd": {Name: "cmd", Usage: "set running cmd (optional), eg. --cmd=/foo/bar"},
"base": {Name: "base", Usage: "set base path (optional), eg. --base=/base/path"},
"help": {Name: "help",DefValue:"1", Usage: "Displays a list of CMD controllers used (optional), eg. --help=1"},
"help": {Name: "help", Usage: "Displays a list of CMD controllers used (optional), eg. --help=1"},
}
)

Expand Down Expand Up @@ -115,8 +115,24 @@ func GetAlias(alias string) string {
}

func cmdList(path string) {
startTime := float64(time.Now().UnixNano()/1e6)
defer GLogger().Info(fmt.Sprintf("exec cmdList cost:%f ms",float64(time.Now().UnixNano()/1e6)-startTime))
list := App().Router().CmdHandlers()

showText:= func(name,nameType,DefValue,usage string ) string{
s := fmt.Sprintf(" \t --%s", name)
if len(nameType) > 0 {
s += " " + nameType
s += " \t"
}
s += strings.ReplaceAll(usage, "\n", "\n \t")
if DefValue != "" {
s += fmt.Sprintf(" (default %v)", DefValue)
}

return s
}

flagParams := func(global bool) string {
retStr := ""
flag.VisitAll(func(vFlag *flag.Flag) {
Expand All @@ -128,14 +144,9 @@ func cmdList(path string) {
if !global && hasGP {
return
}
s := fmt.Sprintf(" \t --%s", vFlag.Name) // Two spaces before -; see next two comments.
name, usage := flag.UnquoteUsage(vFlag)
if len(name) > 0 {
s += " " + name
}

s += " \t"
s += strings.ReplaceAll(usage, "\n", "\n \t")
nameType, usage := flag.UnquoteUsage(vFlag)

isZeroValue := func(vFlag *flag.Flag, value string) bool {
// Build a zero value of the flag's Value type, and see if the
// result of calling its String method equals the value passed in.
Expand All @@ -149,9 +160,11 @@ func cmdList(path string) {
}
return value == z.Interface().(flag.Value).String()
}
defValue := ""
if !isZeroValue(vFlag, vFlag.DefValue) {
s += fmt.Sprintf(" (default %v)", vFlag.DefValue)
defValue = vFlag.DefValue
}
s := showText(vFlag.Name,nameType,defValue, usage)
retStr += s + "\n"

})
Expand All @@ -161,44 +174,57 @@ func cmdList(path string) {

fmt.Println("Global parameters:\n " + flagParams(true))

fmt.Println("The path list:")
showParams := func(path string) string {
fmt.Println("The --cmd path list:")
actionInfo := func(handler *Handler) (actionDesc, paramsMsg string) {
defer func() {
if err := recover(); err != nil {
paramsMsg = path + ":不能解析参数,err:" + util.ToString(err)
}
}()
ctx := &Context{}
ctx.setPath(path)
rv := App().Container().getNew(GetAlias(handler.cPath))

rv, _, _ := App().Router().CreateController(path, ctx)
if !rv.IsValid() {
return ""
return
}

name := ParamsFlagMethodPrefix + ctx.ActionId()
methodV := rv.MethodByName(name)
if !methodV.IsValid() {
return ""
methodT := rv.Type().Method(handler.aId)

actionInfo := NewParser().GetActionInfo(rv.Type().Elem().PkgPath(),rv.Type().Elem().Name(),methodT.Name)

if actionInfo == nil {
return
}
actionDesc = actionInfo.Desc
if actionInfo.ParamsDesc == nil {
return
}

methodV.Call(nil)
return flagParams(false)

for _,v:= range actionInfo.ParamsDesc{
paramsMsg = paramsMsg + showText(v.Name,v.NameType,v.DftValue,v.Usage) + "\n"
}

return

}

if path != "" {
path = strings.ToLower(util.CleanPath(path))
}

for uri, v := range list {
// fmt.Println("path", path,"uri",uri)

for uri, handler := range list {
if path != "" && path != strings.ToLower(uri) {
continue
}

paramsStr:= showParams(uri)
fmt.Println(" --cmd=" + uri + " \t" + v.desc)
if paramsStr != "" {
actionDesc,paramsStr := actionInfo(handler)
fmt.Println(" --cmd=" + uri + " \t" + actionDesc)
if App().CmdMode() && paramsStr != "" {
fmt.Println(paramsStr)
}


}
fmt.Println("")
fmt.Println("")
Expand Down
Loading

0 comments on commit 9792c3e

Please sign in to comment.