set subcommand structs to be struct pointers

This commit is contained in:
Alex Flint 2019-04-30 13:40:45 -07:00
parent af12b7cfc2
commit a78c6ded26
1 changed files with 5 additions and 0 deletions

View File

@ -283,6 +283,7 @@ func cmdFromStruct(name string, dest path, t reflect.Type) (*command, error) {
cmdname = strings.ToLower(field.Name)
}
// parse the subcommand recursively
subcmd, err := cmdFromStruct(cmdname, subdest, field.Type)
if err != nil {
errs = append(errs, err.Error())
@ -445,6 +446,10 @@ func (p *Parser) process(args []string) error {
return fmt.Errorf("invalid subcommand: %s", arg)
}
// instantiate the field to point to a new struct
v := p.writable(subcmd.dest)
v.Set(reflect.New(v.Type().Elem())) // we already checked that all subcommands are struct pointers
// add the new options to the set of allowed options
specs = append(specs, subcmd.specs...)