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

Return nil error in validateRequiredFlags when help command #846

Conversation

serbixote
Copy link

Hi guys!

When setting required flags to the root commands:

rootCmd.MarkPersistentFlagRequired("user")

seems that the help command throws an error before showing the help when those flags are not set:

$ ./cli help
Error: required flag(s) "user" not set
...

It does not make sense to me that required flags have to be validated when calling the help command. I do not know whether this would have more impact than skipping the validation.

In any case, thanks for your time!

@CLAassistant
Copy link

CLAassistant commented Mar 28, 2019

CLA assistant check
All committers have signed the CLA.

@umarcor
Copy link
Contributor

umarcor commented Mar 30, 2019

This should not be required, because 'help' is checked before calling validateRequiredFlags (https://github.com/spf13/cobra/blob/master/command.go#L704) so validateRequiredFlags should never be called. I.e., it should not be required to patch validateRequiredFlags, because it should not apply in this context. Maybe the error is produced in ParseFlags? https://github.com/spf13/cobra/blob/master/command.go#L689

@serbixote
Copy link
Author

@umarcor As you can see, that only checks whether the help flag, not the help command. I'm not sure whether this should be checked anywhere else before calling validateRequiredFlags, but I'm pretty sure that validateRequiredFlags is currently being called when using the help command 😬

@umarcor
Copy link
Contributor

umarcor commented Mar 31, 2019

@umarcor As you can see, that only checks whether the help flag, not the help command.

I was precisely suggesting that the command should be checked too.

I'm not sure whether this should be checked anywhere else before calling validateRequiredFlags, but I'm pretty sure that validateRequiredFlags is currently being called when using the help command 😬

I think it should be checked somewhere between https://github.com/spf13/cobra/blob/master/command.go#L689-L703, even though it is not done now, as you say. validateRequiredFlags should not be executed at all if no flag is to be considered because it is a 'help' command.

@github-actions
Copy link

github-actions bot commented Apr 6, 2020

This PR is being marked as stale due to a long period of inactivity

@dzciemix
Copy link

Hi!

I think I still expirance this bug.

package main

import (
	"log"

	"github.com/spf13/cobra"
)

var RootCmd = &cobra.Command{
	Use: "program",
	SilenceUsage:  true,
	SilenceErrors: true,
}

var createCmd = &cobra.Command{
	Use:  "create",
	Args: cobra.ExactArgs(1),
	RunE: func(cmd *cobra.Command, args []string) error {
		return nil
	},
}

func main() {
	RootCmd.PersistentFlags().StringP("url", "u", "", "Server address")
	RootCmd.MarkPersistentFlagRequired("url")
	RootCmd.ValidateRequiredFlags()

	RootCmd.AddCommand(createCmd)

	err := RootCmd.Execute()
	if err != nil {
		log.Fatalf("Error!\n%v", err)
	}
}
$ go run . help 
2024/06/25 12:12:34 Error!
required flag(s) "url" not set
exit status 1

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

Successfully merging this pull request may close these issues.

4 participants