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

Unknown commands '-h' and '--help' #63

Closed
kizbitz opened this issue Mar 4, 2015 · 7 comments
Closed

Unknown commands '-h' and '--help' #63

kizbitz opened this issue Mar 4, 2015 · 7 comments

Comments

@kizbitz
Copy link

kizbitz commented Mar 4, 2015

Using the default example with 'Short' & 'Long' added to rootCmd:

package main

import (
    "fmt"
    "github.com/spf13/cobra"
    "strings"
)

func main() {

    var echoTimes int

    var cmdPrint = &cobra.Command{
        Use:   "print [string to print]",
        Short: "Print anything to the screen",
        Long: `print is for printing anything back to the screen.
        For many years people have printed back to the screen.
        `,
        Run: func(cmd *cobra.Command, args []string) {
            fmt.Println("Print: " + strings.Join(args, " "))
        },
    }

    var cmdEcho = &cobra.Command{
        Use:   "echo [string to echo]",
        Short: "Echo anything to the screen",
        Long: `echo is for echoing anything back.
        Echo works a lot like print, except it has a child command.
        `,
        Run: func(cmd *cobra.Command, args []string) {
            fmt.Println("Print: " + strings.Join(args, " "))
        },
    }

    var cmdTimes = &cobra.Command{
        Use:   "times [# times] [string to echo]",
        Short: "Echo anything to the screen more times",
        Long: `echo things multiple times back to the user by providing
        a count and a string.`,
        Run: func(cmd *cobra.Command, args []string) {
            for i := 0; i < echoTimes; i++ {
                fmt.Println("Echo: " + strings.Join(args, " "))
            }
        },
    }

    cmdTimes.Flags().IntVarP(&echoTimes, "times", "t", 1, "times to echo the input")

    var rootCmd = &cobra.Command{
        Use:   "usage string",
        Short: "short string",
        Long:  "long string",
    }

    rootCmd.AddCommand(cmdPrint, cmdEcho)
    cmdEcho.AddCommand(cmdTimes)
    rootCmd.Execute()
}

Output of app and app help is correct and lists '-h' and '--help' as global flags:

jbaker: )➤ app                                                                                                                                                                 
long string

Usage:
  usage [command]

Available Commands:
  print [string to print]   Print anything to the screen
  echo [string to echo]     Echo anything to the screen
  help [command]            Help about any command


Global Flags:
  -h, --help=false: help for usage

Use "usage help [command]" for more information about a command.
jbaker: )➤ app help                                                                                                                                                                 
long string

Usage:
  usage [command]

Available Commands:
  print [string to print]   Print anything to the screen
  echo [string to echo]     Echo anything to the screen
  help [command]            Help about any command


Global Flags:
  -h, --help=false: help for usage

Use "usage help [command]" for more information about a command.

jbaker: )➤

app -h and app --help fail with 'unknown command':

jbaker: )➤ app -h                                                                                                                                                                    
Error: unknown command "-h"
Run 'help' for usage.

usage: invalid command [`-h`]
Run 'usage help' for usage
jbaker: )➤ app --help                                                                                                                                                              
Error: unknown command "--help"
Run 'help' for usage.

usage: invalid command [`--help`]
Run 'usage help' for usage
@jmoiron
Copy link
Contributor

jmoiron commented Mar 14, 2015

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.

@apriendeau
Copy link
Contributor

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

@eparis
Copy link
Collaborator

eparis commented Jul 8, 2015

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

@apriendeau
Copy link
Contributor

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.

@apriendeau
Copy link
Contributor

Nvm @eparis pebcak.

@apriendeau
Copy link
Contributor

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

@kizbitz
Copy link
Author

kizbitz commented Jul 10, 2015

Confirmed. Fixed in latest build.

Closing.

@kizbitz kizbitz closed this as completed Jul 10, 2015
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

4 participants