Skip to content
/ go-cli Public

Package cli provides a simple way of creating new command-line interface (CLI) programs.

License

Notifications You must be signed in to change notification settings

jlubawy/go-cli

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

jlubawy/go-cli

GoDoc Build Status

Package cli provides a simple way of creating new command-line interface (CLI) programs. It is built using nothing but standard packages and is based on the behavior of the 'go' command-line tool (with some minor changes).

Example

The following is a simple program that echoes text back to stdout.

package main

import (
    "flag"
    "fmt"
    "strings"

    "github.com/jlubawy/go-cli"
)

type EchoOptions struct {
    Prefix string
}

var echoOptions EchoOptions

var program = cli.Program{
    Name:        "example",
    Description: "Example is a program demonstrating how to use the cli package.",
    Commands: []cli.Command{
        {
            Name:             "echo",
            ShortDescription: "echo the provided text",
            Description:      "Echo the provided text.",
            ShortUsage:       "[text to echo]",
            SetupFlags: func(fs *flag.FlagSet) {
                fs.StringVar(&echoOptions.Prefix, "prefix", "", "prefix to be added to the text")
            },
            Run: func(args []string) {
                var text string
                if len(args) >= 1 {
                    text = strings.Join(args, " ")
                }
                if echoOptions.Prefix == "" {
                    fmt.Println(text)
                } else {
                    fmt.Printf("%s: %s\n", echoOptions.Prefix, text)
                }
            },
        },
    },
}

func main() { program.RunAndExit() }

If run with no arguments:

$ ./example
Example is a program demonstrating how to use the cli package.

Usage:

    example command [options]

The commands are:

    echo       echo the provided text

Use "example help [command]" for more Information about a command.

Help for a particular sub-command:

$ ./example help echo
Usage: example echo [text to echo]

Echo the provided text.

Options:

    -prefix     prefix to be added to the text (default=)

If run normally:

$ ./example echo -prefix="Some prefix" Some random text
Some prefix: Some random text

About

Package cli provides a simple way of creating new command-line interface (CLI) programs.

Resources

License

Stars

Watchers

Forks

Packages

No packages published