From 8e35a4f0d4616f11c322e63dc3ad373fe3d25e0d Mon Sep 17 00:00:00 2001 From: Alex Flint Date: Sun, 31 Mar 2024 10:30:12 -0400 Subject: [PATCH 1/2] handle explicit empty placeholders --- parse.go | 8 ++++---- usage.go | 5 ++++- usage_test.go | 29 +++++++++++++++++++++++++++++ 3 files changed, 37 insertions(+), 5 deletions(-) diff --git a/parse.go b/parse.go index 0bdddc7..9f3e687 100644 --- a/parse.go +++ b/parse.go @@ -56,7 +56,7 @@ type spec struct { env string // the name of the environment variable for this option, or empty for none defaultValue reflect.Value // default value for this option defaultString string // default value for this option, in string form to be displayed in help text - placeholder string // name of the data in help + placeholder string // placeholder string in help } // command represents a named subcommand, or the top-level command @@ -341,9 +341,8 @@ func cmdFromStruct(name string, dest path, t reflect.Type) (*command, error) { spec.help = help } - // Look at the tag - var isSubcommand bool // tracks whether this field is a subcommand - + // process each comma-separated part of the tag + var isSubcommand bool for _, key := range strings.Split(tag, ",") { if key == "" { continue @@ -407,6 +406,7 @@ func cmdFromStruct(name string, dest path, t reflect.Type) (*command, error) { } } + // placeholder is the string used in the help text like this: "--somearg PLACEHOLDER" placeholder, hasPlaceholder := field.Tag.Lookup("placeholder") if hasPlaceholder { spec.placeholder = placeholder diff --git a/usage.go b/usage.go index a9f9844..43af31e 100644 --- a/usage.go +++ b/usage.go @@ -356,7 +356,10 @@ func (p *Parser) lookupCommand(path ...string) (*command, error) { } func synopsis(spec *spec, form string) string { - if spec.cardinality == zero { + // if the user omits the placeholder tag then we pick one automatically, + // but if the user explicitly specifies an empty placeholder then we + // leave out the placeholder in the help message + if spec.cardinality == zero || spec.placeholder == "" { return form } return form + " " + spec.placeholder diff --git a/usage_test.go b/usage_test.go index 1a64ad4..1e2aca4 100644 --- a/usage_test.go +++ b/usage_test.go @@ -522,6 +522,35 @@ Options: assert.Equal(t, expectedUsage, strings.TrimSpace(usage.String())) } +func TestUsageWithEmptyPlaceholder(t *testing.T) { + expectedUsage := "Usage: example [-a] [--b] [--c]" + + expectedHelp := ` +Usage: example [-a] [--b] [--c] + +Options: + -a some help for a + --b some help for b + --c, -c some help for c + --help, -h display this help and exit +` + var args struct { + ShortOnly string `arg:"-a,--" placeholder:"" help:"some help for a"` + LongOnly string `arg:"--b" placeholder:"" help:"some help for b"` + Both string `arg:"-c,--c" placeholder:"" help:"some help for c"` + } + p, err := NewParser(Config{Program: "example"}, &args) + require.NoError(t, err) + + var help bytes.Buffer + p.WriteHelp(&help) + assert.Equal(t, expectedHelp[1:], help.String()) + + var usage bytes.Buffer + p.WriteUsage(&usage) + assert.Equal(t, expectedUsage, strings.TrimSpace(usage.String())) +} + func TestUsageWithShortFirst(t *testing.T) { expectedUsage := "Usage: example [-c CAT] [--dog DOG]" From be792f1f8b2f3971c54e00240ea35a53512ec7bf Mon Sep 17 00:00:00 2001 From: Alex Flint Date: Sun, 31 Mar 2024 11:52:16 -0400 Subject: [PATCH 2/2] ping