mirror of
https://github.com/beego/bee.git
synced 2024-11-22 15:10:54 +00:00
Enhanced the usage and help output
Now each command has its own output destination, which can be changed using a SetOutput(writer) method. Added ability to display the options of each command.
This commit is contained in:
parent
a6ce37ddf3
commit
6cf7b5d518
102
bee.go
102
bee.go
@ -52,6 +52,9 @@ type Command struct {
|
|||||||
// CustomFlags indicates that the command will do its own
|
// CustomFlags indicates that the command will do its own
|
||||||
// flag parsing.
|
// flag parsing.
|
||||||
CustomFlags bool
|
CustomFlags bool
|
||||||
|
|
||||||
|
// output out writer if set in SetOutput(w)
|
||||||
|
output *io.Writer
|
||||||
}
|
}
|
||||||
|
|
||||||
// Name returns the command's name: the first word in the usage line.
|
// Name returns the command's name: the first word in the usage line.
|
||||||
@ -64,10 +67,24 @@ func (c *Command) Name() string {
|
|||||||
return name
|
return name
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// SetOutput sets the destination for usage and error messages.
|
||||||
|
// If output is nil, os.Stderr is used.
|
||||||
|
func (c *Command) SetOutput(output io.Writer) {
|
||||||
|
c.output = &output
|
||||||
|
}
|
||||||
|
|
||||||
|
// Out returns the out writer of the current command.
|
||||||
|
// If cmd.output is nil, os.Stderr is used.
|
||||||
|
func (c *Command) Out() io.Writer {
|
||||||
|
if c.output != nil {
|
||||||
|
return *c.output
|
||||||
|
}
|
||||||
|
return NewColorWriter(os.Stderr)
|
||||||
|
}
|
||||||
|
|
||||||
// Usage puts out the usage for the command.
|
// Usage puts out the usage for the command.
|
||||||
func (c *Command) Usage() {
|
func (c *Command) Usage() {
|
||||||
fmt.Fprintf(os.Stderr, "usage: %s\n\n", c.UsageLine)
|
tmpl(helpTemplate, c)
|
||||||
fmt.Fprintf(os.Stderr, "%s\n", strings.TrimSpace(string(c.Long)))
|
|
||||||
os.Exit(2)
|
os.Exit(2)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -77,7 +94,15 @@ func (c *Command) Runnable() bool {
|
|||||||
return c.Run != nil
|
return c.Run != nil
|
||||||
}
|
}
|
||||||
|
|
||||||
var commands = []*Command{
|
func (c *Command) Options() map[string]string {
|
||||||
|
options := make(map[string]string)
|
||||||
|
c.Flag.VisitAll(func(f *flag.Flag) {
|
||||||
|
options[f.Name] = f.Usage
|
||||||
|
})
|
||||||
|
return options
|
||||||
|
}
|
||||||
|
|
||||||
|
var availableCommands = []*Command{
|
||||||
cmdNew,
|
cmdNew,
|
||||||
cmdRun,
|
cmdRun,
|
||||||
cmdPack,
|
cmdPack,
|
||||||
@ -103,6 +128,7 @@ func main() {
|
|||||||
log.SetFlags(0)
|
log.SetFlags(0)
|
||||||
|
|
||||||
args := flag.Args()
|
args := flag.Args()
|
||||||
|
|
||||||
if len(args) < 1 {
|
if len(args) < 1 {
|
||||||
usage()
|
usage()
|
||||||
}
|
}
|
||||||
@ -112,7 +138,7 @@ func main() {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, cmd := range commands {
|
for _, cmd := range availableCommands {
|
||||||
if cmd.Name() == args[0] && cmd.Run != nil {
|
if cmd.Name() == args[0] && cmd.Run != nil {
|
||||||
cmd.Flag.Usage = func() { cmd.Usage() }
|
cmd.Flag.Usage = func() { cmd.Usage() }
|
||||||
if cmd.CustomFlags {
|
if cmd.CustomFlags {
|
||||||
@ -137,46 +163,57 @@ func main() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Fprintf(os.Stderr, "bee: unknown subcommand %q\nRun 'bee help' for usage.\n", args[0])
|
printErrorAndExit("Unknown subcommand")
|
||||||
os.Exit(2)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
var usageTemplate = `Bee is a tool for managing beego framework.
|
var usageTemplate = `Bee is a Fast and Flexible tool for managing your Beego Web Application.
|
||||||
|
|
||||||
Usage:
|
{{"USAGE" | headline}}
|
||||||
|
{{"bee command [arguments]" | bold}}
|
||||||
|
|
||||||
bee command [arguments]
|
{{"AVAILABLE COMMANDS" | headline}}
|
||||||
|
|
||||||
The commands are:
|
|
||||||
{{range .}}{{if .Runnable}}
|
{{range .}}{{if .Runnable}}
|
||||||
{{.Name | printf "%-11s"}} {{.Short}}{{end}}{{end}}
|
{{.Name | printf "%-11s" | bold}} {{.Short}}{{end}}{{end}}
|
||||||
|
|
||||||
Use "bee help [command]" for more information about a command.
|
Use {{"bee help [command]" | bold}} for more information about a command.
|
||||||
|
|
||||||
Additional help topics:
|
{{"ADDITIONAL HELP TOPICS" | headline}}
|
||||||
{{range .}}{{if not .Runnable}}
|
{{range .}}{{if not .Runnable}}
|
||||||
{{.Name | printf "%-11s"}} {{.Short}}{{end}}{{end}}
|
{{.Name | printf "%-11s"}} {{.Short}}{{end}}{{end}}
|
||||||
|
|
||||||
Use "bee help [topic]" for more information about that topic.
|
Use {{"bee help [topic]" | bold}} for more information about that topic.
|
||||||
`
|
`
|
||||||
|
|
||||||
var helpTemplate = `{{if .Runnable}}usage: bee {{.UsageLine}}
|
var helpTemplate = `{{"USAGE" | headline}}
|
||||||
|
{{.UsageLine | printf "bee %s" | bold}}
|
||||||
|
{{if .Options}}{{endline}}{{"OPTIONS" | headline}}{{range $k,$v := .Options}}
|
||||||
|
{{$k | printf "-%-11s" | bold}} {{$v}}{{end}}{{endline}}{{end}}
|
||||||
|
{{"DESCRIPTION" | headline}}
|
||||||
|
{{.Long | trim}}
|
||||||
|
`
|
||||||
|
|
||||||
{{end}}{{.Long | trim}}
|
var errorTemplate = `bee: %s.
|
||||||
|
Use {{"bee help" | bold}} for more information.
|
||||||
`
|
`
|
||||||
|
|
||||||
func usage() {
|
func usage() {
|
||||||
tmpl(os.Stdout, usageTemplate, commands)
|
tmpl(usageTemplate, availableCommands)
|
||||||
os.Exit(2)
|
os.Exit(2)
|
||||||
}
|
}
|
||||||
|
|
||||||
func tmpl(w io.Writer, text string, data interface{}) {
|
func tmpl(text string, data interface{}) {
|
||||||
|
output := NewColorWriter(os.Stderr)
|
||||||
|
|
||||||
t := template.New("top")
|
t := template.New("top")
|
||||||
t.Funcs(template.FuncMap{"trim": func(s template.HTML) template.HTML {
|
t.Funcs(template.FuncMap{
|
||||||
return template.HTML(strings.TrimSpace(string(s)))
|
"trim": func(s template.HTML) template.HTML { return template.HTML(strings.TrimSpace(string(s))) },
|
||||||
}})
|
"bold": bold,
|
||||||
|
"headline": MagentaBold,
|
||||||
|
"endline": EndLine,
|
||||||
|
})
|
||||||
|
|
||||||
template.Must(t.Parse(text))
|
template.Must(t.Parse(text))
|
||||||
if err := t.Execute(w, data); err != nil {
|
if err := t.Execute(output, data); err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -184,24 +221,23 @@ func tmpl(w io.Writer, text string, data interface{}) {
|
|||||||
func help(args []string) {
|
func help(args []string) {
|
||||||
if len(args) == 0 {
|
if len(args) == 0 {
|
||||||
usage()
|
usage()
|
||||||
// not exit 2: succeeded at 'go help'.
|
|
||||||
return
|
|
||||||
}
|
}
|
||||||
if len(args) != 1 {
|
if len(args) != 1 {
|
||||||
fmt.Fprintf(os.Stdout, "usage: bee help command\n\nToo many arguments given.\n")
|
printErrorAndExit("Too many arguments")
|
||||||
os.Exit(2) // failed at 'bee help'
|
|
||||||
}
|
}
|
||||||
|
|
||||||
arg := args[0]
|
arg := args[0]
|
||||||
|
|
||||||
for _, cmd := range commands {
|
for _, cmd := range availableCommands {
|
||||||
if cmd.Name() == arg {
|
if cmd.Name() == arg {
|
||||||
tmpl(os.Stdout, helpTemplate, cmd)
|
tmpl(helpTemplate, cmd)
|
||||||
// not exit 2: succeeded at 'go help cmd'.
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
printErrorAndExit("Unknown help topic")
|
||||||
fmt.Fprintf(os.Stdout, "Unknown help topic %#q. Run 'bee help'.\n", arg)
|
}
|
||||||
os.Exit(2) // failed at 'bee help cmd'
|
|
||||||
|
func printErrorAndExit(message string) {
|
||||||
|
tmpl(fmt.Sprintf(errorTemplate, message), nil)
|
||||||
|
os.Exit(2)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user