Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
peterbourgon committed Sep 7, 2023
1 parent 6206f31 commit f1a1f0e
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 17 deletions.
19 changes: 10 additions & 9 deletions core_flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -535,17 +535,18 @@ func (fs *CoreFlags) AddFlag(cfg CoreFlagConfig) (Flag, error) {
return f, nil
}

// AddStruct adds flags to the flag set which are taken from the given val,
// which must be a pointer to a struct. Each exported field in that struct with
// a valid `ff:` struct tag corresponds to a unique flag in the flag set. Those
// fields must be a supported ffval.ValueType, or implement flag.Value.
// AddStruct adds flags to the flag set from the given val, which must be a
// pointer to a struct. Each exported field in that struct with a valid `ff:`
// struct tag corresponds to a unique flag in the flag set. Those fields must be
// a supported [ffval.ValueType] or implement [flag.Value].
//
// The `ff:` struct tag is parsed as a sequence of comma- or pipe-separated
// items. An item is either a key, or a key value pair expressed as key=value or
// key: value. Keys, values, and items themselves are trimmed of leading and
// trailing whitespace before use. 'Single quoted' values are unquoted.
// items. An item is either a key, or a key/value pair. Key/value pairs are
// expressed as either key=value (with =), or key: value (with :). Keys, values,
// and items themselves are trimmed of whitespace before use. Values may be
// 'single quoted' and will be unquoted before use.
//
// The following is a list of valid keys.
// The following is a list of valid keys and their expected values.
//
// - s, short, shortname -- value should be a valid short name
// - l, long, longname -- value should be a valid long name
Expand Down Expand Up @@ -590,7 +591,7 @@ func (fs *CoreFlags) AddStruct(val any) error {
if r == '\'' {
quoted = !quoted
}
return !quoted && r == ','
return !quoted && (r == ',' || r == '|')
})
if len(items) <= 0 {
continue
Expand Down
16 changes: 8 additions & 8 deletions core_flags_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -486,13 +486,13 @@ func TestCoreFlags_struct(t *testing.T) {

func ExampleCoreFlags_AddStruct() {
var flags struct {
Alpha string `ff:" shortname=a , longname=alpha , default=abc , , usage=alpha string , "`
Beta int `ff:" , long=beta , , p=β , usage: beta int , "`
Delta bool `ff:" short: d , , , , usage = delta bool , nodefault "`
Epsilon bool `ff:" s=e , l=epsilon , , , u: 'epsilon: bool' , nodefault "`
Gamma string `ff:" s:g , l:gamma , , , u='comma, ok' , "`
Iota float64 `ff:" , long=iota , d=0.43 , , usage: 🦊 , "`
Kappa ffval.StringSet `ff:" short=k , long=kappa , u:kappa (repeatable) , "`
Alpha string `ff:" shortname=a , longname=alpha , default=abc , , usage=alpha string "`
Beta int `ff:" , long=beta , , p=β , usage: beta int "`
Delta bool `ff:" short: d , , , , nodefault , usage: 'delta|bool' "`
Epsilon bool `ff:" s=e , l=epsilon , , , nodefault , u: 'epsilon: bool' "`
Gamma string `ff:" s:g | l:gamma | | | u='comma, ok' "`
Iota float64 `ff:" | long=iota | d=0.43 | | usage: 🦊 "`
Kappa ffval.StringSet `ff:" short=k | long=kappa , | , u:kappa (repeatable) "`
}

fs := ff.NewFlags("mycommand")
Expand All @@ -506,7 +506,7 @@ func ExampleCoreFlags_AddStruct() {
// FLAGS
// -a, --alpha STRING alpha string (default: abc)
// --beta β beta int (default: 0)
// -d delta bool
// -d delta|bool
// -e, --epsilon epsilon: bool
// -g, --gamma STRING comma, ok
// --iota FLOAT64 🦊 (default: 0.43)
Expand Down

0 comments on commit f1a1f0e

Please sign in to comment.