clap

package module
v0.2.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 31, 2021 License: MIT Imports: 11 Imported by: 0

README

clap

Microservices oriented 12-factor Go library for parsing environment variables and command line flags to arbitrary config struct using struct tags to define default values and to override flag names and environment variables' names.

Build Status Go Coverage Go Reference Go Report License

Example

package main

import (
	"fmt"
	"os"

	"go.ectobit.com/clap"
)

func main() {
	type config struct {
		Port int `def:"3000"`
		DB   struct {
			Kind     clap.StringSlice `desc:"database kinds to use" def:"pg,mongo"`
			Postgres struct {
				Host string `flag:"pg-host" env:"PG_HOST" def:"localhost"`
				Port string `flag:"pg-port" env:"PG_PORT" def:"5432"`
			}
			Mongo struct {
				Host string `flag:"mongo-host" env:"MONGO_HOST" def:"mongodb://localhost:27017"`
			}
		}
	}

	cfg := &config{}

	cmd := clap.New("myapp", os.Args[1:])

	cmd.MustConfig(cfg)
}
Output of -help flag
Usage of myapp:
  -db-kind value
    	database kinds to use (env MYAPP_DB_KIND) (default [pg mongo])
  -mongo-host string
    	db mongo host (env MONGO_HOST) (default "mongodb://localhost:27017")
  -pg-host string
    	db postgres host (env PG_HOST) (default "localhost")
  -pg-port string
    	db postgres port (env PG_PORT) (default "5432")
  -port int
    	port (env MYAPP_PORT) (default 3000)

Supported struct tags

  • flag - override generated flag name
  • env - override generated environment variable name
  • desc - override generated flag description
  • def - override default (zero) value

Custom flag types

  • clap.StringSlice
  • clap.IntSlice
  • clap.URL

Order of precedence:

  • command line options
  • environment variables
  • default values

TODO

  • support req struct tag to mark required values

Origin of the name

This library is named the same as Rust's crate clap, but other than that there are not many similarities. Name clap fits very well to something used at the start.

Documentation

Overview

Package clap is a library for parsing command line flags in config struct using struct tags.

Example (Basic)
package main

import (
	"fmt"
	"os"

	"go.ectobit.com/clap"
)

func main() {
	type config struct {
		Host string
		Port int
		DB   struct {
			Kind     string
			Postgres struct {
				Host string
			}
			Mongo struct {
				Host clap.StringSlice
			}
		}
	}

	cfg := &config{}

	cmd := clap.New("cool", os.Args[1:])

	cmd.MustConfig(cfg)
	fmt.Printf("Config:\n%#v\n", cfg)
}
Output:

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Clap

type Clap struct {
	// contains filtered or unexported fields
}

Clap is an abstraction of a CLI command.

func New

func New(cmdName string, args []string) *Clap

New creates new clap command.

func (*Clap) MustConfig

func (c *Clap) MustConfig(config interface{})

MustConfig parses command line flags in supplied config struct using struct tags to optionally override generated environment variables' names, flags names, descriptions and default values.

func (*Clap) SetExit added in v0.2.1

func (c *Clap) SetExit(exitFunc func(string))

SetExit overrides default exit function which uses os.Exit. Useful for testing.

func (*Clap) SetOutput added in v0.0.3

func (c *Clap) SetOutput(output io.Writer)

SetOutput sets the destination for usage and error messages. If output is nil, os.Stderr is used. Useful for testing.

type IntSlice

type IntSlice []int

IntSlice implements flag.Getter interface for []int type.

func (*IntSlice) Get

func (f *IntSlice) Get() interface{}

Get returns flag's value.

func (*IntSlice) Set

func (f *IntSlice) Set(s string) error

Set sets flag's value by splitting provided comma separated string.

func (*IntSlice) String

func (f *IntSlice) String() string

String formats flag's value.

type StringSlice

type StringSlice []string

StringSlice implements flag.Getter interface for []string type.

func (*StringSlice) Get

func (f *StringSlice) Get() interface{}

Get returns flag's value.

func (*StringSlice) Set

func (f *StringSlice) Set(s string) error

Set sets flag's value by splitting provided comma separated string.

func (*StringSlice) String

func (f *StringSlice) String() string

String formats flag's value.

type URL

type URL struct {
	*url.URL
}

URL implements flag.Getter interface for url.URL type.

func (*URL) Get

func (f *URL) Get() interface{}

Get returns flag's value.

func (*URL) Set

func (f *URL) Set(s string) error

Set sets flag's value by parsing provided url.

func (*URL) String

func (f *URL) String() string

String formats flag's value.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL