Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support field tag prefix and custom field tag names #410

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 24 additions & 12 deletions command.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ type Command struct {
commands []*Command
hasBuiltinHelpGroup bool
args []*Arg
flagTags *FlagTags
}

// Commander is an interface which can be implemented by any command added in
Expand Down Expand Up @@ -72,9 +73,15 @@ type lookup struct {
// options are in the command. The provided data can implement the Command and
// Usage interfaces.
func (c *Command) AddCommand(command string, shortDescription string, longDescription string, data interface{}) (*Command, error) {
return c.AddCommandWithCustomFlagTags(command, shortDescription, longDescription, data, c.flagTags)
}

func (c *Command) AddCommandWithCustomFlagTags(command string, shortDescription string, longDescription string, data interface{}, flagTags *FlagTags) (*Command, error) {
cmd := newCommand(command, shortDescription, longDescription, data)

cmd.parent = c
cmd.flagTags = flagTags
cmd.Group.flagTags = flagTags

if err := cmd.scan(); err != nil {
return nil, err
Expand All @@ -88,9 +95,14 @@ func (c *Command) AddCommand(command string, shortDescription string, longDescri
// data needs to be a pointer to a struct from which the fields indicate which
// options are in the group.
func (c *Command) AddGroup(shortDescription string, longDescription string, data interface{}) (*Group, error) {
return c.AddGroupWithCustomFlagTags(shortDescription, longDescription, data, c.flagTags)
}

func (c *Command) AddGroupWithCustomFlagTags(shortDescription string, longDescription string, data interface{}, flagTags *FlagTags) (*Group, error) {
group := newGroup(shortDescription, longDescription, data)

group.parent = c
group.flagTags = flagTags

if err := group.scanType(c.scanSubcommandHandler(group)); err != nil {
return nil, err
Expand Down Expand Up @@ -166,7 +178,7 @@ func (c *Command) scanSubcommandHandler(parentg *Group) scanHandler {
return true, err
}

positional := mtag.Get("positional-args")
positional := mtag.Get(c.flagTags.PositionalArgs)

if len(positional) != 0 {
stype := realval.Type()
Expand All @@ -180,7 +192,7 @@ func (c *Command) scanSubcommandHandler(parentg *Group) scanHandler {
return true, err
}

name := m.Get("positional-arg-name")
name := m.Get(c.flagTags.PositionalArgName)

if len(name) == 0 {
name = field.Name
Expand All @@ -189,7 +201,7 @@ func (c *Command) scanSubcommandHandler(parentg *Group) scanHandler {
required := -1
requiredMaximum := -1

sreq := m.Get("required")
sreq := m.Get(c.flagTags.Required)

if sreq != "" {
required = 1
Expand All @@ -213,7 +225,7 @@ func (c *Command) scanSubcommandHandler(parentg *Group) scanHandler {

arg := &Arg{
Name: name,
Description: m.Get("description"),
Description: m.Get(c.flagTags.Description),
Required: required,
RequiredMaximum: requiredMaximum,

Expand All @@ -223,15 +235,15 @@ func (c *Command) scanSubcommandHandler(parentg *Group) scanHandler {

c.args = append(c.args, arg)

if len(mtag.Get("required")) != 0 {
if len(mtag.Get(c.flagTags.Required)) != 0 {
c.ArgsRequired = true
}
}

return true, nil
}

subcommand := mtag.Get("command")
subcommand := mtag.Get(c.flagTags.Command)

if len(subcommand) != 0 {
var ptrval reflect.Value
Expand All @@ -246,19 +258,19 @@ func (c *Command) scanSubcommandHandler(parentg *Group) scanHandler {
ptrval = realval.Addr()
}

shortDescription := mtag.Get("description")
longDescription := mtag.Get("long-description")
subcommandsOptional := mtag.Get("subcommands-optional")
aliases := mtag.GetMany("alias")
passAfterNonOption := mtag.Get("pass-after-non-option")
shortDescription := mtag.Get(c.flagTags.Description)
longDescription := mtag.Get(c.flagTags.LongDescription)
subcommandsOptional := mtag.Get(c.flagTags.SubCommandsOptional)
aliases := mtag.GetMany(c.flagTags.Alias)
passAfterNonOption := mtag.Get(c.flagTags.PassAfterNonOption)

subc, err := c.AddCommand(subcommand, shortDescription, longDescription, ptrval.Interface())

if err != nil {
return true, err
}

subc.Hidden = mtag.Get("hidden") != ""
subc.Hidden = mtag.Get(c.flagTags.Hidden) != ""

if len(subcommandsOptional) > 0 {
subc.SubcommandsOptional = true
Expand Down
38 changes: 19 additions & 19 deletions convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ type ValueValidator interface {
IsValidValue(value string) error
}

func getBase(options multiTag, base int) (int, error) {
sbase := options.Get("base")
func getBase(options multiTag, flagTags *FlagTags, base int) (int, error) {
sbase := options.Get(flagTags.Base)

var err error
var ivbase int64
Expand All @@ -63,7 +63,7 @@ func convertMarshal(val reflect.Value) (bool, string, error) {
return false, "", nil
}

func convertToString(val reflect.Value, options multiTag) (string, error) {
func convertToString(val reflect.Value, options multiTag, flagTags *FlagTags) (string, error) {
if ok, ret, err := convertMarshal(val); ok {
return ret, err
}
Expand All @@ -90,15 +90,15 @@ func convertToString(val reflect.Value, options multiTag) (string, error) {

return "false", nil
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
base, err := getBase(options, 10)
base, err := getBase(options, flagTags, 10)

if err != nil {
return "", err
}

return strconv.FormatInt(val.Int(), base), nil
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
base, err := getBase(options, 10)
base, err := getBase(options, flagTags, 10)

if err != nil {
return "", err
Expand All @@ -119,7 +119,7 @@ func convertToString(val reflect.Value, options multiTag) (string, error) {
ret += ", "
}

item, err := convertToString(val.Index(i), options)
item, err := convertToString(val.Index(i), options, flagTags)

if err != nil {
return "", err
Expand All @@ -137,13 +137,13 @@ func convertToString(val reflect.Value, options multiTag) (string, error) {
ret += ", "
}

keyitem, err := convertToString(key, options)
keyitem, err := convertToString(key, options, flagTags)

if err != nil {
return "", err
}

item, err := convertToString(val.MapIndex(key), options)
item, err := convertToString(val.MapIndex(key), options, flagTags)

if err != nil {
return "", err
Expand All @@ -154,10 +154,10 @@ func convertToString(val reflect.Value, options multiTag) (string, error) {

return ret + "}", nil
case reflect.Ptr:
return convertToString(reflect.Indirect(val), options)
return convertToString(reflect.Indirect(val), options, flagTags)
case reflect.Interface:
if !val.IsNil() {
return convertToString(val.Elem(), options)
return convertToString(val.Elem(), options, flagTags)
}
}

Expand Down Expand Up @@ -189,7 +189,7 @@ func convertUnmarshal(val string, retval reflect.Value) (bool, error) {
return false, nil
}

func convert(val string, retval reflect.Value, options multiTag) error {
func convert(val string, retval reflect.Value, options multiTag, flagTags *FlagTags) error {
if ok, err := convertUnmarshal(val, retval); ok {
return err
}
Expand Down Expand Up @@ -224,7 +224,7 @@ func convert(val string, retval reflect.Value, options multiTag) error {
retval.SetBool(b)
}
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
base, err := getBase(options, 0)
base, err := getBase(options, flagTags, 0)

if err != nil {
return err
Expand All @@ -238,7 +238,7 @@ func convert(val string, retval reflect.Value, options multiTag) error {

retval.SetInt(parsed)
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
base, err := getBase(options, 0)
base, err := getBase(options, flagTags, 0)

if err != nil {
return err
Expand All @@ -265,13 +265,13 @@ func convert(val string, retval reflect.Value, options multiTag) error {
elemvalptr := reflect.New(elemtp)
elemval := reflect.Indirect(elemvalptr)

if err := convert(val, elemval, options); err != nil {
if err := convert(val, elemval, options, flagTags); err != nil {
return err
}

retval.Set(reflect.Append(retval, elemval))
case reflect.Map:
keyValueDelimiter := options.Get("key-value-delimiter")
keyValueDelimiter := options.Get(flagTags.KeyValueDelimiter)
if keyValueDelimiter == "" {
keyValueDelimiter = ":"
}
Expand All @@ -288,14 +288,14 @@ func convert(val string, retval reflect.Value, options multiTag) error {
keytp := tp.Key()
keyval := reflect.New(keytp)

if err := convert(key, keyval, options); err != nil {
if err := convert(key, keyval, options, flagTags); err != nil {
return err
}

valuetp := tp.Elem()
valueval := reflect.New(valuetp)

if err := convert(value, valueval, options); err != nil {
if err := convert(value, valueval, options, flagTags); err != nil {
return err
}

Expand All @@ -309,10 +309,10 @@ func convert(val string, retval reflect.Value, options multiTag) error {
retval.Set(reflect.New(retval.Type().Elem()))
}

return convert(val, reflect.Indirect(retval), options)
return convert(val, reflect.Indirect(retval), options, flagTags)
case reflect.Interface:
if !retval.IsNil() {
return convert(val, retval.Elem(), options)
return convert(val, retval.Elem(), options, flagTags)
}
}

Expand Down
8 changes: 4 additions & 4 deletions convert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
)

func expectConvert(t *testing.T, o *Option, expected string) {
s, err := convertToString(o.value, o.tag)
s, err := convertToString(o.value, o.tag, NewFlagTags())

if err != nil {
t.Errorf("Unexpected error: %v", err)
Expand Down Expand Up @@ -129,7 +129,7 @@ func TestConvertToStringInvalidIntBase(t *testing.T) {
grp, _ := p.AddGroup("test group", "", &opts)
o := grp.Options()[0]

_, err := convertToString(o.value, o.tag)
_, err := convertToString(o.value, o.tag, NewFlagTags())

if err != nil {
err = newErrorf(ErrMarshal, "%v", err)
Expand All @@ -149,7 +149,7 @@ func TestConvertToStringInvalidUintBase(t *testing.T) {
grp, _ := p.AddGroup("test group", "", &opts)
o := grp.Options()[0]

_, err := convertToString(o.value, o.tag)
_, err := convertToString(o.value, o.tag, NewFlagTags())

if err != nil {
err = newErrorf(ErrMarshal, "%v", err)
Expand All @@ -167,7 +167,7 @@ func TestConvertToMapWithDelimiter(t *testing.T) {
grp, _ := p.AddGroup("test group", "", &opts)
o := grp.Options()[0]

err := convert("key=value", o.value, o.tag)
err := convert("key=value", o.value, o.tag, NewFlagTags())

if err != nil {
t.Errorf("Unexpected error: %v", err)
Expand Down
Loading