summaryrefslogtreecommitdiff
path: root/usage.go
diff options
context:
space:
mode:
authorAlex Flint <[email protected]>2015-11-22 10:04:14 +1030
committerAlex Flint <[email protected]>2015-11-22 10:04:14 +1030
commit5db9c77fa32bd444360f8376ddfa7bc209813b20 (patch)
treeed9a5f3a36a5822d82d47f4e8be7b40671e79183 /usage.go
parent785fa3f8c93bc43540b189bacaed9d9aba890528 (diff)
parent330a0da571888c4ec6dac345140a7ee663d00d6e (diff)
Merge pull request #13 from walle/show_builtin
Add built ins to options in help output
Diffstat (limited to 'usage.go')
-rw-r--r--usage.go44
1 files changed, 24 insertions, 20 deletions
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)