Giter Club home page Giter Club logo

Comments (13)

j16r avatar j16r commented on May 13, 2024 9

Perhaps an alternative Run command, e.g:

RunWithError: func(cmd *cobra.Command, args []string) error {

(not the best name to be sure)

from cobra.

edgard avatar edgard commented on May 13, 2024

👍 this is very welcomed. I cringe everytime I have to do error processing on my inner functions. :D

from cobra.

bep avatar bep commented on May 13, 2024

Yes, well - I don't see a way to change this without breaking a lot of user code ...

To my surprise, the "other Go CLI framework", seems to be doing exactly the same.

from cobra.

bep avatar bep commented on May 13, 2024

A little surprised to see so little interest in this issue.

@eparis what do you to handle this problem? Handle all errors inside the commands?

from cobra.

eparis avatar eparis commented on May 13, 2024

For the most part, everything I do called

if err := cmd.Execute(); if err != nil {
   os.Exit(1)
}
os.Exit(0)

And inside run we've got glog.Fatal() (which does os.Exit(1) on unhandl-able errors)

from cobra.

apriendeau avatar apriendeau commented on May 13, 2024

I would agree with this but it would be a breaking news change. We should always populate the errors up. @eparis a good use case would be testing but also populating the errors up would allow for a single error handling for parent and child commands as well.

from cobra.

pgruenbacher avatar pgruenbacher commented on May 13, 2024

Agreed, I'm using a fork for the time being. I prefer to have a single os.Exit on a single error exit point.
https://github.com/pgruenbacher/cobra

from cobra.

eparis avatar eparis commented on May 13, 2024

So I don't think we can/should change Run. Too many people are using it I'd expect. Maybe we should add a RunE or something like that? Same thing as run but which returns an error which gets populated back up through Execute() ?

from cobra.

eparis avatar eparis commented on May 13, 2024

https://gist.github.com/eparis/be6ca4c767e69fcaaea8 ?? Maybe something like that?

from cobra.

bep avatar bep commented on May 13, 2024

@eparis agree. RunE also matches the naming convention I've seen elsewhere in situation like this.

from cobra.

apriendeau avatar apriendeau commented on May 13, 2024

@eparis @bep I agree. That works if we want to use that but I think it would be better in the longer term to make that the default. Possibly a deprecation period?

from cobra.

apriendeau avatar apriendeau commented on May 13, 2024

I think that the only thing is that patch would need to have is these: PostRunE, PersistentPostRunE, PreRunE and PersistentPreRunE to achieved the full desired effect.

from cobra.

apriendeau avatar apriendeau commented on May 13, 2024

@bep @eparis I think this should be good to close with the fix from Alex, right?

from cobra.

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.