arg/usage.go

114 lines
2.5 KiB
Go
Raw Normal View History

2015-10-31 20:32:20 -05:00
package arg
import (
"fmt"
"io"
"os"
2015-11-01 01:57:26 -05:00
"path/filepath"
2015-10-31 20:32:20 -05:00
"reflect"
"strings"
)
2015-11-01 01:57:26 -05:00
// Usage prints usage information to stdout and exits with status zero
2015-10-31 20:32:20 -05:00
func Usage(dest ...interface{}) {
if err := WriteUsage(os.Stdout, dest...); err != nil {
fmt.Println(err)
}
os.Exit(0)
}
// Fail prints usage information to stdout and exits with non-zero status
func Fail(msg string, dest ...interface{}) {
fmt.Println(msg)
if err := WriteUsage(os.Stdout, dest...); err != nil {
fmt.Println(err)
}
os.Exit(1)
}
// WriteUsage writes usage information to the given writer
func WriteUsage(w io.Writer, dest ...interface{}) error {
spec, err := extractSpec(dest...)
if err != nil {
return err
}
2015-11-01 01:57:26 -05:00
writeUsage(w, filepath.Base(os.Args[0]), spec)
2015-10-31 20:32:20 -05:00
return nil
}
// writeUsage writes usage information to the given writer
2015-11-01 01:57:26 -05:00
func writeUsage(w io.Writer, cmd string, specs []*spec) {
2015-10-31 20:32:20 -05:00
var positionals, options []*spec
for _, spec := range specs {
if spec.positional {
positionals = append(positionals, spec)
} else {
options = append(options, spec)
}
}
2015-11-01 01:57:26 -05:00
fmt.Fprint(w, "usage: %s ", cmd)
2015-10-31 20:32:20 -05:00
// write the option component of the one-line usage message
for _, spec := range options {
if !spec.required {
fmt.Fprint(w, "[")
}
fmt.Fprint(w, synopsis(spec, "--"+spec.long))
if !spec.required {
fmt.Fprint(w, "]")
}
fmt.Fprint(w, " ")
}
// write the positional component of the one-line usage message
for _, spec := range positionals {
up := strings.ToUpper(spec.long)
if spec.multiple {
2015-10-31 20:48:38 -05:00
fmt.Fprintf(w, "[%s [%s ...]]", up, up)
2015-10-31 20:32:20 -05:00
} else {
fmt.Fprint(w, up)
}
fmt.Fprint(w, " ")
}
fmt.Fprint(w, "\n")
// write the list of positionals
if len(positionals) > 0 {
fmt.Fprint(w, "\npositional arguments:\n")
for _, spec := range positionals {
fmt.Fprintf(w, " %s\n", spec.long)
}
}
// write the list of options
if len(options) > 0 {
fmt.Fprint(w, "\noptions:\n")
const colWidth = 25
for _, spec := range options {
left := fmt.Sprint(synopsis(spec, "--"+spec.long))
if spec.short != "" {
left += ", " + fmt.Sprint(synopsis(spec, "-"+spec.short))
}
fmt.Print(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-11-01 01:57:26 -05:00
func synopsis(spec *spec, form string) string {
if spec.dest.Kind() == reflect.Bool {
return form
} else {
return form + " " + strings.ToUpper(spec.long)
}
}