Skip to content

Single Command

Single command CLI applications are perfect for simple tools that perform a single action. This is the simplest way to create a CLI application.

Basic Example

go
package main

import (
	"fmt"

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

func main() {
	app := cli.NewSingleProgram(&cli.SingleProgramConfig{
		Name:    "single",
		Usage:   "single is a program that has a single command.",
		Version: "0.0.1",
		Flags: []cli.Flag{
			&cli.StringFlag{
				Name:  "string",
				Usage: "string flag",
				Value: "default value",
			},
			&cli.IntFlag{
				Name:  "int",
				Usage: "int flag",
				Value: 0,
			},
			&cli.BoolFlag{
				Name:  "bool",
				Usage: "bool flag",
				Value: false,
			},
		},
	})

	app.Command(func(ctx *cli.Context) error {
		fmt.Printf("String: %s\n", ctx.String("string"))
		fmt.Printf("Int: %d\n", ctx.Int("int"))
		fmt.Printf("Bool: %t\n", ctx.Bool("bool"))
		return nil
	})

	app.Run()
}

Configuration Options

SingleProgramConfig

FieldTypeDescription
NamestringThe name of the program
HelpNamestringFull name of command for help
UsagestringDescription of the program
UsageTextstringText to override the USAGE section of help
ArgsUsagestringDescription of the program argument format
VersionstringVersion of the program
DescriptionstringDescription of the program
EnableBashCompletionboolBoolean to enable bash completion commands
HideHelpboolBoolean to hide built-in help command and help flag
HideHelpCommandboolBoolean to hide built-in help command but keep help flag
HideVersionboolBoolean to hide built-in version flag
Flags[]FlagList of flags to parse

Usage

bash
# Run with default values
./single

# Run with flags
./single --string "hello" --int 42 --bool

# Show help
./single --help

# Show version
./single --version

Released under the MIT License.