From d45bd4523c98b9190de5cd7a43ee32087451fe5f Mon Sep 17 00:00:00 2001 From: brettlangdon Date: Sat, 21 Nov 2015 18:59:40 -0500 Subject: [PATCH 1/2] Display help text for positional arguments --- usage.go | 15 ++++++++++++++- usage_test.go | 27 +++++++++++++++++++++++++-- 2 files changed, 39 insertions(+), 3 deletions(-) diff --git a/usage.go b/usage.go index 9404015..2c38fe7 100644 --- a/usage.go +++ b/usage.go @@ -68,11 +68,24 @@ func (p *Parser) WriteHelp(w io.Writer) { p.WriteUsage(w) + // the width of the left column + const colWidth = 25 + // 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) + 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") } } diff --git a/usage_test.go b/usage_test.go index 5a9199c..4a56e6e 100644 --- a/usage_test.go +++ b/usage_test.go @@ -16,7 +16,7 @@ func TestWriteUsage(t *testing.T) { positional arguments: input - output + output positional output options: --verbose, -v verbosity level @@ -27,7 +27,7 @@ options: ` var args struct { Input string `arg:"positional"` - Output []string `arg:"positional"` + Output []string `arg:"positional,help:positional output"` Verbose bool `arg:"-v,help:verbosity level"` Dataset string `arg:"help:dataset to use"` Optimize int `arg:"-O,help:optimization level"` @@ -45,3 +45,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()) +} From 4197d283e41bbce1a2cac20ea56fd1156a677db1 Mon Sep 17 00:00:00 2001 From: Alex Flint Date: Mon, 18 Jan 2016 08:24:21 -0800 Subject: [PATCH 2/2] extract common colWidth constant --- usage.go | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/usage.go b/usage.go index 65f7c2d..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) @@ -69,9 +72,6 @@ func (p *Parser) WriteHelp(w io.Writer) { p.WriteUsage(w) - // the width of the left column - const colWidth = 25 - // write the list of positionals if len(positionals) > 0 { fmt.Fprint(w, "\npositional arguments:\n") @@ -101,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)