github.com/scaleway/scaleway-cli@v1.11.1/pkg/cli/cmd_help.go (about)

     1  // Copyright (C) 2015 Scaleway. All rights reserved.
     2  // Use of this source code is governed by a MIT-style
     3  // license that can be found in the LICENSE.md file.
     4  
     5  package cli
     6  
     7  import (
     8  	"fmt"
     9  	"text/template"
    10  )
    11  
    12  // CmdHelp is the 'scw help' command
    13  var CmdHelp = &Command{
    14  	Exec:        nil,
    15  	UsageLine:   "help [COMMAND]",
    16  	Description: "help of the scw command line",
    17  	Help: `
    18  Help prints help information about scw and its commands.
    19  
    20  By default, help lists available commands with a short description.
    21  When invoked with a command name, it prints the usage and the help of
    22  the command.
    23  `,
    24  }
    25  
    26  func init() {
    27  	// break dependency loop
    28  	CmdHelp.Exec = runHelp
    29  	CmdHelp.Flag.BoolVar(&helpHelp, []string{"h", "-help"}, false, "Print usage")
    30  }
    31  
    32  // Flags
    33  var helpHelp bool // -h, --help flag
    34  
    35  var helpTemplate = `Usage: scw [OPTIONS] COMMAND [arg...]
    36  
    37  Interact with Scaleway from the command line.
    38  
    39  Options:
    40   -h, --help=false             Print usage
    41   -D, --debug=false            Enable debug mode
    42   -V, --verbose=false          Enable verbose mode
    43   -q, --quiet=false            Enable quiet mode
    44   --sensitive=false            Show sensitive data in outputs, i.e. API Token/Organization
    45   -v, --version=false          Print version information and quit
    46   --region=par1                Change the default region (e.g. ams1)
    47  
    48  Commands:
    49  {{range .}}{{if not .Hidden}}    {{.Name | printf "%-9s"}} {{.Description}}
    50  {{end}}{{end}}
    51  Run 'scw COMMAND --help' for more information on a command.
    52  `
    53  
    54  func runHelp(cmd *Command, rawArgs []string) error {
    55  	if waitHelp {
    56  		return cmd.PrintUsage()
    57  	}
    58  	if len(rawArgs) > 1 {
    59  		return cmd.PrintShortUsage()
    60  	}
    61  
    62  	if len(rawArgs) == 1 {
    63  		name := rawArgs[0]
    64  		for _, command := range Commands {
    65  			if command.Name() == name {
    66  				return command.PrintUsage()
    67  			}
    68  		}
    69  		return fmt.Errorf("Unknown help topic `%s`.  Run 'scw help'.", name)
    70  	}
    71  	t := template.New("top")
    72  	template.Must(t.Parse(helpTemplate))
    73  	ctx := cmd.GetContext(rawArgs)
    74  	return t.Execute(ctx.Stdout, Commands)
    75  }