File: tristate.go

package info (click to toggle)
termshark 2.2.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 1,800 kB
  • sloc: sh: 98; makefile: 9
file content (45 lines) | stat: -rw-r--r-- 888 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
// Copyright 2019-2021 Graham Clark. All rights reserved.  Use of this source
// code is governed by the MIT license that can be found in the LICENSE
// file.
//

package cli

//======================================================================

type TriState struct {
	Set bool
	Val bool
}

func (b *TriState) UnmarshalFlag(value string) error {
	switch value {
	case "true", "TRUE", "t", "T", "1", "y", "Y", "yes", "Yes", "YES":
		b.Set = true
		b.Val = true
	case "false", "FALSE", "f", "F", "0", "n", "N", "no", "No", "NO":
		b.Set = true
		b.Val = false
	default:
		b.Set = false
	}
	return nil
}

func (b TriState) MarshalFlag() string {
	if b.Set {
		if b.Val {
			return "true"
		} else {
			return "false"
		}
	} else {
		return "unset"
	}
}

//======================================================================
// Local Variables:
// mode: Go
// fill-column: 78
// End: