Merge pull request #13 from walle/show_builtin
Add built ins to options in help output
This commit is contained in:
commit
5db9c77fa3
6
parse.go
6
parse.go
|
@ -19,6 +19,7 @@ type spec struct {
|
||||||
positional bool
|
positional bool
|
||||||
help string
|
help string
|
||||||
wasPresent bool
|
wasPresent bool
|
||||||
|
isBool bool
|
||||||
}
|
}
|
||||||
|
|
||||||
// ErrHelp indicates that -h or --help were provided
|
// ErrHelp indicates that -h or --help were provided
|
||||||
|
@ -100,6 +101,11 @@ func NewParser(dests ...interface{}) (*Parser, error) {
|
||||||
return nil, fmt.Errorf("%s.%s: %s fields are not supported", t.Name(), field.Name, scalarType.Kind())
|
return nil, fmt.Errorf("%s.%s: %s fields are not supported", t.Name(), field.Name, scalarType.Kind())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Specify that it is a bool for usage
|
||||||
|
if scalarType.Kind() == reflect.Bool {
|
||||||
|
spec.isBool = true
|
||||||
|
}
|
||||||
|
|
||||||
// Look at the tag
|
// Look at the tag
|
||||||
if tag != "" {
|
if tag != "" {
|
||||||
for _, key := range strings.Split(tag, ",") {
|
for _, key := range strings.Split(tag, ",") {
|
||||||
|
|
16
usage.go
16
usage.go
|
@ -5,7 +5,6 @@ import (
|
||||||
"io"
|
"io"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"reflect"
|
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -78,10 +77,17 @@ func (p *Parser) WriteHelp(w io.Writer) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// write the list of options
|
// write the list of options
|
||||||
if len(options) > 0 {
|
|
||||||
fmt.Fprint(w, "\noptions:\n")
|
fmt.Fprint(w, "\noptions:\n")
|
||||||
const colWidth = 25
|
|
||||||
for _, spec := range options {
|
for _, spec := range options {
|
||||||
|
printOption(w, spec)
|
||||||
|
}
|
||||||
|
|
||||||
|
// write the list of built in options
|
||||||
|
printOption(w, &spec{isBool: true, long: "help", short: "h", help: "display this help and exit"})
|
||||||
|
}
|
||||||
|
|
||||||
|
func printOption(w io.Writer, spec *spec) {
|
||||||
|
const colWidth = 25
|
||||||
left := " " + synopsis(spec, "--"+spec.long)
|
left := " " + synopsis(spec, "--"+spec.long)
|
||||||
if spec.short != "" {
|
if spec.short != "" {
|
||||||
left += ", " + synopsis(spec, "-"+spec.short)
|
left += ", " + synopsis(spec, "-"+spec.short)
|
||||||
|
@ -96,12 +102,10 @@ func (p *Parser) WriteHelp(w io.Writer) {
|
||||||
fmt.Fprint(w, spec.help)
|
fmt.Fprint(w, spec.help)
|
||||||
}
|
}
|
||||||
fmt.Fprint(w, "\n")
|
fmt.Fprint(w, "\n")
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func synopsis(spec *spec, form string) string {
|
func synopsis(spec *spec, form string) string {
|
||||||
if spec.dest.Kind() == reflect.Bool {
|
if spec.isBool {
|
||||||
return form
|
return form
|
||||||
}
|
}
|
||||||
return form + " " + strings.ToUpper(spec.long)
|
return form + " " + strings.ToUpper(spec.long)
|
||||||
|
|
|
@ -23,6 +23,7 @@ options:
|
||||||
--dataset DATASET dataset to use
|
--dataset DATASET dataset to use
|
||||||
--optimize OPTIMIZE, -O OPTIMIZE
|
--optimize OPTIMIZE, -O OPTIMIZE
|
||||||
optimization level
|
optimization level
|
||||||
|
--help, -h display this help and exit
|
||||||
`
|
`
|
||||||
var args struct {
|
var args struct {
|
||||||
Input string `arg:"positional"`
|
Input string `arg:"positional"`
|
||||||
|
|
Loading…
Reference in New Issue