Code Monkey home page Code Monkey logo

Comments (7)

jmoiron avatar jmoiron commented on May 18, 2024

This seems to be the case with all "global" flags, and I suspect this has actually never worked. It doesn't work for 10a8494 which introduced support for flags before commands. Basically, it looks like if there are no commands present, it never gets to the flag parsing phase. I wanted to be able to follow certain conventions wrt --help and --version, but it actually seems difficult to do with cobra.

You can workaround this in your code presently:

    rootCmd.ParseFlags(os.Args)
    rootFlags := rootCmd.Flags()
    rootFlags.Visit(func(flag *pflag.Flag) {
        if flag.Name == "help" {
            fmt.Println(rootCmd.Long)
            rootCmd.Usage()
            os.Exit(-1)
        }
    })
    rootCmd.Execute()

I thought about trying to do a PR for this but it doesn't look straightforward to fix.

Update: For whatever reason, rootCmd.Usage() won't show help in the list of available commands.

from cobra.

apriendeau avatar apriendeau commented on May 18, 2024

@eparis as of the latest commit, -h was removed from the help template output. Was this intentional or related to this?

from cobra.

eparis avatar eparis commented on May 18, 2024

it certainly possible i screwed it up and --help disappeared from the help output...

from cobra.

apriendeau avatar apriendeau commented on May 18, 2024

I just built a sample cobra for the first time, and ran into the -h doesn't work but also noticed it wasn't there so I was trying to figure out how to add it.

from cobra.

apriendeau avatar apriendeau commented on May 18, 2024

Nvm @eparis pebcak.

from cobra.

apriendeau avatar apriendeau commented on May 18, 2024

I am not seeing this issue as of the latest build.

from cobra.

kizbitz avatar kizbitz commented on May 18, 2024

Confirmed. Fixed in latest build.

Closing.

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.