Skip to content
master
Go to file
Code

Latest commit

 

Git stats

Files

Permalink
Failed to load latest commit information.
Type
Name
Latest commit message
Commit time
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

README.md

CLI

Documentation License Build Status Coverage Go Report Card

A simple package for building command line applications in Go. The API is influenced by https://github.com/urfave/cli package, but it is way more flexible. It provides the following features:

  • Data conversion that allow conversion of data to a compatible data type accepted by the declared flag
  • Data providers that allow setting the flag's value from different sources such as environment variables, files, AWS S3 and SSM
  • More extensible flag types such as URL, IP, JSON, YAML and so on. For more information see the docs

Installation

Make sure you have a working Go environment. Go version 1.13.x is supported.

See the install instructions for Go.

To install CLI, simply run:

$ go get github.com/phogolabs/cli

Getting Started

import (
	"os"
	"syscall"

	"github.com/phogolabs/cli"
)

var flags = []cli.Flag{
	&cli.StringFlag{
		Name:     "config",
		Usage:    "Application Config",
		EnvVar:   "APP_CONFIG",
		FilePath: "/etc/app/app.config",
		Required: true,
	},
}


func main() {
	app := &cli.App{
		Name:      "prana",
		HelpName:  "prana",
		Usage:     "Golang Database Manager",
		UsageText: "prana [global options]",
		Version:   "1.0-beta-04",
		Flags:     flags,
		Action:    run,
		Signals:   []os.Signal{syscall.SIGTERM},
		OnSignal:  signal,
	}

	app.Run(os.Args)
}

// run executes the application
func run(ctx *cli.Context) error {
	fmt.Println("Application started")
	return nil
}

// signal handles OS signal
func signal(ctx *cli.Context, signal os.Signal) error {
	fmt.Println("Application signal", signal)
	return nil
}

Validation

You can set the Required field to true if you want to make some flags mandatory. If you need some customized validation, you can create a custom validator in the following way:

As a function:

validate := cli.ValidatorFunc(func(ctx *cli.Context, value interface{}) error {
        //TODO: your validation logic
	return nil
})

As a struct that has a Validate function:

type Validator struct {}

func (v *Validator) Validate(ctx *cli.Context, value interface{}) error {
        //TODO: your validation logic
	return nil
}

Then you can set the validator like that:

var flags = []cli.Flag{
	&cli.StringSliceFlag{
		Name:     "endpoint",
		Usage:    "Endpoint URL",
		EnvVar:   "APP_ENDPOINT",
		Required: true,
		Validator: validate,
	},
	&cli.StringFlag{
		Name:      "name",
		EnvVar:    "APP_NAME",
		Validator: &Validator{},
	},
}

Providers

The providers allow setting the flag's value from external sources:

Contributing

We are open for any contributions. Just fork the project.

About

A simple package for building command line applications in Go

Topics

Resources

License

Releases

No releases published

Packages

No packages published
You can’t perform that action at this time.