From b0d37d1fb2b491a861f788a41aabbe66cbf83110 Mon Sep 17 00:00:00 2001 From: Fredrik Wallgren <fredrik.wallgren@gmail.com> Date: Thu, 19 Nov 2015 14:34:09 +0100 Subject: [PATCH 1/2] Add default values to usage Check if the value isn't it's zero value and if not add a default value to the usage text. --- usage.go | 8 +++++++- usage_test.go | 10 ++++++++-- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/usage.go b/usage.go index 9404015..64ed901 100644 --- a/usage.go +++ b/usage.go @@ -76,7 +76,6 @@ func (p *Parser) WriteHelp(w io.Writer) { } } - // write the list of options fmt.Fprint(w, "\noptions:\n") for _, spec := range options { printOption(w, spec) @@ -101,6 +100,13 @@ func printOption(w io.Writer, spec *spec) { } fmt.Fprint(w, spec.help) } + // Check if spec.dest is zero value or not + // If it isn't a default value have been added + v := spec.dest + z := reflect.Zero(v.Type()) + if v.Interface() != z.Interface() { + fmt.Fprintf(w, " [default: %v]", v) + } fmt.Fprint(w, "\n") } diff --git a/usage_test.go b/usage_test.go index 5a9199c..01c9542 100644 --- a/usage_test.go +++ b/usage_test.go @@ -10,15 +10,17 @@ import ( ) func TestWriteUsage(t *testing.T) { - expectedUsage := "usage: example [--verbose] [--dataset DATASET] [--optimize OPTIMIZE] INPUT [OUTPUT [OUTPUT ...]]\n" + expectedUsage := "usage: example [--name NAME] [--value VALUE] [--verbose] [--dataset DATASET] [--optimize OPTIMIZE] INPUT [OUTPUT [OUTPUT ...]]\n" - expectedHelp := `usage: example [--verbose] [--dataset DATASET] [--optimize OPTIMIZE] INPUT [OUTPUT [OUTPUT ...]] + expectedHelp := `usage: example [--name NAME] [--value VALUE] [--verbose] [--dataset DATASET] [--optimize OPTIMIZE] INPUT [OUTPUT [OUTPUT ...]] positional arguments: input output options: + --name NAME name to use [default: Foo Bar] + --value VALUE secret value [default: 42] --verbose, -v verbosity level --dataset DATASET dataset to use --optimize OPTIMIZE, -O OPTIMIZE @@ -28,10 +30,14 @@ options: var args struct { Input string `arg:"positional"` Output []string `arg:"positional"` + Name string `arg:"help:name to use"` + Value int `arg:"help:secret value"` Verbose bool `arg:"-v,help:verbosity level"` Dataset string `arg:"help:dataset to use"` Optimize int `arg:"-O,help:optimization level"` } + args.Name = "Foo Bar" + args.Value = 42 p, err := NewParser(&args) require.NoError(t, err) From 670c7b787d9a475c663e3868eeb226ff7c5e78ad Mon Sep 17 00:00:00 2001 From: Fredrik Wallgren <fredrik.wallgren@gmail.com> Date: Sun, 22 Nov 2015 00:58:27 +0100 Subject: [PATCH 2/2] Fix merge conflicts --- usage.go | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/usage.go b/usage.go index 64ed901..fa01229 100644 --- a/usage.go +++ b/usage.go @@ -5,6 +5,7 @@ import ( "io" "os" "path/filepath" + "reflect" "strings" ) @@ -76,6 +77,7 @@ func (p *Parser) WriteHelp(w io.Writer) { } } + // write the list of options fmt.Fprint(w, "\noptions:\n") for _, spec := range options { printOption(w, spec) @@ -103,9 +105,11 @@ func printOption(w io.Writer, spec *spec) { // Check if spec.dest is zero value or not // If it isn't a default value have been added v := spec.dest - z := reflect.Zero(v.Type()) - if v.Interface() != z.Interface() { - fmt.Fprintf(w, " [default: %v]", v) + if v.IsValid() { + z := reflect.Zero(v.Type()) + if v.Interface() != z.Interface() { + fmt.Fprintf(w, " [default: %v]", v) + } } fmt.Fprint(w, "\n") }