You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
In cli/args/args.go, The current implementation of the code does not correctly handle nested subcommands and fails to remove the command name from the arguments, leading to potential issues in command parsing. This issue aims to address these problems and improve the overall code quality.
Steps to Reproduce:
Create a scenario with nested subcommands, where you have multiple levels of subcommands.
Execute commands that involve nested subcommands and observe the behavior.
Expected Behavior:
Nested subcommands should be handled correctly, with the command name removed from the arguments.
The sequence of parsing subcommands and processing arguments should be correct.
Actual Behavior:
Nested subcommands are not handled correctly.
The command name is not removed from the arguments, leading to incorrect parsing.
Proposed Changes:
Update the parseCommandArguments function to use the new helper function for finding the index of a command or its alias in the arguments.
Revise the MovePostFixCommands function to correctly handle nested subcommands and ensure the command name is removed from the arguments.
In a very rough draft, not intended to be perfect, here is how I believe parseCommandArguments() could be improved:
funcparseCommandArguments(commands []*cli.Command, args...string) []string {
iflen(args) <=1||len(commands) ==0 {
returnargs
}
// Helper function to find the index of a command or its alias in argsfindCommandIndex:=func(cmd*cli.Command) int {
for_, name:=rangeappend([]string{cmd.Name}, cmd.Aliases...) {
idx:=IndexOf(args, name)
ifidx!=-1 {
returnidx
}
}
return-1
}
// Iterate through commandsfor_, cmd:=rangecommands {
idx:=findCommandIndex(cmd)
ifidx!=-1 {
// Parse command flags and argumentscmdArgs:=append([]string{cmd.Name}, args[idx+1:]...)
cmdArgs=append(cmdArgs, MovePostFixCommands(args[:idx], cmd.Subcommands)...)
args=cmdArgsreturnargs
}
}
returnargs
}
and below is how I believe, on a very rough level, again, not intended to be perfect, how we would potentially improve MovePostFixCommands():
// MovePostFixCommands places the command being called at the beginning of the args slice for parsingfuncMovePostFixCommands(args []string, commands []*cli.Command) []string {
cmdArgs:= []string{}
cmdName:=""// Find which command is being calledfor_, cmd:=rangecommands {
argName:=cmd.Nameidx:=IndexOf(args, argName)
ifidx==-1 {
for_, alias:=rangecmd.Aliases {
idx=IndexOf(args, alias)
ifidx!=-1 {
argName=aliasbreak
}
}
}
ifidx!=-1 {
// Set the command namecmdName=argName// Remove argName from argsargs=append(args[:idx], args[idx+1:]...)
// Recursively handle subcommandsargs=MovePostFixCommands(args, cmd.Subcommands)
break
}
}
ifcmdName!="" {
cmdArgs=append(cmdArgs, cmdName)
}
returnappend(cmdArgs, args...)
}
Obviously, this is just a rough draft, and I'm sure there are better ways to implement this. I'm also not sure if this is the best way to handle nested subcommands, but I believe it is a step in the right direction.
Additionally, the ParseArguments() function would remain unchanged.
The text was updated successfully, but these errors were encountered:
Issue Description:
In
cli/args/args.go
, The current implementation of the code does not correctly handle nested subcommands and fails to remove the command name from the arguments, leading to potential issues in command parsing. This issue aims to address these problems and improve the overall code quality.Steps to Reproduce:
Expected Behavior:
Actual Behavior:
Proposed Changes:
In a very rough draft, not intended to be perfect, here is how I believe
parseCommandArguments()
could be improved:and below is how I believe, on a very rough level, again, not intended to be perfect, how we would potentially improve
MovePostFixCommands()
:Obviously, this is just a rough draft, and I'm sure there are better ways to implement this. I'm also not sure if this is the best way to handle nested subcommands, but I believe it is a step in the right direction.
Additionally, the
ParseArguments()
function would remain unchanged.The text was updated successfully, but these errors were encountered: