gocli

package module
v0.0.0-...-0d32f61 Latest Latest
Warning

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

Go to latest
Published: May 23, 2024 License: GPL-3.0 Imports: 4 Imported by: 0

README

gocli

A simple Go CLI built on x/term

Documentation

Overview

Package gocli is a basic command line interface built on x/term that provides tab autocomplete and exception handling.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Exit

func Exit()

Exit restores the original terminal state and exits.

func HandleError

func HandleError(err error)

HandleError handles errors in the standard way, which is to restore terminal to oldState, exit with status 0 for EOF and print the error and exit with status 1 otherwise.

func ReadLine

func ReadLine() (line string, err error)

func ReadPassword

func ReadPassword(prompt string) (line string, err error)

func SetBracketedPasteMode

func SetBracketedPasteMode(on bool)

func SetPrompt

func SetPrompt(prompt string)

wrap and export key *term.Terminal methods

func SetSize

func SetSize(width int, height int) error

func Start

func Start(c *Context)

Start executes init, and then dispatch. dispatch must be a function that dispatches on the commands in cmds.

func Write

func Write(msg string)

Write writes msg to stdout. Write must be used instead of fmt.Print and related functions for correct terminal behaviour. This is a reexport of *term.Terminal.Write but with error handling added.

Types

type Context

type Context struct {
	// An init function to run before starting the CLI. User can set to "func() {}" if not needed.
	Init func()
	// The command dispatcher. Dispatches on args which is the line read the command line.
	// The first element in args is the command, the remaining elements are the arguments.
	Dispatch func(args []string)
	// Function that validates the admin password.
	AdminPasswordValidator func(adminName, password string) (isPWValid bool, err error)
	// Function that updates the admin password.
	AdminPasswordUpdater func(adminName, newPassword string) error
	// The commands recognized by the CLI.
	Cmds                 []string
	StartMsg             string
	DefaultAdminPassword string
	AskToLogin           bool // toggles if login is required to use CLI
}

Context defines the context for the CLI, which the user is to provide.

Jump to

Keyboard shortcuts

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