Closed shrimalmadhur closed 4 months ago
@shrimalmadhur The AfterFunc will be run only once after the subcommand actions have been completed. So in your case of cmd operation1 operation2
the AfterFunc defined for "cmd" will be executed once in the same command context after actions for operation1, operation2 have been completed. I am not entirely clear what you want to achieve. If you want a single function to handle all your actions you could attach the same function to all the Actions/After/Before for each of the subcommands individually. There is no way for the cli library to do this automatically for you
@dearchap thanks for the answer. my detailed case is I want to send telemetry data to an endpoint after any command gets executed - I need the whole command path while sending telemetry.
From what you are saying every I add a new command I will have to mention that function in After
field. The thing which I wanted to achieve is that I just define that in root
command and since it gets executed every time - the context would have that information.
But I guess I understand that the context is very specific to each command for AfterFunc
Yes that's correct. If you pass in the same function in all the places the context for the command will be passed to you
I see but interestingly I found an issue in v2.27.2
// FullName returns the full name of the command.
// For subcommands this ensures that parent commands are part of the command path
func (c *Command) FullName() string {
if c.commandNamePath == nil {
return c.Name
}
return strings.Join(c.commandNamePath, " ")
}
This always returns only the leaf command path but it's supposed to return whole command path. I don't see c.commandNamePath
set somewhere.
In the latest master branch, the code is
// FullName returns the full name of the command.
// For commands with parents this ensures that the parent commands
// are part of the command path.
func (cmd *Command) FullName() string {
namePath := []string{}
if cmd.parent != nil {
namePath = append(namePath, cmd.parent.FullName())
}
return strings.Join(append(namePath, cmd.Name), " ")
}
This will insure parent command path is also included
ok c.Command.HelpName
actually gives the full command I need. I can wait for v3 to get released for using the FullName()
feature.
@shrimalmadhur you can start using v3, It is almost production ready.
@dearchap oh awesome. excited for using v3. Thanks for the update, I will close this issue now.
I am using AfterFunc feature and adding it to the root command
According to documentation it will execute after every sub command too. The issue I'm facing is that the
*Context
doesn't have knowledge of full command.For example if my command is
cmd opreration1 operatione
thectx.Command.FullName()
gives backcmd
. I am wondering if that's the expected behavior and I have to addAfter
in all the leaf commands to get that. If that's the expected behavior, my question is, why not have the current subcommand execution context inAfter
function so that I can get command full name by only attachingAfterFunc
to root command?