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