<commit_before>package env

import "fmt"

func FlattenArguments(argMap map[string]string) []string {
	args := []string{}

	for flag, value := range argMap {
		argument := fmt.Sprintf(fmt.Sprintf("--%v=%v", flag, value))

		args = append(args, argument)
	}

	return args
}<commit_msg>Use reflection to flatten flag tags<commit_after>package env

import (
	"errors"
	"fmt"
	"reflect"
	"strings"
)

var (
	ErrFlagTagNotFound = errors.New("arg: given field doesn't have a `flag` tag")
)

// Uses reflection to retrieve the `flag` tag of a field.
// The value of the `flag` field with the value of the field is
// used to construct a POSIX long flag argument string.
func GetLongFlagFormOfField(fieldValue reflect.Value, fieldType reflect.StructField) (string, error) {
	flagTag := fieldType.Tag.Get("flag")
	if flagTag == "" {
		return "", ErrFlagTagNotFound
	}

	switch fieldValue.Kind() {
	case reflect.Bool:
		return fmt.Sprintf("--%v", flagTag), nil
	case reflect.Array:
		fallthrough
	case reflect.Slice:
		args := make([]string, 0)
		for i := 0; i < fieldValue.Len(); i++ {
			args = append(args, fieldValue.Index(i).String())
		}

		return fmt.Sprintf("--%v=%v", flagTag, strings.Join(args, ",")), nil
	default:
		return fmt.Sprintf("--%v=%v", flagTag, fieldValue.String()), nil
	}
}

// Uses reflection to transform a struct containing fields with `flag` tags
// to a string slice of POSIX compliant long form arguments.
func GetArgumentFormOfStruct(strt interface{}) (flags []string) {
	numberOfFields := reflect.ValueOf(strt).NumField()
	for i := 0; i < numberOfFields; i++ {
		fieldValue := reflect.ValueOf(strt).Field(i)
		fieldType := reflect.TypeOf(strt).Field(i)

		if fieldValue.String() == "" {
			continue
		}

		flagFormOfField, err := GetLongFlagFormOfField(fieldValue, fieldType)
		if err != nil {
			continue
		}

		flags = append(flags, flagFormOfField)
	}

	return
}
{'fraction_non_alphanumeric': 0.14262990455991517, 'fraction_numerical': 0.0015906680805938495, 'mean_word_length': 4.227146814404432, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5077963', 'n_tokens_mistral': 655, 'n_tokens_neox': 613, 'n_words': 180}