File: options.go

package info (click to toggle)
golang-github-francoispqt-gojay 1.2.13-5
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 1,456 kB
  • sloc: makefile: 86
file content (55 lines) | stat: -rw-r--r-- 1,320 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package codegen

import (
	"flag"
	"strings"

	"github.com/go-errors/errors"
	"github.com/viant/toolbox"
	"github.com/viant/toolbox/url"
)

type Options struct {
	Source      string
	Dest        string
	Types       []string
	PoolObjects bool
	TagName     string
	Pkg         string
}

func (o *Options) Validate() error {
	if o.Source == "" {
		return errors.New("Source was empty")
	}
	if len(o.Types) == 0 {
		return errors.New("Types was empty")
	}
	return nil
}

const (
	optionKeySource      = "s"
	optionKeyDest        = "o"
	optionKeyTypes       = "t"
	optionKeyTagName     = "a"
	optionKeyPoolObjects = "p"
	optionKeyPkg         = "pkg"
)

//NewOptionsWithFlagSet creates a new options for the supplide flagset
func NewOptionsWithFlagSet(set *flag.FlagSet) *Options {
	toolbox.Dump(set)

	var result = &Options{}
	result.Dest = set.Lookup(optionKeyDest).Value.String()
	result.Source = set.Lookup(optionKeySource).Value.String()
	result.PoolObjects = toolbox.AsBoolean(set.Lookup(optionKeyPoolObjects).Value.String())
	result.TagName = set.Lookup(optionKeyTagName).Value.String()
	result.Types = strings.Split(set.Lookup(optionKeyTypes).Value.String(), ",")
	result.Pkg = set.Lookup(optionKeyPkg).Value.String()
	if result.Source == "" {
		result.Source = url.NewResource(".").ParsedURL.Path
	}
	return result
}