Skip to content
/ cli Public

A simple package for building command line applications in Go

License

Notifications You must be signed in to change notification settings

phogolabs/cli

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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:

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

Installation

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

See the install instructions for Go.

To install CLI, simply run:

$ go get github.com/phogolabs/cli

Getting Started

package main

import (
	"fmt"
	"os"
	"syscall"

	"github.com/phogolabs/cli"
)

var flags = []cli.Flag{
	&cli.YAMLFlag{
		Name:     "config",
		Usage:    "Application Config",
		Path:     "/etc/app/default.conf",
		EnvVar:   "APP_CONFIG",
		Value:    &Config{},
		Required: true,
	},
	&cli.StringFlag{
		Name:     "listen-addr",
		Usage:    "Application TCP Listen Address",
		EnvVar:   "APP_LISTEN_ADDR",
		Value:    ":8080",
		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 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.StringFlag{
		Name:      "name",
		EnvVar:    "APP_NAME",
		Validator: &Validator{},
	},
}

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

Code of conduct

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published