-
Notifications
You must be signed in to change notification settings - Fork 2.9k
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
rootCmd MarkPersistentFlagRequired does not raise error #747
Comments
I'm facing the same issue. |
I came across this issue in one of my CLI tools built with cobra in which I have a command for which I have defined some flags which I marked as required, which works fine as long as I define the flag with Flags().StringP(...) but does not work if I use Flags().StringVarP(...) so I think it does not relate to the flags being persistent. Code to replicate the issue:
The problem is, that the command executes without printing any error, unlike how it does when defining flags with Flags().StringP(). This works just fine:
|
This issue is being marked as stale due to a long period of inactivity |
I'm really surprised this code below doesn't work either:
Error not raise if in rootCmd. Any thoughts are this are welcomed. |
I'm having the same problem with: package cmd
import (
"fmt"
"github.com/spf13/cobra"
)
var (
// Used for flags.
Region string
Test string
rootCmd = &cobra.Command{
Use: "cli-test",
}
)
// Execute executes the root command.
func Execute() {
fmt.Println(Test)
}
func init() {
rootCmd.PersistentFlags().StringVarP(&Region, "region", "r", "", "region (required)")
rootCmd.PersistentFlags().StringVarP(&Test, "test", "t", "", "Test flag")
rootCmd.MarkPersistentFlagRequired("region")
} If I call my CLI without |
I'm facing the same issue mentioned here, however I feel this is not a problem with Cobra and root commands but an interpretation maybe. rootcmd.PersistentFlags().String("config-file", "", "The config file that contains the information to add the entry")
rootcmd.MarkPersistentFlagRequired("config-file") If we add some extra traces we will see that a config file wasn't found: 2023-01-16T11:04:00.810-0600 [ERROR] mycmd: error adding an entry: open : no such file or directory Previous output is actually including the empty string like |
Using spf13/Cobra for cli flag parsing.
root command has a field marked required:
However, cobra does not raise an error if it's the root command.
If I add a subcommand and add a required field, .MarkFlagRequired raises an error as expected if the argument is not provided on the command line.
The text was updated successfully, but these errors were encountered: