Documentation
¶
Overview ¶
Package cliutils holds helper functions for interacting with a user at a command-line interface.
Index ¶
- Constants
- func Ask(question, dfault string, in io.Reader, out io.Writer) (answer string, err error)
- func AskBool(question string, dfault bool, in io.Reader, out io.Writer) (answer bool, err error)
- func MustAsk(question, dfault string) (answer string)
- func Printf(format string, args ...any) (int, error)
- func Sprintf(format string, args ...any) string
Constants ¶
const (
NoDefault = "[[twos.dev/winter/cliutils.NoDefault]]"
)
Variables ¶
This section is empty.
Functions ¶
func Ask ¶
Ask writes question to out with a visual treatment indicating a prompt, then reads from in until it reads a line break. It returns all text read until the line break (excluding it).
If dfault is NoDefault, when the returned text would be an empty string, the function repeats from the start until the returned text would be nonempty.
If dfault is any other string, when the returned text would be an empty string, dfault is returned instead.
question is implicitly passed through Sprintf for formatting.
If using Ask with os.Stdin and os.Stdout and no error is expected, use MustAsk instead.
func AskBool ¶
AskBool is like Ask but asks the user a yes/no question instead of a generic string question.
AskBool writes question to out with a visual treatment indicating a prompt, then reads from in until it reads a line break.
If the read text is "y", "Y", "yes", or similar; true is returned. If the read text is "n", "N", "no", or similar; false is returned. If the read text is the empty string, dfault is returned. Otherwise, the function repeats from the start until a sufficient answer is read.
question is implicitly passed through Sprintf for formatting.
func MustAsk ¶
MustAsk is like Ask but panics on error and always interacts with stdin/stdout.
MustAsk writes question to stdout with a visual treatment indicating a prompt, then reads from stdin until it reads a line break. It returns all text read until the line break (excluding it).
If dfault is NoDefault, when the returned text would be an empty string, the function repeats from the start until the returned text would be nonempty.
If dfault is any other string, when the returned text would be an empty string, dfault is returned instead.
question is implicitly passed through Sprintf for formatting.
If any issue occurs, MustAsk panics. To handle errors or use other readers or writers, use Ask instead.
func Printf ¶
Printf is like fmt.Printf, but applies several visual treatments to make format pleasant to read at a terminal, especially when it spans multiple lines or paragraphs.
Specifically, single newlines and common indentation are ignored and the text is wrapped to 80 characters (allowing strings to be well-formatted in both source code and output), and limited Markdown support is applied.
func Sprintf ¶
Sprintf is like fmt.Sprintf, but applies visual treatments for pleasant reading at a terminal, especially when it spans multiple lines or paragraphs.
Specifically, single newlines and common indentation are ignored and the text is wrapped to 80 characters (allowing strings to be well-formatted in both source code and output), and limited Markdown support is applied.
Types ¶
This section is empty.