Update CLI to use internal flags implementation

This commit is contained in:
Chris Roberts 2022-01-28 14:47:18 -08:00 committed by Paul Hinze
parent 05facc0035
commit c010ae0429
No known key found for this signature in database
GPG Key ID: B69DEDF2D55501C0
2 changed files with 32 additions and 35 deletions

View File

@ -9,7 +9,6 @@ import (
"strconv" "strconv"
"strings" "strings"
"github.com/DavidGamba/go-getoptions"
"github.com/hashicorp/go-hclog" "github.com/hashicorp/go-hclog"
"github.com/hashicorp/go-multierror" "github.com/hashicorp/go-multierror"
@ -21,6 +20,7 @@ import (
clientpkg "github.com/hashicorp/vagrant/internal/client" clientpkg "github.com/hashicorp/vagrant/internal/client"
"github.com/hashicorp/vagrant/internal/clierrors" "github.com/hashicorp/vagrant/internal/clierrors"
"github.com/hashicorp/vagrant/internal/config" "github.com/hashicorp/vagrant/internal/config"
"github.com/hashicorp/vagrant/internal/flags"
"github.com/hashicorp/vagrant/internal/server/proto/vagrant_server" "github.com/hashicorp/vagrant/internal/server/proto/vagrant_server"
"github.com/hashicorp/vagrant/internal/serverclient" "github.com/hashicorp/vagrant/internal/serverclient"
) )
@ -446,68 +446,64 @@ func (c *baseCommand) flagSet(bit flagSetBit, f func([]*component.CommandFlag) [
} }
func (c *baseCommand) Parse( func (c *baseCommand) Parse(
flags []*component.CommandFlag, set []*component.CommandFlag,
args []string, args []string,
passThrough bool, passThrough bool,
) ([]string, error) { ) ([]string, error) {
opt := c.generateCliFlags(flags) fset := c.generateCliFlags(set)
if passThrough { if passThrough {
opt.SetUnknownMode(getoptions.Pass) flags.SetUnknownMode(flags.PassOnUnknown)(fset)
} else {
opt.SetUnknownMode(getoptions.Fail)
} }
opt.SetMode(getoptions.Bundling)
c.Log.Warn("parsing arguments with flags", "args", args, "flags", flags) c.Log.Warn("parsing arguments with flags", "args", args, "flags", set)
remainArgs, err := opt.Parse(args) remainArgs, err := fset.Parse(args)
if err != nil { if err != nil {
return nil, err return nil, err
} }
for _, f := range flags { for _, f := range set {
if called := opt.Called(f.LongName); !called { pf, err := fset.Flag(f.LongName)
c.Log.Error("flag was not called", "name", f.LongName) if err != nil {
return nil, err
}
if !pf.Updated() {
continue continue
} }
c.Log.Warn("flag was called", "name", f.LongName) c.flagData[f] = pf.Value()
if f.Type == component.FlagString {
c.flagData[f] = opt.Value(f.LongName)
continue
}
val := true
if strings.HasPrefix(opt.CalledAs(f.LongName), "no-") {
val = false
}
c.flagData[f] = val
} }
return remainArgs, nil return remainArgs, nil
} }
func (c *baseCommand) generateCliFlags(set []*component.CommandFlag) *getoptions.GetOpt { func (c *baseCommand) generateCliFlags(set []*component.CommandFlag) *flags.Set {
opt := getoptions.New() fs := flags.NewSet("flags",
opt.SetUnknownMode(getoptions.Pass) // TODO: make this configurable flags.SetErrorMode(flags.ReturnOnError),
flags.SetUnknownMode(flags.ErrorOnUnknown),
)
for _, f := range set { for _, f := range set {
opts := []getoptions.ModifyFn{} opts := []flags.FlagModifier{}
if f.Description != "" { if f.Description != "" {
opts = append(opts, opt.Description(f.Description)) opts = append(opts, flags.Description(f.Description))
}
if f.ShortName != "" {
opts = append(opts, flags.ShortName(rune(f.ShortName[0])))
} }
// if f.ShortName != "" {
// opts = append(opts, opt.Alias(f.ShortName))
// }
switch f.Type { switch f.Type {
case component.FlagBool: case component.FlagBool:
opts = append(opts, opt.Alias("no-"+f.LongName))
b, _ := strconv.ParseBool(f.DefaultValue) b, _ := strconv.ParseBool(f.DefaultValue)
opt.Bool(f.LongName, b, opts...) opts = append(opts, flags.DefaultValue(b))
fs.DefaultGroup().Bool(f.LongName, opts...)
case component.FlagString: case component.FlagString:
opt.String(f.LongName, f.DefaultValue, opts...) if f.DefaultValue != "" {
opts = append(opts, flags.DefaultValue(f.DefaultValue))
}
fs.DefaultGroup().String(f.LongName, opts...)
} }
} }
return opt return fs
} }
// flagSetBit is used with baseCommand.flagSet // flagSetBit is used with baseCommand.flagSet

View File

@ -124,7 +124,8 @@ func (c *DynamicCommand) Synopsis() string {
} }
func (c *DynamicCommand) Help() string { func (c *DynamicCommand) Help() string {
return formatHelp(fmt.Sprintf("%s\n%s\n", c.help, c.Flags().Display())) fset := c.generateCliFlags(c.Flags())
return formatHelp(fmt.Sprintf("%s\n%s\n", c.help, fset.Display()))
} }
func (c *DynamicCommand) Flags() component.CommandFlags { func (c *DynamicCommand) Flags() component.CommandFlags {