-
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
Problem when an argument is empty #909
Comments
I'm not sure what you mean by this. The message However, to address your primary issue, you can use "=" to ensure that the following flag is not read accidentally in the case of an empty value. i.e:
|
Alternatively, I think you can 'fix' it by using quotes around the variable, |
Thanks for the quick replay. |
Do you mean that it fails because it does not accept an empty argument? Or that you cannot handle it because you need to tell apart a default |
It doesn't work because the flag exists but it is an empty string. So, the
|
I cannot reproduce. See: package main
import (
"fmt"
"os"
"github.com/spf13/cobra"
)
func main() {
if err := rootCmd.Execute(); err != nil {
fmt.Println(err)
os.Exit(1)
}
}
// rootCmd represents the base command when called without any subcommands
var rootCmd = &cobra.Command{
Use: "test",
Short: "A brief description of your application",
Long: `A longer description...`,
Run: func(cmd *cobra.Command, args []string) {
fmt.Println(args)
},
}
var Username string
var Password string
func init() {
rootCmd.Flags().StringVarP(&Username, "username", "u", "default", "username (required)")
rootCmd.Flags().StringVarP(&Password, "password", "p", "default", "password (required)")
rootCmd.MarkFlagRequired("username")
rootCmd.MarkFlagRequired("password")
} root@91266ab2b8e1:/src/cobra/test# go run main.go
Error: required flag(s) "password", "username" not set
root@91266ab2b8e1:/src/cobra/test# go run main.go -u user -p pass
[]
root@91266ab2b8e1:/src/cobra/test# go run main.go -u user -p pass aaa
[aaa]
root@91266ab2b8e1:/src/cobra/test# go run main.go -u -p pass aaa
Error: required flag(s) "password" not set
root@91266ab2b8e1:/src/cobra/test# go run main.go -u "" -p pass aaa
[aaa]
root@91266ab2b8e1:/src/cobra/test# go run main.go -u= -p pass aaa
[aaa]
root@91266ab2b8e1:/src/cobra/test# go run main.go -u= -p= aaa
[aaa]
root@91266ab2b8e1:/src/cobra/test# go run main.go -u= -p "" aaa
[aaa]
root@91266ab2b8e1:/src/cobra/test# go run main.go -u "" -p "" aaa
[aaa] |
@umarcor which version of cobra did you try to reproduce this with? |
Thank @umarcor for your example.
For me, if I set an argument |
This is expected behavior. It guarantees the presence of the flag, not the argument. |
You are right. My bad! |
No problem. |
I used the latest
I think that this feature request makes sense, but not as the default behaviour for Meanwhile, you can add a check in the |
Hi,
I am facing this issue. When an argument is empty, username in this case:
command --username $USERNAME --pwd $PWD
Cobra assigns
--pwd
to--username
.If both are required, the error says
required flag(s) "pwd" not set
.The message is confused, and I don't understand why this happens.
Library versions:
Regards,
Cristian.
The text was updated successfully, but these errors were encountered: