2015-10-31 20:32:20 -05:00
|
|
|
package arg
|
|
|
|
|
|
|
|
import (
|
2019-04-14 20:00:40 -05:00
|
|
|
"encoding"
|
2015-10-31 20:32:20 -05:00
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"os"
|
2015-11-21 17:58:27 -06:00
|
|
|
"reflect"
|
2015-10-31 20:32:20 -05:00
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2016-01-18 10:24:21 -06:00
|
|
|
// the width of the left column
|
|
|
|
const colWidth = 25
|
|
|
|
|
2015-11-09 03:27:15 -06:00
|
|
|
// Fail prints usage information to stderr and exits with non-zero status
|
2015-11-01 01:13:23 -06:00
|
|
|
func (p *Parser) Fail(msg string) {
|
2015-11-09 03:27:15 -06:00
|
|
|
p.WriteUsage(os.Stderr)
|
2015-11-11 03:29:01 -06:00
|
|
|
fmt.Fprintln(os.Stderr, "error:", msg)
|
2015-11-01 15:53:51 -06:00
|
|
|
os.Exit(-1)
|
2015-10-31 20:32:20 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// WriteUsage writes usage information to the given writer
|
2015-11-01 01:13:23 -06:00
|
|
|
func (p *Parser) WriteUsage(w io.Writer) {
|
2015-10-31 20:32:20 -05:00
|
|
|
var positionals, options []*spec
|
2019-04-14 21:50:17 -05:00
|
|
|
for _, spec := range p.cmd.specs {
|
2015-10-31 20:32:20 -05:00
|
|
|
if spec.positional {
|
|
|
|
positionals = append(positionals, spec)
|
|
|
|
} else {
|
|
|
|
options = append(options, spec)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-08 23:18:19 -05:00
|
|
|
if p.version != "" {
|
|
|
|
fmt.Fprintln(w, p.version)
|
|
|
|
}
|
|
|
|
|
2019-04-14 21:50:17 -05:00
|
|
|
fmt.Fprintf(w, "Usage: %s", p.cmd.name)
|
2015-10-31 20:32:20 -05:00
|
|
|
|
2015-11-01 15:53:51 -06:00
|
|
|
// write the option component of the usage message
|
2015-10-31 20:32:20 -05:00
|
|
|
for _, spec := range options {
|
2015-11-07 08:39:23 -06:00
|
|
|
// prefix with a space
|
|
|
|
fmt.Fprint(w, " ")
|
2015-10-31 20:32:20 -05:00
|
|
|
if !spec.required {
|
|
|
|
fmt.Fprint(w, "[")
|
|
|
|
}
|
|
|
|
fmt.Fprint(w, synopsis(spec, "--"+spec.long))
|
|
|
|
if !spec.required {
|
|
|
|
fmt.Fprint(w, "]")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-01 15:53:51 -06:00
|
|
|
// write the positional component of the usage message
|
2015-10-31 20:32:20 -05:00
|
|
|
for _, spec := range positionals {
|
2015-11-07 08:39:23 -06:00
|
|
|
// prefix with a space
|
|
|
|
fmt.Fprint(w, " ")
|
2015-10-31 20:32:20 -05:00
|
|
|
up := strings.ToUpper(spec.long)
|
|
|
|
if spec.multiple {
|
2017-03-30 13:32:39 -05:00
|
|
|
if !spec.required {
|
|
|
|
fmt.Fprint(w, "[")
|
|
|
|
}
|
|
|
|
fmt.Fprintf(w, "%s [%s ...]", up, up)
|
|
|
|
if !spec.required {
|
|
|
|
fmt.Fprint(w, "]")
|
|
|
|
}
|
2015-10-31 20:32:20 -05:00
|
|
|
} else {
|
|
|
|
fmt.Fprint(w, up)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fmt.Fprint(w, "\n")
|
2015-11-01 15:53:51 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// WriteHelp writes the usage string followed by the full help string for each option
|
|
|
|
func (p *Parser) WriteHelp(w io.Writer) {
|
|
|
|
var positionals, options []*spec
|
2019-04-14 21:50:17 -05:00
|
|
|
for _, spec := range p.cmd.specs {
|
2015-11-01 15:53:51 -06:00
|
|
|
if spec.positional {
|
|
|
|
positionals = append(positionals, spec)
|
|
|
|
} else {
|
|
|
|
options = append(options, spec)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-08 10:41:07 -06:00
|
|
|
if p.description != "" {
|
|
|
|
fmt.Fprintln(w, p.description)
|
|
|
|
}
|
2015-11-01 15:53:51 -06:00
|
|
|
p.WriteUsage(w)
|
2015-10-31 20:32:20 -05:00
|
|
|
|
|
|
|
// write the list of positionals
|
|
|
|
if len(positionals) > 0 {
|
2017-03-08 13:44:01 -06:00
|
|
|
fmt.Fprint(w, "\nPositional arguments:\n")
|
2015-10-31 20:32:20 -05:00
|
|
|
for _, spec := range positionals {
|
2017-03-08 13:44:01 -06:00
|
|
|
left := " " + strings.ToUpper(spec.long)
|
2015-11-21 17:59:40 -06:00
|
|
|
fmt.Fprint(w, left)
|
|
|
|
if spec.help != "" {
|
|
|
|
if len(left)+2 < colWidth {
|
|
|
|
fmt.Fprint(w, strings.Repeat(" ", colWidth-len(left)))
|
|
|
|
} else {
|
|
|
|
fmt.Fprint(w, "\n"+strings.Repeat(" ", colWidth))
|
|
|
|
}
|
|
|
|
fmt.Fprint(w, spec.help)
|
|
|
|
}
|
|
|
|
fmt.Fprint(w, "\n")
|
2015-10-31 20:32:20 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-21 17:58:27 -06:00
|
|
|
// write the list of options
|
2017-03-08 13:44:01 -06:00
|
|
|
fmt.Fprint(w, "\nOptions:\n")
|
2015-11-11 03:15:57 -06:00
|
|
|
for _, spec := range options {
|
2019-04-14 21:50:17 -05:00
|
|
|
p.printOption(w, spec)
|
2015-11-11 03:15:57 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// write the list of built in options
|
2019-04-30 13:16:01 -05:00
|
|
|
p.printOption(w, &spec{
|
|
|
|
boolean: true,
|
|
|
|
long: "help",
|
|
|
|
short: "h",
|
|
|
|
help: "display this help and exit",
|
|
|
|
})
|
2016-09-08 23:18:19 -05:00
|
|
|
if p.version != "" {
|
2019-04-30 13:16:01 -05:00
|
|
|
p.printOption(w, &spec{
|
|
|
|
boolean: true,
|
|
|
|
long: "version",
|
|
|
|
help: "display version and exit",
|
|
|
|
})
|
2016-09-08 23:18:19 -05:00
|
|
|
}
|
2015-11-11 03:15:57 -06:00
|
|
|
}
|
|
|
|
|
2019-04-14 21:50:17 -05:00
|
|
|
func (p *Parser) printOption(w io.Writer, spec *spec) {
|
2015-11-11 03:15:57 -06:00
|
|
|
left := " " + synopsis(spec, "--"+spec.long)
|
|
|
|
if spec.short != "" {
|
|
|
|
left += ", " + synopsis(spec, "-"+spec.short)
|
|
|
|
}
|
|
|
|
fmt.Fprint(w, left)
|
|
|
|
if spec.help != "" {
|
|
|
|
if len(left)+2 < colWidth {
|
|
|
|
fmt.Fprint(w, strings.Repeat(" ", colWidth-len(left)))
|
|
|
|
} else {
|
|
|
|
fmt.Fprint(w, "\n"+strings.Repeat(" ", colWidth))
|
2015-10-31 20:32:20 -05:00
|
|
|
}
|
2015-11-11 03:15:57 -06:00
|
|
|
fmt.Fprint(w, spec.help)
|
2015-10-31 20:32:20 -05:00
|
|
|
}
|
2016-01-18 12:31:01 -06:00
|
|
|
// If spec.dest is not the zero value then a default value has been added.
|
2019-04-14 21:50:17 -05:00
|
|
|
var v reflect.Value
|
2019-04-30 15:30:23 -05:00
|
|
|
if len(spec.dest.fields) > 0 {
|
|
|
|
v = p.readable(spec.dest)
|
2019-04-14 21:50:17 -05:00
|
|
|
}
|
2015-11-21 17:58:27 -06:00
|
|
|
if v.IsValid() {
|
|
|
|
z := reflect.Zero(v.Type())
|
2016-03-06 14:07:01 -06:00
|
|
|
if (v.Type().Comparable() && z.Type().Comparable() && v.Interface() != z.Interface()) || v.Kind() == reflect.Slice && !v.IsNil() {
|
2018-04-12 23:46:24 -05:00
|
|
|
if scalar, ok := v.Interface().(encoding.TextMarshaler); ok {
|
|
|
|
if value, err := scalar.MarshalText(); err != nil {
|
|
|
|
fmt.Fprintf(w, " [default: error: %v]", err)
|
|
|
|
} else {
|
|
|
|
fmt.Fprintf(w, " [default: %v]", string(value))
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
fmt.Fprintf(w, " [default: %v]", v)
|
|
|
|
}
|
2015-11-21 17:58:27 -06:00
|
|
|
}
|
2015-11-19 07:34:09 -06:00
|
|
|
}
|
2015-11-11 03:15:57 -06:00
|
|
|
fmt.Fprint(w, "\n")
|
2015-10-31 20:32:20 -05:00
|
|
|
}
|
2015-11-01 01:57:26 -05:00
|
|
|
|
|
|
|
func synopsis(spec *spec, form string) string {
|
2016-01-23 22:49:57 -06:00
|
|
|
if spec.boolean {
|
2015-11-01 01:57:26 -05:00
|
|
|
return form
|
|
|
|
}
|
2015-11-11 07:08:28 -06:00
|
|
|
return form + " " + strings.ToUpper(spec.long)
|
2015-11-01 01:57:26 -05:00
|
|
|
}
|