args

package
v0.11.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 2, 2024 License: Apache-2.0 Imports: 5 Imported by: 0

Documentation

Overview

Package args stores useful methods to parse and extract data from extra arguments given to cli commands

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetArrayValues

func GetArrayValues(ctx context.Context, args []string, flag string) (values []string, ok bool)

GetArrayValues returns a list of values for a one flag in a set of given arguments args should be all the arguments to be interpreted. i.e ["--arg1","key1=value1","key2=value2", "--arg2", "key3=value3"] flag should be the flag name to select keys and values from, i.e arg1 or arg2 in the example above.

Note: When the value is empty, it will be removed.

 i.e: flag: arg1
	  args: ["--arg1", "", "key1=value1", "--arg2", "key3=value3"]
      return: ["key1=value1"]

func GetKeyValues

func GetKeyValues(ctx context.Context, args []string, flag string,
	opts ...ValuesValidateOption) (keyValues map[string]string,
	err error)

GetKeyValues returns the set of keys and values for one flag in a set of given arguments args should be all the arguments to be interpreted. i.e --arg1 key1=value1 key2=value2 --arg2 key3=value3 flag should be the flag name to select keys and values from, i.e arg1 or arg2 in the example above

Types

type ValuesValidateOption

type ValuesValidateOption func(values []string) error

ValuesValidateOption provides extensive func to set validation rules of GetKeyValues

var ValuesValidationOptDuplicatedKeys ValuesValidateOption = func(pairs []string) error {
	var duplicatedKeys, invalidPairs []string
	keyCount := map[string]int{}

	for _, pair := range pairs {
		split := strings.SplitN(pair, "=", 2)
		if len(split) != 2 && !slices.Contains(invalidPairs, pair) {
			invalidPairs = append(invalidPairs, pair)
			continue
		}
		pairKey := split[0]
		keyCount[pairKey]++
		if keyCount[pairKey] == 2 {
			duplicatedKeys = append(duplicatedKeys, pairKey)
		}
	}

	if len(duplicatedKeys) == 0 && len(invalidPairs) == 0 {
		return nil
	}

	var duplicatedKeysErrMsg, invalidPairsErrMsg string
	if len(duplicatedKeys) > 0 {
		duplicatedKeysErrMsg = fmt.Sprintf("duplicated env keys: %s", duplicatedKeys)
	}

	if len(invalidPairs) > 0 {
		invalidPairsErrMsg = fmt.Sprintf("invalid env items: %s", invalidPairs)
	}
	return fmt.Errorf("invalid env-flags: %s", strings.Trim(strings.Join([]string{duplicatedKeysErrMsg,
		invalidPairsErrMsg},
		","), ","))
}

ValuesValidationOptDuplicatedKeys is the builtin option for validating values are unique key=value format

var ValuesValidationOptRequired ValuesValidateOption = func(values []string) error {
	if len(values) == 0 {
		return fmt.Errorf("empty values")
	}
	return nil
}

ValuesValidationOptRequired is the builtin option for validating values are not empty

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL