github.com/googollee/clic@v0.1.5/clic.go (about)

     1  /*
     2  package clic is short for CLI Config. It implements a framework to load/parse cli configuration from a file, environment or flags.
     3  
     4  Usage:
     5    - Register functions ([Register] and [RegisterCallback]) should be called in `func init()` in packages, or before calling [Parse].
     6    - [Parse] function should be called at the beginning of "main()", before calling functions in other packages.
     7    - [Parse] function must not be called in `func init()`, because other sub-packages may not finish initialization at that time.
     8  
     9  See examples for the usage.
    10  */
    11  package clic
    12  
    13  import (
    14  	"context"
    15  	"flag"
    16  	"fmt"
    17  	"os"
    18  )
    19  
    20  var CommandLine = NewSet(flag.CommandLine, DefaultSources...)
    21  
    22  /*
    23  RegisterType registers a "Config" type with the "name" as the scope name and returns a getter function which returns a parsed Config value.
    24  
    25  Example:
    26  
    27  	package main
    28  
    29  	func main() {
    30  		ctx := context.Background()
    31  
    32  		loadCfg := clic.RegisterType[database.Config]()
    33  
    34  		clic.Parse(ctx)
    35  
    36  		db := database.New(loadCfg())
    37  	}
    38  */
    39  func RegisterType[Config any](name string) func(ctx context.Context) *Config {
    40  	var c Config
    41  	Register(name, &c)
    42  
    43  	return func(ctx context.Context) *Config {
    44  		return &c
    45  	}
    46  }
    47  
    48  /*
    49  Register registers a "Config" value with the "name" as the scope name. The value is filled after calling [Parse] function.
    50  
    51  Example:
    52  
    53  	package main
    54  
    55  	func main() {
    56  		ctx := context.Background()
    57  
    58  		var dbCfg database.Config
    59  		clic.Register("database", &db)
    60  
    61  		clic.Parse(ctx)
    62  
    63  		db := database.New(dbCfg)
    64  	}
    65  */
    66  func Register(prefix string, value any) {
    67  	CommandLine.RegisterValue(prefix, value)
    68  }
    69  
    70  /*
    71  RegisterCallback registers a callback function with the "name" as the scope name. The callback is called after calling [Parse] function.
    72  
    73  Example:
    74  
    75  	package main
    76  
    77  	type Log struct {
    78  		Level slog.Level `clic:"level,INFO,the level of log"`
    79  	}
    80  
    81  	func initLogLevel(ctx context.Context, cfg *Log) {
    82  		slog.SetLevel(cfg.Level)
    83  	}
    84  
    85  	func main() {
    86  		ctx := context.Background()
    87  
    88  		clic.RegisterCallback("log", initLogLevel)
    89  
    90  		clic.Parse(ctx)
    91  	}
    92  */
    93  func RegisterCallback(prefix string, f any) {
    94  	CommandLine.RegisterCallback(prefix, f)
    95  }
    96  
    97  // Parse parses configuration from [DefaultSources] and [os.Args].
    98  //
    99  // If any error happens during calling, "Parse()" prints that error on Stderr and calls [os.Exit] to exit with "125" code.
   100  func Parse(ctx context.Context) {
   101  	if err := CommandLine.Parse(ctx, os.Args[1:]); err != nil {
   102  		fmt.Fprintln(os.Stderr, "parse config error:", err)
   103  		os.Exit(125)
   104  	}
   105  }