Skip to content

Commit

Permalink
Allow for even more extended usage
Browse files Browse the repository at this point in the history
  • Loading branch information
AdamSLevy committed Mar 5, 2020
1 parent 1008425 commit a8f0146
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 25 deletions.
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
37 changes: 22 additions & 15 deletions bind.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down Expand Up @@ -144,12 +143,13 @@ var PrefixSeparator = "-"
//
// <usage> - 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)
//
Expand Down Expand Up @@ -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 _<fieldT.Name> 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) {
Expand Down
10 changes: 6 additions & 4 deletions bind_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down
4 changes: 1 addition & 3 deletions flagtag.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ type flagTag struct {
ShortName string
Value string
Usage string
UsageRef bool

Ignored bool

Expand Down Expand Up @@ -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
}
Expand Down

0 comments on commit a8f0146

Please sign in to comment.