-
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
pass json string as flag remove double quotes #898
Comments
In the following example, all four methods work as expected: printing or exporting package main
import (
"fmt"
"os"
"encoding/json"
"github.com/spf13/cobra"
)
var rootCmd = &cobra.Command{
Use: "hugo",
Run: func(cmd *cobra.Command, args []string) {
fmt.Printf("pupconfig content is %s\n", pupConfig)
json.NewEncoder(os.Stdout).Encode(pupConfig)
},
}
func main() {
rootCmd.Execute();
fmt.Printf("pupconfig content is %s\n", rootCmd.Flag("pupconfig").Value)
json.NewEncoder(os.Stdout).Encode(rootCmd.Flag("pupconfig").Value)
}
var pupConfig string
func init() {
rootCmd.PersistentFlags().StringVarP(&pupConfig, "pupconfig", "p", "", "Pup json configuration from storage")
} # go run main.go --pupconfig '{"flavor_name":"foo"}'
pupconfig content is {"flavor_name":"foo"}
"{\"flavor_name\":\"foo\"}"
pupconfig content is {"flavor_name":"foo"}
"{\"flavor_name\":\"foo\"}"
# go version
go version go1.12.6 linux/amd64 |
Thanks @umarcor, it does work indeed on linux but I forgot to mentioned that I was on windows box with powershell go version go1.12.5 windows/amd64 |
@fprieur, in that case, I think that the issue is related to powershell, neither to cobra nor to pflag. E.g. add > go run main.go --pupconfig '{"flavor_name":"foo"}'
[C:\Users\user\AppData\Local\Temp\go-build722346422\b001\exe\main.exe --pupconfig {flavor_name:foo}]
pupconfig content is {flavor_name:foo}
"{flavor_name:foo}"
pupconfig content is {flavor_name:foo}
"{flavor_name:foo}"
> go run main.go --pupconfig '{\"flavor_name\":\"foo\"}'
[C:\Users\user\AppData\Local\Temp\go-build567492138\b001\exe\main.exe --pupconfig {"flavor_name":"foo"}]
pupconfig content is {"flavor_name":"foo"}
"{\"flavor_name\":\"foo\"}"
pupconfig content is {"flavor_name":"foo"}
"{\"flavor_name\":\"foo\"}" > go version
go version go1.12.6 windows/amd64 |
Yeah that's what I thought, the escape string was the only case that worked for me also, |
Hi, I'm trying to pass a json string a flag
It seems like the rootCmd.PersistentFlags().StringVarP remove the double quote from flag value
command using for testing
go run main.go version --pupconfig '{"flavor_name":"foo"}'
Setting the flag at the rootcmd level
rootCmd.PersistentFlags().StringVarP(&pupConfig, "pupconfig", "p", "", "Pup json configuration from storage")
Checking the value later in the version cmd code
fmt.Printf("pupconfig content is %s", rootCmd.Flag("pupconfig").Value)
output:
pupconfig content is {flavor_name:foo}
I need the pupconfig as it was passed in the flag because i'm saving it as a json file later
The text was updated successfully, but these errors were encountered: