Skip to content

How to set up a cli.StringMapFlag of Slices (v3) #2162

@aristosvo

Description

@aristosvo

My question is how to set up a string map of a string slice.

Input: --cidrs "dev=10.0.0.0/8,192.168.1.1/32" --ip-list "prod=10.1.0.0/8"

Currently configured like this:

&cli.StringMapFlag{
	Name:  "cidrs",
	Value: map[string]string{},
	
	Validator: func(m map[string]string) error {
		for _, value := range m {
			cidrs := strings.Split(value, ",")
			if len(cidrs) < 1 {
				return fmt.Errorf("expected at least on cidr")
			}
			for _, cidr := range cidrs {
				_, _, err := net.ParseCIDR(cidr)
				if err != nil {
					return fmt.Errorf("%w, parsing at least one of the entered CIDR was unsuccessful, expect format per CIDR of '<IP>/<suffix>'", err)
				}
			}
		}

		return nil
	},
},

This results now in this error: Incorrect Usage: invalid value "dev=10.0.0.0/8,192.168.1.1/32" for flag -cidrs: item "192.168.1.1/32" is missing separator "="

Metadata

Metadata

Assignees

No one assigned

    Labels

    area/v3relates to / is being considered for v3kind/questionsomeone asking a questionstatus/triagemaintainers still need to look into this

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions