mirror of
https://github.com/spf13/cobra
synced 2025-05-05 12:57:22 +00:00
update command.go
This commit is contained in:
parent
d2e2d77694
commit
aa5b1bc599
1 changed files with 8 additions and 11 deletions
19
command.go
19
command.go
|
@ -567,13 +567,13 @@ func shortHasNoOptDefVal(name string, fs *flag.FlagSet) bool {
|
||||||
return flag.NoOptDefVal != ""
|
return flag.NoOptDefVal != ""
|
||||||
}
|
}
|
||||||
|
|
||||||
func splitFirstWord(args []string, c *Command) (beforeArgs []string, firstWord string, afterArgs []string) {
|
func splitFirstWord(args []string, c *Command) (beforeArgs []string, firstWord *string, afterArgs []string) {
|
||||||
if len(args) == 0 {
|
if len(args) == 0 {
|
||||||
return args, "", nil
|
return args, nil, nil
|
||||||
}
|
}
|
||||||
c.mergePersistentFlags()
|
c.mergePersistentFlags()
|
||||||
|
|
||||||
command := ""
|
var command *string = nil
|
||||||
flags := c.Flags()
|
flags := c.Flags()
|
||||||
|
|
||||||
Loop:
|
Loop:
|
||||||
|
@ -609,7 +609,7 @@ Loop:
|
||||||
beforeArgs = append(beforeArgs, s)
|
beforeArgs = append(beforeArgs, s)
|
||||||
continue
|
continue
|
||||||
case s != "" && !strings.HasPrefix(s, "-"):
|
case s != "" && !strings.HasPrefix(s, "-"):
|
||||||
command = s
|
command = &s
|
||||||
afterArgs = args
|
afterArgs = args
|
||||||
break Loop
|
break Loop
|
||||||
}
|
}
|
||||||
|
@ -708,11 +708,11 @@ func (c *Command) findAndParseFlag(args []string) (*Command, error) {
|
||||||
|
|
||||||
innerfind = func(c *Command, innerArgs []string) (*Command, []string) {
|
innerfind = func(c *Command, innerArgs []string) (*Command, []string) {
|
||||||
beforeFlags, nextSubCmd, afterArgs := splitFirstWord(innerArgs, c)
|
beforeFlags, nextSubCmd, afterArgs := splitFirstWord(innerArgs, c)
|
||||||
if nextSubCmd == "" {
|
if nextSubCmd == nil {
|
||||||
c.ParseFlags(beforeFlags)
|
c.ParseFlags(innerArgs)
|
||||||
return c, innerArgs
|
return c, innerArgs
|
||||||
}
|
}
|
||||||
cmd := c.findNext(nextSubCmd)
|
cmd := c.findNext(*nextSubCmd)
|
||||||
|
|
||||||
if cmd != nil {
|
if cmd != nil {
|
||||||
// initialize help and version flag at the last point possible to allow for user
|
// initialize help and version flag at the last point possible to allow for user
|
||||||
|
@ -917,10 +917,6 @@ func (c *Command) execute(a []string) (err error) {
|
||||||
c.preRun()
|
c.preRun()
|
||||||
|
|
||||||
argWoFlags := c.Flags().Args()
|
argWoFlags := c.Flags().Args()
|
||||||
if c.DisableFlagParsing {
|
|
||||||
argWoFlags = a
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := c.ValidateArgs(argWoFlags); err != nil {
|
if err := c.ValidateArgs(argWoFlags); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -1772,6 +1768,7 @@ func (c *Command) parsePersistentFlags(args []string) error {
|
||||||
// ParseFlags parses persistent flag tree and local flags.
|
// ParseFlags parses persistent flag tree and local flags.
|
||||||
func (c *Command) ParseFlags(args []string) error {
|
func (c *Command) ParseFlags(args []string) error {
|
||||||
if c.DisableFlagParsing {
|
if c.DisableFlagParsing {
|
||||||
|
c.Flags().SetArgs(args)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue