Giter Site home page Giter Site logo

Comments (7)

dearchap avatar dearchap commented on July 22, 2024

@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

from cli.

shrimalmadhur avatar shrimalmadhur commented on July 22, 2024

@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

from cli.

dearchap avatar dearchap commented on July 22, 2024

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

from cli.

shrimalmadhur avatar shrimalmadhur commented on July 22, 2024

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

from cli.

shrimalmadhur avatar shrimalmadhur commented on July 22, 2024

ok c.Command.HelpName actually gives the full command I need. I can wait for v3 to get released for using the FullName() feature.

from cli.

dearchap avatar dearchap commented on July 22, 2024

@shrimalmadhur you can start using v3, It is almost production ready.

from cli.

shrimalmadhur avatar shrimalmadhur commented on July 22, 2024

@dearchap oh awesome. excited for using v3. Thanks for the update, I will close this issue now.

from cli.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.