Skip to content

Commit c46ab6e

Browse files
committed
assert has own cmd flag
1 parent 7e4bae2 commit c46ab6e

File tree

2 files changed

+41
-48
lines changed

2 files changed

+41
-48
lines changed

assert/assert.go

Lines changed: 41 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package assert
22

33
import (
4+
"flag"
45
"fmt"
56
"os"
67
"reflect"
@@ -16,13 +17,17 @@ import (
1617
type defInd = uint32
1718

1819
const (
20+
// Plain converts asserts just plain error messages without extra
21+
// information.
22+
Plain defInd = 0 + iota
23+
1924
// Production (pkg default) is the best asserter for most uses. The
2025
// assertion violations are treated as Go error values. And only a
2126
// pragmatic caller info is automatically included into the error message
2227
// like file name, line number, and caller function, all in one line:
2328
//
2429
// copy file: main.go:37: CopyFile(): assertion violation: string shouldn't be empty
25-
Production defInd = 0 + iota
30+
Production
2631

2732
// Development is the best asserter for most development uses. The
2833
// assertion violations are treated as Go error values. And a formatted
@@ -76,8 +81,12 @@ const (
7681
Debug
7782
)
7883

84+
type flagAsserter struct{}
85+
7986
// Deprecated: use e.g. assert.That(), only default asserter is used.
8087
var (
88+
PL = AsserterToError
89+
8190
// P is a production Asserter that sets panic objects to errors which
8291
// allows err2 handlers to catch them.
8392
P = AsserterToError | AsserterCallerInfo
@@ -97,12 +106,13 @@ var (
97106
// These two are our indexing system for default asserter. Note also the
98107
// mutex blew. All of this is done to keep client package race detector
99108
// cool.
109+
// Plain
100110
// Production
101111
// Development
102112
// Test
103113
// TestFull
104114
// Debug
105-
defAsserter = []Asserter{P, B, T, TF, D}
115+
defAsserter = []Asserter{PL, P, B, T, TF, D}
106116
def defInd
107117

108118
// mu is package lvl Mutex that is used to cool down race detector of
@@ -112,10 +122,13 @@ var (
112122
// indexing system we are using for default asserter (above) we are pretty
113123
// much theard safe.
114124
mu sync.Mutex
125+
126+
asserterFlag flagAsserter
115127
)
116128

117129
func init() {
118130
SetDefault(Production)
131+
flag.Var(&asserterFlag, "asserter", "`asserter`: Plain, Prod, Dev, Debug")
119132
}
120133

121134
type (
@@ -543,29 +556,31 @@ func SetDefault(i defInd) Asserter {
543556

544557
// mapDefInd runtime asserters, that's why test asserts are removed for now.
545558
var mapDefInd = map[string]defInd{
546-
"Production": Production,
547-
"Development": Development,
559+
"Plain": Plain,
560+
"Prod": Production,
561+
"Dev": Development,
548562
//"Test": Test,
549563
//"TestFull": TestFull,
550564
"Debug": Debug,
551565
}
552566

553-
var MapDefIndToString = map[defInd]string{
554-
Production: "Production",
555-
Development: "Development",
567+
var mapDefIndToString = map[defInd]string{
568+
Plain: "Plain",
569+
Production: "Prod",
570+
Development: "Dev",
556571
Test: "Test",
557572
TestFull: "TestFull",
558573
Debug: "Debug",
559574
}
560575

561576
func AsserterString() string {
562-
return MapDefIndToString[def]
577+
return mapDefIndToString[def]
563578
}
564579

565-
func NewDefInd(v string) defInd {
580+
func newDefInd(v string) defInd {
566581
ind, found := mapDefInd[v]
567582
if !found {
568-
return Production
583+
return Plain
569584
}
570585
return ind
571586
}
@@ -601,3 +616,19 @@ func myByteToInt(b []byte) int {
601616
type Number interface {
602617
constraints.Float | constraints.Integer
603618
}
619+
620+
// String is part of the flag interfaces
621+
func (f *flagAsserter) String() string {
622+
return AsserterString()
623+
}
624+
625+
// Get is part of the flag interfaces, getter.
626+
func (f *flagAsserter) Get() any {
627+
return mapDefIndToString[def]
628+
}
629+
630+
// Set is part of the flag.Value interface.
631+
func (*flagAsserter) Set(value string) error {
632+
SetDefault(newDefInd(value))
633+
return nil
634+
}

err2_init.go

Lines changed: 0 additions & 38 deletions
This file was deleted.

0 commit comments

Comments
 (0)