Skip to content

cobrafy confused by JSON attributes #19

Open
@ryanschneider

Description

@ryanschneider

I haven't used base commandeer yet, but the wanted to try using cobrafy to reuse some structs we use for JSON parsing as arguments for a sub command we shell out to.

However, it seems like the generated Cobra.Command is confused by the omitempty on the json attribute.

See for example: https://go.dev/play/p/dTj_Fxo63sr

package main

import (
	"fmt"

	"github.com/jaffee/commandeer/cobrafy"
)

type Args struct {
	Count      int  `json:"count,omitempty"`
	Reticulate bool `json:"reticulate,omitempty"`
}

func main() {
	args := Args{
		Count:      1,
		Reticulate: false,
	}
	cmd, err := cobrafy.Command(&args)
	if err != nil {
		panic(err)
	}

	fmt.Println("Trying --count ... ")
	cmd.SetArgs([]string{"--count"})
	if err := cmd.Execute(); err != nil {
		fmt.Println(err)
	}

	fmt.Println("Trying --reticulate,omitempty ... ")
	cmd.SetArgs([]string{"--reticulate,omitempty"})
	if err := cmd.Execute(); err != nil {
		fmt.Println(err)
	} else {
		fmt.Println("Accepted")
	}
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions