File: as.go

package info (click to toggle)
golang-github-mgutz-minimist 0.0~git20151219.39eb8cf-3
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 104 kB
  • sloc: makefile: 3
file content (76 lines) | stat: -rw-r--r-- 1,460 bytes parent folder | download | duplicates (2)
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package minimist

import (
	"time"

	"github.com/mgutz/to"
)

// AsBool tries to convert any of related aliases to bool
func (am ArgMap) AsBool(aliases ...string) bool {
	for _, key := range aliases {
		b, err := to.Bool(am[key])
		if err == nil {
			return b
		}
	}
	return false
}

// AsDuration tries to convert any of related aliases to bool
func (am ArgMap) AsDuration(aliases ...string) time.Duration {
	for _, key := range aliases {
		d, err := to.Duration(am[key])
		if err == nil {
			return d
		}
	}
	return 0
}

// AsFloat should get value from path or return val.
func (am ArgMap) AsFloat(aliases ...string) float64 {
	for _, key := range aliases {
		f, err := to.Float64(am[key])
		if err == nil {
			return f
		}
	}
	return 0
}

// AsInt should get value from path or return val.
func (am ArgMap) AsInt(aliases ...string) int {
	for _, key := range aliases {
		i, err := to.Int64(am[key])
		if err == nil {
			return int(i)
		}
	}
	return 0
}

// AsString should get value from path or return val.
func (am ArgMap) AsString(aliases ...string) string {
	if len(aliases) == 0 {
		panic("Alias key(s) required")
	}
	for _, key := range aliases {
		s := to.String(am[key])
		if len(s) > 0 {
			return s
		}
	}
	return ""
}

// AsTime should get value from path or return val.
func (am ArgMap) AsTime(aliases ...string) time.Time {
	for _, key := range aliases {
		t, err := to.Time(am[key])
		if err == nil {
			return t
		}
	}
	return time.Time{}
}