Skip to content

Commit

Permalink
Merge pull request #243 from alexflint/handle-empty-placeholder
Browse files Browse the repository at this point in the history
Handle explicit empty placeholders
  • Loading branch information
alexflint committed Apr 2, 2024
2 parents 188bd31 + be792f1 commit dfca71d
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 5 deletions.
8 changes: 4 additions & 4 deletions parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -335,9 +335,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
Expand Down Expand Up @@ -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
Expand Down
5 changes: 4 additions & 1 deletion usage.go
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,10 @@ func (p *Parser) printEnvOnlyVar(w io.Writer, spec *spec) {
}

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
Expand Down
29 changes: 29 additions & 0 deletions usage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -601,6 +601,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]"

Expand Down

0 comments on commit dfca71d

Please sign in to comment.