surdeus 1f2386d9c8 feat: WE ARE HERE. 10 months ago
..
README.md 7fe533fd71 Forking. 11 months ago
cli.go 7fe533fd71 Forking. 11 months ago
cli_test.go 1f2386d9c8 feat: WE ARE HERE. 10 months ago
command.go 1f2386d9c8 feat: WE ARE HERE. 10 months ago
command_test.go 7fe533fd71 Forking. 11 months ago
option.go 1f2386d9c8 feat: WE ARE HERE. 10 months ago
option_test.go 7fe533fd71 Forking. 11 months ago
usage.go 7fe533fd71 Forking. 11 months ago
usage_test.go 1f2386d9c8 feat: WE ARE HERE. 10 months ago

README.md

cli

Go package for building cli tool that can load program configuration from multiple sources:

  • command flags
  • Environment

Usage

Define a Go struct that uses field tags to describe a command configuration:

type config struct {
	String string `cli:"string" env:"CONF_STRING" help:"A string argmument for demo purpose."`
	Int    int    `cli:"int"    env:"CONF_INT"    help:"A int argmument for demo purpose."`
	Help   bool   `cli:"h"      env:"-"           help:"Show help."`
}
Field Tag Description
cli Maps a cli flag for the given field.
env Maps environment variable for the given field.
help Setup a description for the given field when using the help flag -h

Load the config:

func main() {
	cfg := config{
		Int: 42, // Set 42 as default value for Int field.
	}

	// Registers the config.
	cli.Register().
		Help("A demo cli program").
		Options(&cfg)

	cli.Load() // Stores corresponding cli and env into cfg.

	fmt.Println(cfg)
}

command output with -h flag:

▶ ./my-program -h
Usage:

    hds [options]

Description:

    A demo cli program

Options:

    -string  string    A string argmument for demo purpose.
                       Env:     CONF_STRING

    -int     int       A int argmument for demo purpose.
                       Env:     CONF_INT
                       Default: 42

    -h       bool      Show help.