forked from google/skia-buildbot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
urfavecli.go
61 lines (53 loc) · 1.54 KB
/
urfavecli.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
// Package urfavecli contains utility functions for working with https://github.com/urfave/cli.
package urfavecli
import (
cli "github.com/urfave/cli/v2"
"go.skia.org/infra/go/sklog"
)
// LogFlags reflects all the flags and their values as Info logs.
//
// Should be called from within the Action of a Command after logging is setup.
//
// Example
//
// &cli.Command{
// Name: "my-command",
// Action: func(c *cli.Context) error {
// urfavecli.LogFlags(c)
// // Do command stuff.
// },
// },
func LogFlags(cliContext *cli.Context) {
for _, flag := range cliContext.App.Flags {
name := flag.Names()[0]
sklog.Infof("App Flags: --%s=%v", name, cliContext.Value(name))
}
for _, flag := range cliContext.Command.Flags {
name := flag.Names()[0]
sklog.Infof("Command Flags: --%s=%v", name, cliContext.Value(name))
}
}
// MarkdownDocTemplate is a common template used to format commands as Markdown.
const MarkdownDocTemplate = `# NAME
{{ .App.Name }}{{ if .App.Usage }} - {{ .App.Usage }}{{ end }}
# SYNOPSIS
{{ .App.Name }}
{{ if .SynopsisArgs }}
` + "```" + `
{{ range $v := .SynopsisArgs }}{{ $v }}{{ end }}` + "```" + `
{{ end }}{{ if .App.UsageText }}
# DESCRIPTION
{{ .App.UsageText }}
{{ end }}
**Usage**:
` + "```" + `
{{ .App.Name }} [GLOBAL OPTIONS] command [COMMAND OPTIONS] [ARGUMENTS...]
` + "```" + `
{{ if .GlobalArgs }}
# GLOBAL OPTIONS
{{ range $v := .GlobalArgs }}
{{ $v }}{{ end }}
{{ end }}{{ if .Commands }}
# COMMANDS
{{ range $v := .Commands }}
{{ $v }}{{ end }}{{ end }}`