-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathclic_test.go
More file actions
54 lines (45 loc) · 1.14 KB
/
clic_test.go
File metadata and controls
54 lines (45 loc) · 1.14 KB
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
package clic_test
import (
"context"
"flag"
"fmt"
"log/slog"
"os"
"time"
"github.com/googollee/clic"
)
func Example() {
// structs
type Database struct {
Driver string `clic:"driver,sqlite3,the driver of the database [sqlite3,mysql,postgres]"`
URL string `clic:"url,./database.sqlite,the url of the database"`
}
type Log struct {
Level slog.Level `clic:"level,INFO,the level of the log [DEBUG,INFO,WARN,ERROR]"`
}
initLog := func(ctx context.Context, log *Log) error {
fmt.Println("set log level:", log.Level)
return nil
}
// args
oldArgs := os.Args
os.Args = []string{oldArgs[0], "-database.driver", "driver", "-database.url", "url", "sub_command"}
defer func() {
os.Args = oldArgs
}()
// main code
ctx := context.Background()
var db Database
clic.Register("database", &db)
clic.RegisterCallback("log", initLog)
// config should be finished in a minute.
configCtx, cancel := context.WithTimeout(ctx, time.Minute)
defer cancel()
clic.Parse(configCtx)
fmt.Println("database:", db)
fmt.Println("remain args:", flag.Args())
// Output:
// set log level: INFO
// database: {driver url}
// remain args: [sub_command]
}