diff --git a/README.md b/README.md index d2b6eff..f30c2c2 100644 --- a/README.md +++ b/README.md @@ -23,9 +23,10 @@ flags := struct { // otherwise it is ignored for use with the standard flag package. ShortName bool `flag:"short,s"` - // Optionally put the usage string in the struct by setting it to "_". - URL string `flag:"url,u;http://www.example.com/;_" - _URL string + // Optionally extende the usage tag with subsequent `use` tags + // on _ fields. + URL string `flag:"url,u;http://www.example.com/;Start usage here" + _ struct{} `use:"continue longer usage string for --url below it", // Nested and Embedded structs can add a flag name prefix, or not. Nested StructA diff --git a/bind.go b/bind.go index 266ad93..011a7ae 100644 --- a/bind.go +++ b/bind.go @@ -36,10 +36,10 @@ // // otherwise it is ignored for use with the standard flag package. // ShortName bool `flag:"short,s"` // -// // Optionally put the usage string in the struct by setting it -// // to "_". -// URL string `flag:"url,u;http://www.example.com/;_" -// _URL string +// // Optionally extende the usage tag with subsequent `use` tags +// // on _ fields. +// URL string `flag:"url,u;http://www.example.com/;Start usage here" +// _ struct{} `use:"continue longer usage string for --url below it", // // // Nested and Embedded structs can add a flag name prefix, or not. // Nested StructA @@ -53,7 +53,6 @@ // }{ // // Default values may also be set directly to override the tag. // StringFlag: "override tag default", -// _URL: "Include a longer usage string for --url here", // } // // fs := pflag.NewFlagSet("", pflag.ContinueOnError) @@ -144,12 +143,13 @@ var PrefixSeparator = "-" // // - The usage string for the flag. By default, the usage for the flag // is empty unless specified. For longer usage strings that don't fit nicely in -// the same tag, specify "_" and define the following field as _ with a -// `use:"..."` tag. For example, +// a single tag, you may define subsequent fields named _ with a `use:"..."` +// tag. These are joined with a single space into the full usage. For example, // // flags := struct { -// URL string `flag:"url;;_"` -// _ struct{} `use:"URL usage goes here"` +// URL string `flag:"url;;Start usage here..."` +// _ struct{} `use:"... contiued usage goes here"` +// _ struct{} `use:"... and more here"` // }{"http://www.example.com", "Query this URL"} // err := Bind(flg, &flags) // @@ -257,14 +257,21 @@ func BindWithPrefix(flg FlagSet, v interface{}, prefix string) error { tag.Name = fmt.Sprintf("%v%v", prefix, tag.Name) - // If UsageRef is set, check for the _ string - // usage field. - if tag.UsageRef && i+1 < val.NumField() { + // Check for extended usage tags. + for i := i + 1; i < val.NumField(); i++ { // Check if next field is named "_" and has a use tag. - usageT := valT.Field(i + 1) - if usageT.Name == "_" { - tag.Usage = usageT.Tag.Get("use") + usageT := valT.Field(i) + if usageT.Name != "_" { + break + } + usage, ok := usageT.Tag.Lookup("use") + if !ok { + break + } + if tag.Usage != "" { + tag.Usage += " " } + tag.Usage += usage } switch p := fieldV.Interface().(type) { diff --git a/bind_test.go b/bind_test.go index fe5195d..f36c7ba 100644 --- a/bind_test.go +++ b/bind_test.go @@ -144,11 +144,13 @@ type ValidTestFlags struct { LongShort bool `flag:"long,l"` ShortLong bool `flag:"r,-rlong"` - ExtendedUsage bool `flag:";;_"` - _ string `use:"Extended usage"` + ExtendedUsage bool `flag:";;"` + _ struct{} `use:"Extended usage, "` + _ struct{} `use:"continue usage"` - Hidden bool `flag:";;Hidden usage;hidden"` - HideDefault string `flag:";default value;Hide default;hide-default"` + Hidden bool `flag:";;Hidden usage;hidden"` + HideDefault string `flag:";default value;Hide default;hide-default"` + _ struct{} // no use tag Ptr *bool PtrDefault *int `flag:";50"` diff --git a/flagtag.go b/flagtag.go index 46b40bd..00cfd76 100644 --- a/flagtag.go +++ b/flagtag.go @@ -31,7 +31,6 @@ type flagTag struct { ShortName string Value string Usage string - UsageRef bool Ignored bool @@ -62,8 +61,7 @@ func newFlagTag(tag string) (fTag flagTag) { return } - fTag.Usage = args[2] - fTag.UsageRef = fTag.Usage == "_" + fTag.Usage = strings.TrimSpace(args[2]) if len(args) == 3 { return }