ab5cf7e6ef
* Add tracing to the parser. * Synthesize a --help flag. * Parsing now occurs in multiple phases. 1. Reset target. 2. Parse command-line into a "trace" (no values are written to target). 3. Apply traced, parsed values to the target fields. This is another step in facilitating context-sensitive help and completion. * Detect duplicate flags.
34 lines
555 B
Go
34 lines
555 B
Go
package kong
|
|
|
|
import (
|
|
"io"
|
|
"text/template"
|
|
)
|
|
|
|
const defaultHelp = `{{- with .Application -}}
|
|
usage: {{.Name}}
|
|
|
|
{{.Help}}
|
|
{{range .Flags}}
|
|
--{{.Name}}
|
|
{{end}}
|
|
|
|
{{- end -}}
|
|
`
|
|
|
|
var defaultHelpTemplate = template.Must(template.New("help").Parse(defaultHelp))
|
|
|
|
// WriteHelp to w. If w is nil, the default stdout writer will be used.
|
|
func (k *Kong) WriteHelp(w io.Writer) error {
|
|
if w == nil {
|
|
w = k.stdout
|
|
}
|
|
ctx := map[string]interface{}{
|
|
"Application": k.Model,
|
|
}
|
|
for k, v := range k.helpContext {
|
|
ctx[k] = v
|
|
}
|
|
return k.help.Execute(w, ctx)
|
|
}
|