gocli

package module
v2.1.2+incompatible Latest Latest
Warning

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

Go to latest
Published: Aug 11, 2016 License: MIT Imports: 8 Imported by: 13

README

gocli

Build Status Coverage GoDoc Release

A Go CLI library that provides subcommand handling, tidy usage and version printing.

Installation

go get github.com/yieldbot/gocli

Usage

A simple CLI app

See simple.go for full code.

func init() {
  // Init flags
  flag.BoolVar(&usageFlag, "h", false, "Display usage")
  flag.BoolVar(&usageFlag, "help", false, "Display usage")
  flag.BoolVar(&versionFlag, "version", false, "Display version information")
  flag.BoolVar(&versionFlag, "v", false, "Display version information")
}

func main() {

  // Init cli
  cli = gocli.Cli{
    Name:        "simple",
    Version:     "1.0.0",
    Description: "A simple app",
    Commands: map[string]string{
      "echo": "Print the given arguments",
    },
  }
  cli.Init()

  // Run commands
  if cli.SubCommand == "echo" {
    // Echo command
    fmt.Println(strings.Join(cli.SubCommandArgs, " "))
  } else if versionFlag {
    // Version
    cli.PrintVersion(true)
  } else {
    // Default
    cli.PrintUsage()
  }
}
$ go run examples/simple.go
Usage: simple [OPTIONS] COMMAND [arg...]

A simple app

Options:
  -h, --help    : Display usage
  -v, --version : Display version information

Commands:
  echo          : Print the given arguments
$ go run examples/simple.go -v
Bin version : 1.0.0
Go version  : go1.6
$ go run examples/simple.go echo hello world
hello world

License

Licensed under The MIT License (MIT)
For the full copyright and license information, please view the LICENSE.txt file.

Documentation

Overview

Package gocli is a CLI library that provides subcommand handling, tidy usage and version printing.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Cli

type Cli struct {
	// Name is the cli name
	Name string

	// Version is the cli version
	Version string

	// Description is the cli description
	Description string

	// Commands contains the subcommand list of the cli
	Commands map[string]string

	// SubCommand contains the runtime subcommand
	SubCommand string

	// SubCommandArgs contains the args of the runtime subcommand
	SubCommandArgs []string

	// SubCommandArgsMap contains the args of the runtime subcommand as mapped
	SubCommandArgsMap map[string]string

	// Flags contains flags
	Flags map[string]string

	// LogOut is logger for stdout
	LogOut *log.Logger

	// LogErr is logger for stderr
	LogErr *log.Logger
}

Cli represent command line interface

func (*Cli) Init

func (cl *Cli) Init()

Init initializes Cli instance

func (Cli) PrintUsage

func (cl Cli) PrintUsage()

PrintUsage prints usage info Usage format follows common convention for Go apps

func (Cli) PrintVersion

func (cl Cli) PrintVersion(extra bool)

PrintVersion prints version information

type Table

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

Table represent tabular data as a table

func (*Table) AddRow

func (t *Table) AddRow(row int, cols ...string) error

AddRow adds a row data by the given row number and column values

func (*Table) Data

func (t *Table) Data() [][]string

Data gets data

func (*Table) PrintData

func (t *Table) PrintData()

PrintData prints data

func (*Table) SetData

func (t *Table) SetData(row, col int, val string) error

SetData sets a data by the given row, column and value

Directories

Path Synopsis
A simple app
A simple app

Jump to

Keyboard shortcuts

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