Documentation
¶
Index ¶
- func GenerateManPage(root any, tr help.Translator) (string, error)
- type Base
- type Command
- func (c *Command) AddCommand(name string, cmd *parser.CommandNode)
- func (c *Command) ConfirmAction(prompt, yesText, noText string, defaultChoice bool) (bool, error)
- func (c *Command) Execute() error
- func (c *Command) GetRoot() any
- func (c *Command) Name() string
- func (c *Command) PromptText(prompt, placeholder string) (string, error)
- func (c *Command) Reload() error
- func (c *Command) SelectOption(prompt string, options []string) (string, error)
- func (c *Command) SetName(name string)
- func (c *Command) SetTranslator(tr help.Translator)
- func (c Command) StartProgressBar(message string, total int) *ProgressBarModel
- func (c Command) StartSpinner(message string) *SpinnerModel
- func (c *Command) Table(headers []string, data [][]string) error
- type ManCmd
- type ProgressBarModel
- type SpinnerModel
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func GenerateManPage ¶
func GenerateManPage(root any, tr help.Translator) (string, error)
GenerateManPage generates a man page for the declarative struct
Example:
type RootCmd struct {
cli.Base
Poll PollCmd `cmd:"poll" help:"Ask the user preferred hero"`
Man ManCmd `cmd:"man" help:"Generate man page"`
}
man, err := cli.GenerateManPage(&RootCmd{}, nil)
if err != nil {
return "", err
}
GenerateManPage automatically uses a zero-value instance of the root struct to exclude any dynamic commands.
Types ¶
type Command ¶
type Command struct {
Use string
Short string
Long string
// contains filtered or unexported fields
}
Command represents a CLI command.
func NewCommandFromStruct ¶
NewCommandFromStruct returns a new Command created from a struct.
Example:
type RootCmd struct {
cli.Base
Poll PollCmd `cmd:"poll" help:"Ask the user preferred hero"`
Man ManCmd `cmd:"man" help:"Generate man page"`
}
cmd, err := cli.NewCommandFromStruct(&RootCmd{})
if err != nil {
return nil, err
}
err := cmd.Execute()
func (*Command) AddCommand ¶
func (c *Command) AddCommand(name string, cmd *parser.CommandNode)
AddCommand adds a dynamic command to the application.
func (*Command) ConfirmAction ¶
ConfirmAction prompts the user to confirm an action, it supports customizing the prompt and the text for the "yes" and "no" options. If the user does not provide an answer, the default choice is used.
Example:
confirm, err := myApp.CLI.ConfirmAction(
"Do you like Batman?",
"Yes", "No",
true,
)
if err != nil {
fmt.Println(err)
return err
}
if confirm {
fmt.Println("Everybody likes Batman!")
} else {
fmt.Println("You don't like Batman...")
}
func (*Command) PromptText ¶
PromptText prompts the user to input a text, it supports customizing the prompt and the placeholder.
Example:
response, err := myApp.CLI.PromptText(
"What is your name?",
"Bruce Wayne",
)
if err != nil {
fmt.Println(err)
return err
}
fmt.Printf("Hello %s!\n", response)
func (*Command) SelectOption ¶
SelectOption prompts the user to select an option from a list of options.
Example:
selected, err := myApp.CLI.SelectOption(
"What is your preferred hero?",
[]string{"Batman", "Ironman", "Spiderman", "Robin", "None"},
)
if err != nil {
fmt.Println(err)
return err
}
fmt.Printf("You selected %s!\n", selected)
func (*Command) SetTranslator ¶
func (c *Command) SetTranslator(tr help.Translator)
SetTranslator sets the translator for the application.
func (Command) StartProgressBar ¶
func (c Command) StartProgressBar(message string, total int) *ProgressBarModel
StartProgressBar starts a progress bar with a message and a total. The progress bar is stopped automatically when it reaches the total or manually by calling the Stop method on the returned model.
Example:
progressBar := myApp.CLI.StartProgressBar("Loading the batmobile...", 100)
for i := 0; i < 100; i++ {
progressBar.Increment(1)
time.Sleep(50 * time.Millisecond)
}
func (Command) StartSpinner ¶
func (c Command) StartSpinner(message string) *SpinnerModel
StartSpinner starts a spinner with a message. The spinner can be stopped by calling the Stop method on the returned model.
Example:
spinner := myApp.CLI.StartSpinner("Loading the batmobile...")
time.Sleep(3 * time.Second)
spinner.Stop()
type ManCmd ¶
type ManCmd struct {
Base
// contains filtered or unexported fields
}
ManCmd is the command to generate the man page
type ProgressBarModel ¶
type ProgressBarModel struct {
// contains filtered or unexported fields
}
func (*ProgressBarModel) Increment ¶
func (m *ProgressBarModel) Increment(inc int)
func (*ProgressBarModel) Stop ¶
func (m *ProgressBarModel) Stop()
func (*ProgressBarModel) UpdateMessage ¶
func (m *ProgressBarModel) UpdateMessage(msg string)
UpdateMessage updates the title logic.
type SpinnerModel ¶
type SpinnerModel struct {
// contains filtered or unexported fields
}
func (*SpinnerModel) Stop ¶
func (m *SpinnerModel) Stop()
func (*SpinnerModel) UpdateMessage ¶
func (m *SpinnerModel) UpdateMessage(message string)
UpdateMessage updates the spinner message dynamically.
Example:
spinner.UpdateMessage("Loading the batcave...")