Giter Site home page Giter Site logo

gocmder's Introduction

Go CMDER

Go Cmder combine Cobra and Viper to auto generate command-line tool where the user can control configuration in a precedence order: flags > environment variables > configuration files and the defaults defined by the config.

This library was inspired by Sting of the viper article from Carolyn Van Slyck.

Build codecov Go Report Card License

Install

go get github.com/ergagnon/gocmder

How to use

Create the config struct with tags

Supported tags:

  1. desc: used for the Flag description.
  2. default: default value used in Flag and Viper config. Supported value type: string, int, float32, bool
  3. required: set a required Flag.
  4. hidden: don't create a Flag when hidden is true.

Example:

type AppConfig struct {
    Directory string `desc:"Directory to browse" default:"."`
    Server ServerConfig // support multi-level config          
}

type ServerConfig struct {
    Url string `desc:"Server url" default:"localhost"`
    Port int `desc:"Username" default:"8080"`
}

Create a Go CMDER with the config and options

Call the function

func NewCmder(cfg any, onFinalize OnFinalizeFunc, opts ...CmderOption) (*Cmder, error)
cli, err := gocmder.NewCmder(AppConfig{}, func(cfg any) error {
    // At this step, the config object has been filled
    // with all the values. 
    // You can use it in your application.
    return nil
})

This will auto-generate

Flags:

Usage:
   [flags]

Flags:
      --directory string    Directory to browse (default ".")
  -h, --help                help for this command
      --server-port int     Username (default 8080)
      --server-url string   Server url (default "localhost")

Environment Variable

DIRECTORY
SERVER_PORT
SERVER_URL

Config file (optional)
Configuration will use Viper defaults.

reading from JSON, TOML, YAML, HCL, envfile and Java properties config files

Yaml example:

directory: "my_dir"
server:
  url: "127.0.0.1"
  port: 8080

gocmder's People

Contributors

ergagnon avatar

Stargazers

 avatar Ml Lp avatar Mathieu St-Vincent avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.