Axon is a lightweight Go library for building interactive command-line applications, providing simple and easy-to-use terminal interaction features.

go get github.com/muleiwu/axon
package main
import (
"fmt"
"github.com/muleiwu/axon"
"github.com/muleiwu/axon/pkg/question"
)
func main() {
// Standard question
name := axon.Question("Please enter your name: ", "")
fmt.Printf("Hello, %s!\n", name)
// Confirmation question
proceed := axon.Confirm("Continue? (y/n) ", true)
if proceed {
fmt.Println("Continuing...")
} else {
fmt.Println("Cancelled")
return
}
// Password input (hidden display)
password := axon.Password("Please enter password: ", "")
fmt.Println("Password saved")
// Phone number (with validation and masking)
phone := axon.PhoneNumber("Please enter mobile number: ", "")
fmt.Println("Mobile number saved")
// Selection list
items := []question.SelectionItem{
{Label: "Option 1", Description: "This is the first option", Value: "option1"},
{Label: "Option 2", Description: "This is the second option", Value: "option2"},
{Label: "Option 3", Description: "This is the third option", Value: "option3"},
}
selected := axon.Selection("Please select an option:", items)
if selected == "exit" {
fmt.Println("You chose to exit")
} else {
fmt.Printf("You selected: %s\n", selected)
}
// Multi select
selectedValues := axon.MultiSelect("Please select options:", items)
fmt.Printf("You selected: %v\n", selectedValues)
// Typed inputs
age := axon.Number("Please enter age: ", 18)
email := axon.Email("Please enter email: ", "")
fmt.Println(age, email)
// Form wizard
result := axon.Form("Create user", []axon.FormField{
{Name: "name", Question: "Name: ", Kind: axon.FieldText, Required: true},
{Name: "email", Question: "Email: ", Kind: axon.FieldEmail, Required: true},
{Name: "admin", Question: "Admin? ", Kind: axon.FieldConfirm},
})
fmt.Printf("%#v\n", result)
}
func Question(question string, defaultValue string) string
Ask the user a standard question and get text input.
question: The question text to display to the userdefaultValue: Default value, used when the user presses enter directlyfunc Confirm(question string, defaultValue bool) bool
Ask the user a confirmation question to get a yes/no answer.
question: The question text to display to the userdefaultValue: Default value, used when the user presses enter directlyfunc Password(question string, defaultValue string) string
Ask the user for a password, which will be hidden during input.
question: The question text to display to the userdefaultValue: Default value, used when the user presses enter directlyfunc PhoneNumber(question string, defaultValue string) string
Ask the user for a phone number, with format validation. The input is automatically masked to protect user privacy.
question: The question text to display to the userdefaultValue: Default value, used when the user presses enter directlyfunc Selection(question string, items []SelectionItem) string
Let the user select an item from a list.
question: The question text to display to the useritems: The list of selection itemsfunc MultiSelect(question string, items []SelectionItem) []string
Let the user select multiple items. Supports 1,3-5 and all; returns an empty slice when the user exits.
func SearchSelect(question string, items []SelectionItem) string
Filter options by keyword first, then ask the user to select from the matches.
func KeyboardSelect(question string, items []SelectionItem) string
Navigate with arrow keys or j/k, confirm with Enter, and exit with q.
func ValidatedQuestion(question string, defaultValue string, validators ...Validator) string
Read text input and run one or more custom validators. Returning an error asks the user again.
func Number(question string, defaultValue int) int
func Email(question string, defaultValue string) string
func URL(question string, defaultValue string) string
func Path(question string, defaultValue string, mustExist bool) string
func DateTime(question string, defaultValue time.Time, layout string) time.Time
Read integer, email, URL, path, and time values. DateTime uses 2006-01-02 15:04:05 when layout is empty.
func MultiLine(question string, defaultValue string) string
Read multiline text. Enter a single . line to finish.
func DangerousConfirm(question string, expected string) bool
Return true only when the user types expected exactly. Useful for destructive operations.
func Form(title string, fields []FormField) FormResult
func Wizard(title string, fields []FormField) FormResult
Collect multiple fields in order and return a map[string]any. Wizard is a semantic alias for Form.
func Spinner(message string) SpinnerController
func Progress(total int, message string) ProgressReporter
Use Spinner for indeterminate tasks and Progress for tasks with a known total.
When creating a selection list, you can define options using the SelectionItem structure:
type SelectionItem struct {
Label string // The display label
Description string // Description information
Value string // Actual value
Disabled bool // Whether the item is disabled
}
Each interaction mode has a dedicated interface in pkg/interfaces, and default implementations live in pkg/question. Replace any mode through its setter:
type MyQuestioner struct{}
func (MyQuestioner) Ask(question string, defaultValue string) string {
return defaultValue
}
func main() {
axon.SetQuestioner(MyQuestioner{})
name := axon.Question("Please enter name: ", "axon")
fmt.Println(name)
}
This project is open source under the LICENSE license.