summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlex Flint <[email protected]>2016-01-18 08:58:11 -0800
committerAlex Flint <[email protected]>2016-01-18 08:58:11 -0800
commitc9155bb0c3e57557bae7d7c54b60ab8fe2e95211 (patch)
treeca87fe752f37ce817559efa60f58ef27e7be6c35
parentf8ea16beeeb29e99546599ca08095378a124262f (diff)
parent4197d283e41bbce1a2cac20ea56fd1156a677db1 (diff)
Merge pull request #26 from alexflint/brettlangdon-dev/positional.help.sqwished
Resolve some merge conflicts from #8
-rw-r--r--usage.go16
-rw-r--r--usage_test.go27
2 files changed, 39 insertions, 4 deletions
diff --git a/usage.go b/usage.go
index 23f7aa5..61f0ad6 100644
--- a/usage.go
+++ b/usage.go
@@ -9,6 +9,9 @@ import (
"strings"
)
+// the width of the left column
+const colWidth = 25
+
// Fail prints usage information to stderr and exits with non-zero status
func (p *Parser) Fail(msg string) {
p.WriteUsage(os.Stderr)
@@ -73,7 +76,17 @@ func (p *Parser) WriteHelp(w io.Writer) {
if len(positionals) > 0 {
fmt.Fprint(w, "\npositional arguments:\n")
for _, spec := range positionals {
- fmt.Fprintf(w, " %s\n", spec.long)
+ left := " " + spec.long
+ 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")
}
}
@@ -88,7 +101,6 @@ func (p *Parser) WriteHelp(w io.Writer) {
}
func printOption(w io.Writer, spec *spec) {
- const colWidth = 25
left := " " + synopsis(spec, "--"+spec.long)
if spec.short != "" {
left += ", " + synopsis(spec, "-"+spec.short)
diff --git a/usage_test.go b/usage_test.go
index 07edc18..2375e81 100644
--- a/usage_test.go
+++ b/usage_test.go
@@ -16,7 +16,7 @@ func TestWriteUsage(t *testing.T) {
positional arguments:
input
- output
+ output list of outputs
options:
--name NAME name to use [default: Foo Bar]
@@ -30,7 +30,7 @@ options:
`
var args struct {
Input string `arg:"positional"`
- Output []string `arg:"positional"`
+ Output []string `arg:"positional,help:list of outputs"`
Name string `arg:"help:name to use"`
Value int `arg:"help:secret value"`
Verbose bool `arg:"-v,help:verbosity level"`
@@ -53,3 +53,26 @@ options:
p.WriteHelp(&help)
assert.Equal(t, expectedHelp, help.String())
}
+
+func TestUsageLongPositionalWithHelp(t *testing.T) {
+ expectedHelp := `usage: example VERYLONGPOSITIONALWITHHELP
+
+positional arguments:
+ verylongpositionalwithhelp
+ this positional argument is very long
+
+options:
+ --help, -h display this help and exit
+`
+ var args struct {
+ VeryLongPositionalWithHelp string `arg:"positional,help:this positional argument is very long"`
+ }
+
+ p, err := NewParser(&args)
+ require.NoError(t, err)
+
+ os.Args[0] = "example"
+ var help bytes.Buffer
+ p.WriteHelp(&help)
+ assert.Equal(t, expectedHelp, help.String())
+}