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

pass json string as flag remove double quotes #898

Closed
fprieur opened this issue Jul 4, 2019 · 4 comments
Closed

pass json string as flag remove double quotes #898

fprieur opened this issue Jul 4, 2019 · 4 comments

Comments

@fprieur
Copy link

fprieur commented Jul 4, 2019

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

@fprieur fprieur changed the title pass json string as persistent flag pass json string as flag remove double quotes Jul 4, 2019
@umarcor
Copy link
Contributor

umarcor commented Jul 4, 2019

In the following example, all four methods work as expected: printing or exporting pupConfig or RootCmd.Flag("pupconfig").Value.

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

@fprieur
Copy link
Author

fprieur commented Jul 4, 2019

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

@umarcor
Copy link
Contributor

umarcor commented Jul 4, 2019

@fprieur, in that case, I think that the issue is related to powershell, neither to cobra nor to pflag. E.g. add fmt.Println(os.Args) just before rootCmd.Execute() and try:

> 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

@fprieur
Copy link
Author

fprieur commented Jul 4, 2019

Yeah that's what I thought, the escape string was the only case that worked for me also,
Thanks again @umarcor

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

2 participants