You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
// Parse command-line flags and environment variables into your config
70
+
// Parse command-line flags and environment variables into config
55
71
settingo.ParseTo(config)
56
72
57
-
//Now config fields will be updated according to:
58
-
// 1. Command-line flags
59
-
// 2. Environment variables
60
-
// 3. Struct defaults
61
-
fmt.Println("APIKey =", config.APIKey)
62
-
fmt.Println("UploadDir=", config.UploadDir)
63
-
fmt.Println("Port =", config.Port)
64
-
fmt.Println("Quality =", config.Quality)
73
+
//Print out the configuration values
74
+
fmt.Println("Configuration:")
75
+
fmt.Println("APIKey =", config.APIKey)
76
+
fmt.Println("Port =", config.Port)
77
+
fmt.Println("Verbose=", config.Verbose)
78
+
fmt.Println("Headers =", config.Headers)
79
+
fmt.Println("Hosts=", config.Hosts)
80
+
fmt.Println("Items =", config.Items)
65
81
}
66
-
67
82
```
68
83
When you build your application (e.g., go build -o myapp) and run ./myapp --help, settingo automatically generates help text based on struct tags and default values:
69
84
```bash
70
85
Usage of ./myapp:
71
86
-APIKEY string
72
87
API key for authentication (default "foo-bar")
73
-
-PORT string
74
-
Port to run the server on (default "8080")
75
-
-QUALITY int
76
-
WebP quality (0-100) (default 85)
77
-
-UPLOADDIR string
78
-
Directory for file uploads (default "./uploads")
88
+
-HEADERS string
89
+
HTTP headers to include (key:value1,value2;key2:value3 format) (default "Accept:application/json")
90
+
-HOSTS string
91
+
List of allowed hosts (comma-separated) (default "localhost,127.0.0.1")
92
+
-ITEMS string
93
+
List of items (pipe-separated, sep=|) (default "alpha,beta,gamma")
94
+
-PORT int
95
+
Port to run the server on (default 8080)
96
+
-VERBOSE string
97
+
Enable verbose output (default "true")
79
98
```
80
99
81
100
```go
@@ -129,26 +148,51 @@ The priority order is as follows
129
148
2. Environment variables
130
149
3. Default values
131
150
132
-
## Types
133
-
Settingo supports different types.
151
+
## Example: Custom Parsing for "Messy" Input with `SetParsed`
152
+
153
+
Sometimes, environment variables or command-line arguments might not be perfectly formatted. You might receive an empty string, mixed-case input, or data that needs transformation. `settingo`'s `SetParsed` is ideal for cleaning up and standardizing such "messy" input.
154
+
155
+
This example demonstrates handling a `RAW_USERNAME` environment variable, ensuring the `Username` setting is always a lowercase, non-empty string, defaulting to "anonymous" if the input is blank:
156
+
134
157
```go
135
-
// string
136
-
settingo.Set("FOO", "default", "help text")
137
-
settingo.Get("FOO")
138
-
139
-
// integer
140
-
settingo.SetInt("FOO", 42, "help text")
141
-
settingo.GetInt("FOO")
142
-
143
-
// boolean
144
-
settingo.SetBool("FOO", true, "help text")
145
-
settingo.GetBool("FOO")
146
-
147
-
// map
148
-
defaultMap:=make(map[string][]string)
149
-
defaultMap["foo"] = []string{"bar"}
150
-
settingo.SetMap("FOO", defaultMap, "help text")
151
-
settingo.GetMap("FOO")
158
+
package main
159
+
160
+
import (
161
+
"fmt"
162
+
"github.com/Attumm/settingo/settingo"
163
+
"strings"
164
+
)
165
+
166
+
// Define your configuration with Parsed setting
167
+
typeConfigstruct {
168
+
Usernamestring`settingo:"USERNAME for application access"`
169
+
}
170
+
171
+
funcmain() {
172
+
config:= &Config{
173
+
Username: "default",
174
+
}
175
+
176
+
// Use SetParsed to handle potentially messy Username input
177
+
settingo.SetParsed("USERNAME", "default", "Username for application access", func(input string) string {
178
+
if input == "" {
179
+
return"anonymous"// Default to "anonymous" if empty input
180
+
}
181
+
return strings.ToLower(input) // Convert username to lowercase
0 commit comments