cli

package
v1.2.9 Latest Latest
Warning

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

Go to latest
Published: Jun 19, 2025 License: MIT Imports: 5 Imported by: 0

README

cli

This project is a Go library for building powerful and user-friendly command-line interfaces (CLIs). The library makes it easy to create and manage complex command structures, parse command-line arguments, and provide helpful error messages and usage information to the user.



Installation

go get oss.nandlabs.io/golly/cli

Features

  • Easy to use API for building complex command structures
  • Argument parsing and validation
  • Automatically generates usage and help information
  • Written in Go and follows best practices for Go programming
Usage
Default
package main

import (
    "fmt"
    "log"
    "os"

    "oss.nandlabs.io/golly/cli"
)

func main() {
    app := cli.NewCLI()

    gollyCmd := &cli.Command{
        Name:        "welcome",
        Description: "Welcome command",
        Handler: func(ctx *cli.Context) error {
            fmt.Println("welcome to golly!")
            return nil
        },
    }

    app.AddCommand(gollyCmd)

    if err := app.Execute(); err != nil {
        log.Fatal(err)
    }
}

CLI Command and Output

~ % go run main.go welcome
welcome to golly!
Subcommands
package main

import (
    "fmt"
    "log"
    "os"

    "oss.nandlabs.io/golly/cli"
)

func main() {

    app := cli.NewCLI()

    welcomeCmd := &cli.Command{
        Name: "welcome",
        Description: "Welcome to golly",
        Handler: func(ctx *cli.Context) error {
            fmt.Println("welcome to golly")
            return nil
        },
        SubCommands: map[string]*cli.Command{
            "home": {
                Name:        "home",
                Description: "welcome home",
                Handler: func(ctx *cli.Context) error {
                    fmt.Println("welcome home")
                    return nil
                },
            },
            "office": {
                Name:        "level",
                Description: "level of the skill",
                Handler: func(ctx *cli.Context) error {
                    fmt.Println("welcome office")
                    return nil
                },
            },
        },
    }

    app.AddCommand(welcomeCmd)

    if err := app.Execute(); err != nil {
        fmt.Println("Error:", err)
    }
}

CLI Commands and Output

~ % go run main.go welcome
welcome to golly
~ % go run main.go welcome home
welcome home
~ % go run main.go welcome office
welcome office
Flags
package main

import (
    "fmt"
    "log"
    "os"

    "oss.nandlabs.io/golly/cli"
)

func main() {
    app := cli.NewCLI()

    server := &cli.Command{
        Name:        "server",
        Description: "Server command",
        Handler: func(ctx *cli.Context) error {
        region, _ := ctx.GetFlag("region")
            fmt.Printf("IN REGION, %s\n", region)
            return nil
        },
        Flags: []cli.Flag{
            {
                Name:    "region",
                Aliases: []string{"r"},
                Usage:   "Provide region",
                Default: "",
            },
        },
        SubCommands: map[string]*cli.Command{
            "create": {
                Name:        "create",
                Description: "create",
                Handler: func(ctx *cli.Context) error {
                    typ, _ := ctx.GetFlag("type")
                    fmt.Printf("SERVER TYPE %s\n", typ)
                    return nil
                },
                Flags: []cli.Flag{
                    {
                        Name:    "type",
                        Aliases: []string{"t"},
                        Usage:   "server type",
                        Default: "",
                    },
                },
            },
        },
    }

    app.AddCommand(server)

    if err := app.Execute(); err != nil {
        fmt.Println("Error:", err)
    }
}

CLI Commands and Output

~ % go run main.go server --region="us-east-1"
IN REGION, us-east-1
~ % go run main.go server create --type="t3.medium"
SERVER TYPE t3.medium

Documentation

Overview

Package cli provides a command-line interface (CLI) framework for building command-line applications.

	if err := app.Execute(); err != nil {
		fmt.Println("Error:", err)
	}
}

For more information and examples, please refer to the package documentation at:

https://github.com/nandlabs/golly/cli

Package cli provides functionality for handling command-line flags.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type CLI added in v1.2.2

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

CLI represents the command-line interface.

func NewCLI added in v1.2.2

func NewCLI() *CLI

NewCLI creates a new CLI instance.

func (*CLI) AddCommand added in v1.2.2

func (cli *CLI) AddCommand(cmd *Command)

AddCommand adds a root command to the CLI.

func (*CLI) AddVersion added in v1.2.8

func (cli *CLI) AddVersion(version string)

func (*CLI) Execute added in v1.2.2

func (cli *CLI) Execute() error

Execute executes the command specified by the command-line arguments.

type Command

type Command struct {
	Name        string
	Usage       string
	Version     string
	Aliases     []string
	Action      func(ctx *Context) error
	SubCommands map[string]*Command
	Flags       []*Flag
}

Command represents a command in the CLI.

func NewCommand added in v1.2.2

func NewCommand(name, description, version string, action func(ctx *Context) error) *Command

NewCommand creates a new command with the given name, description, and handler function.

func (*Command) AddSubCommand added in v1.2.2

func (cmd *Command) AddSubCommand(subCmd *Command)

AddSubCommand adds a subcommand to the command.

type Context

type Context struct {
	CommandStack []string          // CommandStack stores the stack of executed commands.
	Flags        map[string]string // Flags stores the command-line flags and their values.
}

Context represents the context for the command-line interface.

func NewCLIContext added in v1.2.2

func NewCLIContext() *Context

NewCLIContext creates a new CLI context.

func (*Context) GetFlag

func (ctx *Context) GetFlag(name string) (string, bool)

GetFlag retrieves the value of a command-line flag. It returns the value and a boolean indicating whether the flag exists.

func (*Context) SetFlag added in v1.2.2

func (ctx *Context) SetFlag(name, value string)

SetFlag sets the value of a command-line flag.

type Flag

type Flag struct {
	Name    string   // Name of the flag.
	Aliases []string // Aliases for the flag.
	Usage   string   // Usage information for the flag.
	Default string   // Default value for the flag.
}

Flag represents a command-line flag.

Jump to

Keyboard shortcuts

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