github.com/insionng/yougam@v0.0.0-20170714101924-2bc18d833463/libraries/cli/help.go (about)

     1  package cli
     2  
     3  import (
     4  	"fmt"
     5  	"io"
     6  	"strings"
     7  	"text/tabwriter"
     8  	"text/template"
     9  )
    10  
    11  // The text template for the Default help topic.
    12  // cli.go uses text/template to render templates. You can
    13  // render custom help text by setting this variable.
    14  var AppHelpTemplate = `NAME:
    15     {{.Name}} - {{.Usage}}
    16  
    17  USAGE:
    18     {{if .UsageText}}{{.UsageText}}{{else}}{{.HelpName}} {{if .Flags}}[global options]{{end}}{{if .Commands}} command [command options]{{end}} {{if .ArgsUsage}}{{.ArgsUsage}}{{else}}[arguments...]{{end}}{{end}}
    19     {{if .Version}}{{if not .HideVersion}}
    20  VERSION:
    21     {{.Version}}
    22     {{end}}{{end}}{{if len .Authors}}
    23  AUTHOR(S):
    24     {{range .Authors}}{{ . }}{{end}}
    25     {{end}}{{if .Commands}}
    26  COMMANDS:{{range .Categories}}{{if .Name}}
    27    {{.Name}}{{ ":" }}{{end}}{{range .Commands}}
    28      {{.Name}}{{with .ShortName}}, {{.}}{{end}}{{ "\t" }}{{.Usage}}{{end}}
    29  {{end}}{{end}}{{if .Flags}}
    30  GLOBAL OPTIONS:
    31     {{range .Flags}}{{.}}
    32     {{end}}{{end}}{{if .Copyright }}
    33  COPYRIGHT:
    34     {{.Copyright}}
    35     {{end}}
    36  `
    37  
    38  // The text template for the command help topic.
    39  // cli.go uses text/template to render templates. You can
    40  // render custom help text by setting this variable.
    41  var CommandHelpTemplate = `NAME:
    42     {{.HelpName}} - {{.Usage}}
    43  
    44  USAGE:
    45     {{.HelpName}}{{if .Flags}} [command options]{{end}} {{if .ArgsUsage}}{{.ArgsUsage}}{{else}}[arguments...]{{end}}{{if .Category}}
    46  
    47  CATEGORY:
    48     {{.Category}}{{end}}{{if .Description}}
    49  
    50  DESCRIPTION:
    51     {{.Description}}{{end}}{{if .Flags}}
    52  
    53  OPTIONS:
    54     {{range .Flags}}{{.}}
    55     {{end}}{{ end }}
    56  `
    57  
    58  // The text template for the subcommand help topic.
    59  // cli.go uses text/template to render templates. You can
    60  // render custom help text by setting this variable.
    61  var SubcommandHelpTemplate = `NAME:
    62     {{.HelpName}} - {{.Usage}}
    63  
    64  USAGE:
    65     {{.HelpName}} command{{if .Flags}} [command options]{{end}} {{if .ArgsUsage}}{{.ArgsUsage}}{{else}}[arguments...]{{end}}
    66  
    67  COMMANDS:{{range .Categories}}{{if .Name}}
    68    {{.Name}}{{ ":" }}{{end}}{{range .Commands}}
    69      {{.Name}}{{with .ShortName}}, {{.}}{{end}}{{ "\t" }}{{.Usage}}{{end}}
    70  {{end}}{{if .Flags}}
    71  OPTIONS:
    72     {{range .Flags}}{{.}}
    73     {{end}}{{end}}
    74  `
    75  
    76  var helpCommand = Command{
    77  	Name:      "help",
    78  	Aliases:   []string{"h"},
    79  	Usage:     "Shows a list of commands or help for one command",
    80  	ArgsUsage: "[command]",
    81  	Action: func(c *Context) {
    82  		args := c.Args()
    83  		if args.Present() {
    84  			ShowCommandHelp(c, args.First())
    85  		} else {
    86  			ShowAppHelp(c)
    87  		}
    88  	},
    89  }
    90  
    91  var helpSubcommand = Command{
    92  	Name:      "help",
    93  	Aliases:   []string{"h"},
    94  	Usage:     "Shows a list of commands or help for one command",
    95  	ArgsUsage: "[command]",
    96  	Action: func(c *Context) {
    97  		args := c.Args()
    98  		if args.Present() {
    99  			ShowCommandHelp(c, args.First())
   100  		} else {
   101  			ShowSubcommandHelp(c)
   102  		}
   103  	},
   104  }
   105  
   106  // Prints help for the App or Command
   107  type helpPrinter func(w io.Writer, templ string, data interface{})
   108  
   109  var HelpPrinter helpPrinter = printHelp
   110  
   111  // Prints version for the App
   112  var VersionPrinter = printVersion
   113  
   114  func ShowAppHelp(c *Context) {
   115  	HelpPrinter(c.App.Writer, AppHelpTemplate, c.App)
   116  }
   117  
   118  // Prints the list of subcommands as the default app completion method
   119  func DefaultAppComplete(c *Context) {
   120  	for _, command := range c.App.Commands {
   121  		for _, name := range command.Names() {
   122  			fmt.Fprintln(c.App.Writer, name)
   123  		}
   124  	}
   125  }
   126  
   127  // Prints help for the given command
   128  func ShowCommandHelp(ctx *Context, command string) {
   129  	// show the subcommand help for a command with subcommands
   130  	if command == "" {
   131  		HelpPrinter(ctx.App.Writer, SubcommandHelpTemplate, ctx.App)
   132  		return
   133  	}
   134  
   135  	for _, c := range ctx.App.Commands {
   136  		if c.HasName(command) {
   137  			HelpPrinter(ctx.App.Writer, CommandHelpTemplate, c)
   138  			return
   139  		}
   140  	}
   141  
   142  	if ctx.App.CommandNotFound != nil {
   143  		ctx.App.CommandNotFound(ctx, command)
   144  	} else {
   145  		fmt.Fprintf(ctx.App.Writer, "No help topic for '%v'\n", command)
   146  	}
   147  }
   148  
   149  // Prints help for the given subcommand
   150  func ShowSubcommandHelp(c *Context) {
   151  	ShowCommandHelp(c, c.Command.Name)
   152  }
   153  
   154  // Prints the version number of the App
   155  func ShowVersion(c *Context) {
   156  	VersionPrinter(c)
   157  }
   158  
   159  func printVersion(c *Context) {
   160  	fmt.Fprintf(c.App.Writer, "%v version %v\n", c.App.Name, c.App.Version)
   161  }
   162  
   163  // Prints the lists of commands within a given context
   164  func ShowCompletions(c *Context) {
   165  	a := c.App
   166  	if a != nil && a.BashComplete != nil {
   167  		a.BashComplete(c)
   168  	}
   169  }
   170  
   171  // Prints the custom completions for a given command
   172  func ShowCommandCompletions(ctx *Context, command string) {
   173  	c := ctx.App.Command(command)
   174  	if c != nil && c.BashComplete != nil {
   175  		c.BashComplete(ctx)
   176  	}
   177  }
   178  
   179  func printHelp(out io.Writer, templ string, data interface{}) {
   180  	funcMap := template.FuncMap{
   181  		"join": strings.Join,
   182  	}
   183  
   184  	w := tabwriter.NewWriter(out, 0, 8, 1, '\t', 0)
   185  	t := template.Must(template.New("help").Funcs(funcMap).Parse(templ))
   186  	err := t.Execute(w, data)
   187  	if err != nil {
   188  		// If the writer is closed, t.Execute will fail, and there's nothing
   189  		// we can do to recover. We could send this to os.Stderr if we need.
   190  		return
   191  	}
   192  	w.Flush()
   193  }
   194  
   195  func checkVersion(c *Context) bool {
   196  	found := false
   197  	if VersionFlag.Name != "" {
   198  		eachName(VersionFlag.Name, func(name string) {
   199  			if c.GlobalBool(name) || c.Bool(name) {
   200  				found = true
   201  			}
   202  		})
   203  	}
   204  	return found
   205  }
   206  
   207  func checkHelp(c *Context) bool {
   208  	found := false
   209  	if HelpFlag.Name != "" {
   210  		eachName(HelpFlag.Name, func(name string) {
   211  			if c.GlobalBool(name) || c.Bool(name) {
   212  				found = true
   213  			}
   214  		})
   215  	}
   216  	return found
   217  }
   218  
   219  func checkCommandHelp(c *Context, name string) bool {
   220  	if c.Bool("h") || c.Bool("help") {
   221  		ShowCommandHelp(c, name)
   222  		return true
   223  	}
   224  
   225  	return false
   226  }
   227  
   228  func checkSubcommandHelp(c *Context) bool {
   229  	if c.GlobalBool("h") || c.GlobalBool("help") {
   230  		ShowSubcommandHelp(c)
   231  		return true
   232  	}
   233  
   234  	return false
   235  }
   236  
   237  func checkCompletions(c *Context) bool {
   238  	if (c.GlobalBool(BashCompletionFlag.Name) || c.Bool(BashCompletionFlag.Name)) && c.App.EnableBashCompletion {
   239  		ShowCompletions(c)
   240  		return true
   241  	}
   242  
   243  	return false
   244  }
   245  
   246  func checkCommandCompletions(c *Context, name string) bool {
   247  	if c.Bool(BashCompletionFlag.Name) && c.App.EnableBashCompletion {
   248  		ShowCommandCompletions(c, name)
   249  		return true
   250  	}
   251  
   252  	return false
   253  }