Help us shape the future of Go! Take the 2026 Go Developer Survey

srgnt

package module
v0.0.0-...-6e17924 Latest Latest
Warning

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

Go to latest
Published: Sep 30, 2017 License: MIT Imports: 7 Imported by: 1

README

srgnt

Simple command-line framework for Go

Install

To get started, you need a working Go environment. Once available, grab the package here:

$ go get github.com/vutran/srgnt

Usage

srgnt provides a very simple API. The following is an example of a minimal CLI app that has 1 command called "hello" which will print out "Hello, world!".

package main

import (
    "flag"
    "fmt"
    "github.com/vutran/srgnt"
)

func Hello(_ *flag.FlagSet) {
    fmt.Println("Hello, world!")
}

func main() {
    cli := srgnt.CreateProgram("foo")
    cli.AddCommand("hello", Hello, "Prints \"Hello, world!\"")
    cli.Run()
}

By running foo in the example above, it will display the default help text that is generated for you automatically.

Usage:

        foo [flags] <command>

Commands:

        hello   Prints "Hello, world!"
Flags and Commands

You can register as much flags and commands as you would like. Flags must precede commands.

The following example will output "Hello, world!":

$ foo hello

And with flags:

$ foo --name Vu hello

Our example above doesn't register any flags so the previous command can't read the --name flag. To register our flag, we need to update our cli instance from the original basic example.

package main

import (
    "flag"
    "fmt"
    "github.com/vutran/srgnt"
)

func Hello(flags *flag.FlagSet) {
+   name := flags.Lookup("name")
+   fmt.Printf("Hello, %s!\n", name.Value.String())
-   fmt.Println("Hello, world!")
}

func main() {
    cli := srgnt.CreateProgram("foo")
-   cli.AddCommand("hello", Hello, "Prints \"Hello, world!\"")
+   cli.AddCommand("hello", Hello, "Prints \"Hello, <name>!\"")
+   cli.AddStringFlag("name", "", "Set a name")
    cli.Run()
}

License

MIT © Vu Tran

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Help

func Help(p *Program)

Types

type Command

type Command struct {
	Description string
	Callback    CommandFunction
}

type CommandFunction

type CommandFunction func(flags *flag.FlagSet)

type Program

type Program struct {
	Name     string
	Args     []string
	Commands map[string]Command
}

func CreateProgram

func CreateProgram(name string) Program

func (*Program) AddBoolFlag

func (p *Program) AddBoolFlag(name string, value bool, usage string)

func (*Program) AddCommand

func (p *Program) AddCommand(name string, callback CommandFunction, desc string) Command

func (*Program) AddDurationFlag

func (p *Program) AddDurationFlag(name string, value time.Duration, usage string)

func (*Program) AddFloat64Flag

func (p *Program) AddFloat64Flag(name string, value float64, usage string)

func (*Program) AddInt64Flag

func (p *Program) AddInt64Flag(name string, value int64, usage string)

func (*Program) AddIntFlag

func (p *Program) AddIntFlag(name string, value int, usage string)

func (*Program) AddStringFlag

func (p *Program) AddStringFlag(name string, value string, usage string)

func (*Program) AddUint

func (p *Program) AddUint(name string, value uint, usage string)

func (*Program) AddUint64

func (p *Program) AddUint64(name string, value uint64, usage string)

func (*Program) Run

func (p *Program) Run()

Directories

Path Synopsis
demo
basic command
flag command

Jump to

Keyboard shortcuts

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