urfave / cli

A simple, fast, and fun package for building command line apps in Go

Home Page:https://cli.urfave.org

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

AfterFunc execution details

shrimalmadhur opened this issue · comments

I am using AfterFunc feature and adding it to the root command

// AfterFunc is an action to execute after any subcommands are run, but after the
// subcommand has finished it is run even if Action() panics
type AfterFunc func(*Context) error

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 the ctx.Command.FullName() gives back cmd. I am wondering if that's the expected behavior and I have to add After 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 in After function so that I can get command full name by only attaching AfterFunc to root command?

@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.