<commit_before>package goFlags

import (
	"fmt"
	"os"
	"reflect"
	"strconv"
	"strings"

	"flag"

	"github.com/crgimenes/goConfig/structTag"
)

type parameterMeta struct {
	Kind  reflect.Kind
	Value interface{}
	Tag   string
}

var parametersMetaMap map[*reflect.Value]parameterMeta
var visitedMap map[string]*flag.Flag
var disableFags bool

// Preserve disable default values and get only visited parameters thus preserving the values passed in the structure, default false
var Preserve bool

// Prefix is a string that would be placed at the beginning of the generated tags.
var Prefix string

//Usage is a function to show the help, can be replaced by your own version.
var Usage func()

// Setup maps and variables
func Setup(tag string, tagDefault string) {
	Usage = DefaultUsage
	parametersMetaMap = make(map[*reflect.Value]parameterMeta)
	visitedMap = make(map[string]*flag.Flag)

	structTag.Setup()
	structTag.Prefix = Prefix
	SetTag(tag)
	SetTagDefault(tagDefault)

	structTag.ParseMap[reflect.Int] = reflectInt
	structTag.ParseMap[reflect.Float64] = reflectFloat
	structTag.ParseMap[reflect.String] = reflectString
	structTag.ParseMap[reflect.Bool] = reflectBool
}

// SetTag set a new tag
func SetTag(tag string) {
	structTag.Tag = tag
}

// SetTagDefault set a new TagDefault to retorn default values
func SetTagDefault(tag string) {
	structTag.TagDefault = tag
}

// Parse configuration
func Parse(config interface{}) (err error) {
	if disableFags {
		return
	}

	flag.Usage = Usage
	err = structTag.Parse(config, "")
	if err != nil {
		return
	}

	flag.Parse()

	flag.Visit(loadVisit)

	for k, v := range parametersMetaMap {
		if _, ok := visitedMap[v.Tag]; !ok && Preserve {
			continue
		}
		switch v.Kind {
		case reflect.String:
			value := *v.Value.(*string)
			//fmt.Printf("Parse %v = \"%v\"\n", v.Tag, value)
			k.SetString(value)
		case reflect.Int:
			value := *v.Value.(*int)
			//fmt.Printf("Parse %v = \"%v\"\n", v.Tag, value)
			k.SetInt(int64(value))
		case reflect.Float64:
			value := *v.Value.(*float64)
			k.SetFloat(float64(value))
		case reflect.Bool:
			value := *v.Value.(*bool)
			//fmt.Printf("Parse %v = \"%v\"\n", v.Tag, value)
			k.SetBool(value)

		}
	}

	disableFags = true
	return
}

// Reset maps caling setup function
func Reset() {
	disableFags = false
	flag.CommandLine = flag.NewFlagSet(os.Args[0], flag.ContinueOnError)
	flag.Usage = nil

	structTag.Reset()
	Setup(structTag.Tag, structTag.TagDefault)
}

func loadVisit(f *flag.Flag) {
	visitedMap[f.Name] = f<commit_msg>remove debug printf and unnecessary conversion<commit_after>package goFlags

import (
	"fmt"
	"os"
	"reflect"
	"strconv"
	"strings"

	"flag"

	"github.com/crgimenes/goConfig/structTag"
)

type parameterMeta struct {
	Kind  reflect.Kind
	Value interface{}
	Tag   string
}

var parametersMetaMap map[*reflect.Value]parameterMeta
var visitedMap map[string]*flag.Flag
var disableFags bool

// Preserve disable default values and get only visited parameters thus preserving the values passed in the structure, default false
var Preserve bool

// Prefix is a string that would be placed at the beginning of the generated tags.
var Prefix string

//Usage is a function to show the help, can be replaced by your own version.
var Usage func()

// Setup maps and variables
func Setup(tag string, tagDefault string) {
	Usage = DefaultUsage
	parametersMetaMap = make(map[*reflect.Value]parameterMeta)
	visitedMap = make(map[string]*flag.Flag)

	structTag.Setup()
	structTag.Prefix = Prefix
	SetTag(tag)
	SetTagDefault(tagDefault)

	structTag.ParseMap[reflect.Int] = reflectInt
	structTag.ParseMap[reflect.Float64] = reflectFloat
	structTag.ParseMap[reflect.String] = reflectString
	structTag.ParseMap[reflect.Bool] = reflectBool
}

// SetTag set a new tag
func SetTag(tag string) {
	structTag.Tag = tag
}

// SetTagDefault set a new TagDefault to retorn default values
func SetTagDefault(tag string) {
	structTag.TagDefault = tag
}

// Parse configuration
func Parse(config interface{}) (err error) {
	if disableFags {
		return
	}

	flag.Usage = Usage
	err = structTag.Parse(config, "")
	if err != nil {
		return
	}

	flag.Parse()

	flag.Visit(loadVisit)

	for k, v := range parametersMetaMap {
		if _, ok := visitedMap[v.Tag]; !ok && Preserve {
			continue
		}

		switch v.Kind {
		case reflect.String:
			value := *v.Value.(*string)
			k.SetString(value)
		case reflect.Int:
			value := *v.Value.(*int)
			k.SetInt(int64(value))
		case reflect.Float64:
			value := *v.Value.(*float64)
			k.SetFloat(value)
		case reflect.Bool:
			value := *v.Value.(*bool)
			k.SetBool(value)
		}
	}

	disableFags = true
	return
}

// Reset maps caling setup function
func Reset() {
	disableFags = false
	flag.CommandLine = flag.NewFlagSet(os.Args[0], flag.ContinueOnError)
	flag.Usage = nil

	structTag.Reset()
	Setup(structTag.Tag, structTag.TagDefault)
}

func loadVisit(f *flag.Flag) {
	visitedMap[f.Name] = f
{'fraction_non_alphanumeric': 0.11616058691664968, 'fraction_numerical': 0.004075810067250866, 'mean_word_length': 4.049382716049383, 'pattern_counts': {'":': 0, '<': 3, '<?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': '1731666', 'n_tokens_mistral': 1748, 'n_tokens_neox': 1639, 'n_words': 451}