Skip to content

Commit 88800c0

Browse files
committed
docs: improved documentation and added SetFlag method
1 parent c2039e8 commit 88800c0

File tree

3 files changed

+175
-53
lines changed

3 files changed

+175
-53
lines changed

errors.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package go_flags
2+
3+
var (
4+
ErrInvalidValue = "invalid value %q, allowed values are: %s"
5+
)

flag.go

Lines changed: 86 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,124 @@
1-
package go_mode_flag
1+
package go_flags
22

33
import (
44
"flag"
55
"fmt"
66
"strings"
77
)
88

9-
// Flag is a custom flag type
10-
type Flag struct {
11-
value string
12-
allowed []string
13-
}
9+
type (
10+
// Flag is a custom flag type that restricts its value to a predefined set of allowed strings.
11+
Flag struct {
12+
defaultValue string
13+
value *string
14+
allowed []string
15+
name string
16+
usage string
17+
}
18+
)
1419

15-
// NewFlag creates a new Flag
16-
func NewFlag(value string, allowed []string) *Flag {
20+
// NewFlag creates a new Flag with a default value and a list of allowed values.
21+
//
22+
// Parameters:
23+
//
24+
// defaultValue - the default value for the flag.
25+
// value - the default value for the flag.
26+
// allowed - slice of allowed string values.
27+
// name - the name of the flag.
28+
// usage - the usage description for the flag.
29+
//
30+
// Returns:
31+
//
32+
// A pointer to the created Flag.
33+
func NewFlag(
34+
defaultValue string,
35+
value *string,
36+
allowed []string,
37+
name string,
38+
usage string,
39+
) *Flag {
1740
return &Flag{
41+
defaultValue,
1842
value,
1943
allowed,
44+
name,
45+
usage,
2046
}
2147
}
2248

23-
// String returns the string representation of the flag value
49+
// Default returns the default value of the flag.
50+
//
51+
// Returns:
52+
//
53+
// The default value of the flag as a string.
54+
func (f *Flag) Default() string {
55+
return f.defaultValue
56+
}
57+
58+
// String returns the string representation of the flag's current value.
59+
//
60+
// Returns:
61+
//
62+
// The current value of the flag as a string.
2463
func (f *Flag) String() string {
25-
return f.value
64+
if f.value != nil {
65+
return *f.value
66+
}
67+
return f.defaultValue
2668
}
2769

28-
// Value returns the flag value
70+
// Value returns the current value of the flag.
71+
//
72+
// Returns:
73+
//
74+
// The current value of the flag as a string.
2975
func (f *Flag) Value() string {
30-
return f.value
76+
return f.String()
3177
}
3278

33-
// Allowed returns the allowed values
79+
// Allowed returns the list of allowed values for the flag.
80+
//
81+
// Returns:
82+
//
83+
// The allowed values.
3484
func (f *Flag) Allowed() []string {
3585
return f.allowed
3686
}
3787

38-
// Set validates and sets the flag value
88+
// Set validates and sets the flag's value.
89+
//
90+
// Parameters:
91+
//
92+
// value - the value to set.
93+
//
94+
// Returns:
95+
//
96+
// An error if the value is not allowed, otherwise nil.
3997
func (f *Flag) Set(value string) error {
4098
for _, v := range f.allowed {
4199
if value == v {
42-
f.value = value
100+
f.value = &value
43101
return nil
44102
}
45103
}
46104
return fmt.Errorf(
47-
"invalid value %q, allowed values are: %s", value,
105+
ErrInvalidValue, value,
48106
strings.Join(f.allowed, ", "),
49107
)
50108
}
51109

52-
// SetFlag sets the mode flag
110+
// SetFlag registers the custom flag with the standard flag package.
111+
func (f *Flag) SetFlag() {
112+
SetFlag(f, f.name, f.usage)
113+
}
114+
115+
// SetFlag registers the custom flag with the standard flag package.
116+
//
117+
// Parameters:
118+
//
119+
// value - the flag.Value to register.
120+
// name - the flag name.
121+
// usage - the usage description for the flag.
53122
func SetFlag(value flag.Value, name string, usage string) {
54123
flag.Var(
55124
value,

mode/mode.go

Lines changed: 84 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,89 +1,137 @@
11
package mode
22

33
import (
4+
"fmt"
5+
"strings"
6+
47
goflags "github.com/ralvarezdev/go-flags"
58
)
69

710
const (
8-
// FlagName is the name of the mode flag
11+
// FlagName is the name of the mode flag.
912
FlagName = "m"
1013

11-
// FlagUsage is the usage of the mode flag
12-
FlagUsage = "Specify mode. Allowed values are: dev, prod, debug, migrate. Default is the dev mode"
14+
// FlagUsage is the usage of the mode flag.
15+
// It describes allowed values and the default mode.
16+
FlagUsage = "Specify mode. Allowed values are: %s. Default is the %s mode."
1317
)
1418

1519
type (
16-
// Mode is the environment mode
20+
// Mode represents the environment mode as a string.
1721
Mode string
1822

19-
// Flag is a custom flag type for mode
23+
// Flag is a custom flag type for mode, embedding goflags.Flag.
2024
Flag struct {
2125
goflags.Flag
2226
}
2327
)
2428

2529
var (
26-
// Dev is the development mode
30+
// Dev is the development mode.
2731
Dev Mode = "dev"
2832

29-
// Prod is the production mode
33+
// Prod is the production mode.
3034
Prod Mode = "prod"
3135

32-
// Debug is the debug mode
36+
// Debug is the debug mode.
3337
Debug Mode = "debug"
3438

35-
// Migrate is the migration mode
39+
// Migrate is the migration mode.
3640
Migrate Mode = "migrate"
41+
42+
// DefaultMode is the default mode (development).
43+
DefaultMode = Dev
44+
45+
// AllowedModes is the list of allowed modes.
46+
AllowedModes = []string{
47+
string(Dev), string(Prod), string(Debug), string(Migrate),
48+
}
49+
50+
// ModeFlag is the environment mode flag instance.
51+
ModeFlag = NewFlag(
52+
string(DefaultMode),
53+
nil,
54+
AllowedModes,
55+
FlagName,
56+
fmt.Sprintf(
57+
FlagUsage,
58+
strings.Join(AllowedModes, ", "),
59+
string(Dev),
60+
),
61+
)
3762
)
3863

39-
// NewFlag creates a new Flag with allowed values
40-
func NewFlag(defaultValue string, allowed []string) *Flag {
64+
// NewFlag creates a new Flag with allowed values.
65+
//
66+
// Parameters:
67+
//
68+
// defaultValue - the default value for the flag.
69+
// value - pointer to a string to store the flag value (can be nil).
70+
// allowed - slice of allowed string values.
71+
// name - the flag name.
72+
// usage - the usage description for the flag.
73+
//
74+
// Returns:
75+
//
76+
// A pointer to the created Flag.
77+
func NewFlag(
78+
defaultValue string,
79+
value *string,
80+
allowed []string,
81+
name string,
82+
usage string,
83+
) *Flag {
4184
return &Flag{
42-
Flag: *goflags.NewFlag(defaultValue, allowed),
85+
Flag: *goflags.NewFlag(defaultValue, value, allowed, name, usage),
4386
}
4487
}
4588

46-
// Mode returns the mode
47-
func (f *Flag) Mode() Mode {
48-
if f.IsProd() {
49-
return Prod
50-
}
51-
if f.IsDev() {
52-
return Dev
53-
}
54-
if f.IsDebug() {
55-
return Debug
56-
}
57-
return Migrate
89+
// Default returns the default value of the flag.
90+
//
91+
// Returns:
92+
//
93+
// The default value.
94+
func (f *Flag) Default() string {
95+
return f.Default()
5896
}
5997

60-
// IsDev returns true if it's the development mode
98+
// IsDev returns true if the current mode is development.
99+
//
100+
// Returns:
101+
//
102+
// True if development mode, false otherwise.
61103
func (f *Flag) IsDev() bool {
62104
return f.Value() == string(Dev)
63105
}
64106

65-
// IsProd returns true if it's the production mode
107+
// IsProd returns true if the current mode is production.
108+
//
109+
// Returns:
110+
//
111+
// True if production mode, false otherwise.
66112
func (f *Flag) IsProd() bool {
67113
return f.Value() == string(Prod)
68114
}
69115

70-
// IsDebug returns true if it's the debug mode
116+
// IsDebug returns true if the current mode is debug.
117+
//
118+
// Returns:
119+
//
120+
// True if debug mode, false otherwise.
71121
func (f *Flag) IsDebug() bool {
72122
return f.Value() == string(Debug)
73123
}
74124

75-
// IsMigrate returns true if it's the migration mode
125+
// IsMigrate returns true if the current mode is migration.
126+
//
127+
// Returns:
128+
//
129+
// True if migration mode, false otherwise.
76130
func (f *Flag) IsMigrate() bool {
77131
return f.Value() == string(Migrate)
78132
}
79133

80-
// ModeFlag is the environment mode
81-
var ModeFlag = NewFlag(
82-
string(Dev),
83-
[]string{string(Dev), string(Prod), string(Debug), string(Migrate)},
84-
)
85-
86-
// init initializes the mode flag
134+
// init initializes the mode flag.
87135
func init() {
88-
goflags.SetFlag(ModeFlag, FlagName, FlagUsage)
136+
ModeFlag.SetFlag()
89137
}

0 commit comments

Comments
 (0)