summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--parse.go6
-rw-r--r--usage.go44
-rw-r--r--usage_test.go1
3 files changed, 31 insertions, 20 deletions
diff --git a/parse.go b/parse.go
index a9e509f..b6eae4e 100644
--- a/parse.go
+++ b/parse.go
@@ -19,6 +19,7 @@ type spec struct {
positional bool
help string
wasPresent bool
+ isBool bool
}
// 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())
}
+ // Specify that it is a bool for usage
+ if scalarType.Kind() == reflect.Bool {
+ spec.isBool = true
+ }
+
// Look at the tag
if tag != "" {
for _, key := range strings.Split(tag, ",") {
diff --git a/usage.go b/usage.go
index 824f0eb..9404015 100644
--- a/usage.go
+++ b/usage.go
@@ -5,7 +5,6 @@ import (
"io"
"os"
"path/filepath"
- "reflect"
"strings"
)
@@ -78,30 +77,35 @@ func (p *Parser) WriteHelp(w io.Writer) {
}
// write the list of options
- if len(options) > 0 {
- fmt.Fprint(w, "\noptions:\n")
- const colWidth = 25
- for _, spec := range options {
- 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))
- }
- fmt.Fprint(w, spec.help)
- }
- fmt.Fprint(w, "\n")
+ fmt.Fprint(w, "\noptions:\n")
+ 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)
+ 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))
}
+ fmt.Fprint(w, spec.help)
}
+ fmt.Fprint(w, "\n")
}
func synopsis(spec *spec, form string) string {
- if spec.dest.Kind() == reflect.Bool {
+ if spec.isBool {
return form
}
return form + " " + strings.ToUpper(spec.long)
diff --git a/usage_test.go b/usage_test.go
index 83da1c1..5a9199c 100644
--- a/usage_test.go
+++ b/usage_test.go
@@ -23,6 +23,7 @@ options:
--dataset DATASET dataset to use
--optimize OPTIMIZE, -O OPTIMIZE
optimization level
+ --help, -h display this help and exit
`
var args struct {
Input string `arg:"positional"`