Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

--help flag does not work #550

Closed
JoakimSoderberg opened this issue Oct 14, 2017 · 2 comments
Closed

--help flag does not work #550

JoakimSoderberg opened this issue Oct 14, 2017 · 2 comments

Comments

@JoakimSoderberg
Copy link

package main

import (
	"log"
	"github.com/spf13/cobra"
)

func main() {
	var rootCmd = &cobra.Command{Use: "siknas-skylt"}
	rootCmd.Flags().String("port", "8080", "The port the webserver should listen on")

	if err := rootCmd.Execute(); err != nil {
		log.Fatalln(err)
	}
	
	log.Println("Hello")
}

As I understand it by doing this I should get a --help flag automatically added.

Running with no flags

/go/src/bla # go-wrapper run
+ exec bla
2017/10/14 23:38:22 Hello

With --help flag

As you can see there is no help output and "Hello" is printed.

/go/src/bla # go-wrapper run --help
+ exec bla --help
2017/10/14 23:38:25 Hello

Invalid flag shows help

If I misspell help or try some other incorrect command I get the help output, which clearly states there's supposed to be a --help.

/go/src/bla # go-wrapper run --hel
+ exec bla --hel
Error: unknown flag: --hel
Usage:

Flags:
  -h, --help          help for siknas-skylt
      --port string   The port the webserver should listen on (default "8080")

2017/10/14 23:38:28 unknown flag: --hel

Am I missing something obvious or is this a bug?

@n10v
Copy link
Collaborator

n10v commented Oct 15, 2017

It's because you're missing Run field in your command. It will work:

var rootCmd = &cobra.Command{Use: "siknas-skylt", Run: func(c *cobra.Command, args []string){}}

@JoakimSoderberg
Copy link
Author

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants